This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
443 lines (434 loc) · 16.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Fibaro Melcloud Platform plugin for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "Melcloud",
// "name": "Melcloud",
// "username": "PUT USERNAME OF YOUR MELCLOUD ACCOUNT HERE",
// "password": "PUT PASSWORD OF YOUR MELCLOUD ACCOUNT HERE"
// }
// ],
//
// When you attempt to add a device, it will ask for a "PIN code".
// The default code for all HomeBridge accessories is 031-45-154.
'use strict';
var Service, Characteristic;
var request = require("request");
function MelcloudPlatform(log, config){
this.log = log;
this.language = config["language"];
this.username = config["username"];
this.password = config["password"];
this.ContextKey = null;
this.UseFahrenheit = null;
this.CurrentHeatingCoolingStateUUID = (new Characteristic.CurrentHeatingCoolingState()).UUID;
this.TargetHeatingCoolingStateUUID = (new Characteristic.TargetHeatingCoolingState()).UUID;
this.CurrentTemperatureUUID = (new Characteristic.CurrentTemperature()).UUID;
this.TargetTemperatureUUID = (new Characteristic.TargetTemperature()).UUID;
this.TemperatureDisplayUnitsUUID = (new Characteristic.TemperatureDisplayUnits()).UUID;
this.RotationSpeedUUID = (new Characteristic.RotationSpeed()).UUID;
this.CurrentHorizontalTiltAngleUUID = (new Characteristic.CurrentHorizontalTiltAngle()).UUID;
this.TargetHorizontalTiltAngleUUID = (new Characteristic.TargetHorizontalTiltAngle()).UUID;
this.CurrentVerticalTiltAngleUUID = (new Characteristic.CurrentVerticalTiltAngle()).UUID;
this.TargetVerticalTiltAngleUUID = (new Characteristic.TargetVerticalTiltAngle()).UUID;
this.currentAirInfoExecution = 0;
this.airInfoExecutionPending = [];
}
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-melcloud", "Melcloud", MelcloudPlatform);
}
MelcloudPlatform.prototype = {
accessories: function(callback) {
this.log("Fetching Melcloud devices...");
var that = this;
// Login
var url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin";
var form = {
AppVersion: "1.9.3.0",
CaptchaChallenge: "",
CaptchaResponse: "",
Email: this.username,
Language: this.language,
Password: this.password,
Persist: "true"
};
var method = "post";
var that = this;
request({
url: url,
form: form,
method: method
}, function(err, response) {
if (err) {
that.log("There was a problem sending login to: " + url);
that.log(err);
callback([]);
} else {
var r = eval("(" + response.body + ")");
that.ContextKey = r.LoginData.ContextKey;
that.UseFahrenheit = r.LoginData.UseFahrenheit;
that.log("ContextKey: " + that.ContextKey);
that.getDevices(callback);
}
});
},
getDevices: function(callback) {
var url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices";
var method = "get";
var that = this;
request({
url: url,
method: method,
headers: {
"X-MitsContextKey" : this.ContextKey
}
}, function(err, response) {
if (err) {
that.log("There was a problem getting devices from: " + url);
that.log(err);
} else {
var r = eval("(" + response.body + ")");
var foundAccessories = [];
for (var b = 0; b < r.length; b++) {
var building = r[b];
var devices = building.Structure.Devices;
that.createAccessories(building, devices, foundAccessories);
for (var f = 0; f < building.Structure.Floors.length; f++) {
var devices = building.Structure.Floors[f].Devices;
that.createAccessories(building, devices, foundAccessories);
for (var a = 0; a < building.Structure.Floors[f].Areas.length; a++) {
var devices = building.Structure.Floors[f].Areas[a].Devices;
that.createAccessories(building, devices, foundAccessories);
}
}
for (var a = 0; a < building.Structure.Areas.length; a++) {
var devices = building.Structure.Areas[a].Devices;
that.createAccessories(building, devices, foundAccessories);
}
}
callback(foundAccessories);
}
});
},
createAccessories: function(building, devices, foundAccessories) {
for (var d = 0; d < devices.length; d++){
var device = devices[d];
var accessory = new MelcloudBridgedAccessory([
{
controlService: new Service.Thermostat(device.DeviceName),
characteristics: [
Characteristic.CurrentHeatingCoolingState,
Characteristic.TargetHeatingCoolingState,
Characteristic.CurrentTemperature,
Characteristic.TargetTemperature,
Characteristic.TemperatureDisplayUnits,
Characteristic.RotationSpeed,
Characteristic.CurrentHorizontalTiltAngle,
Characteristic.TargetHorizontalTiltAngle,
Characteristic.CurrentVerticalTiltAngle,
Characteristic.TargetVerticalTiltAngle
]
}
]);
accessory.platform = this;
accessory.remoteAccessory = device;
accessory.id = device.DeviceID;
accessory.name = device.DeviceName;
accessory.model = "";
accessory.manufacturer = "Mitsubishi";
accessory.serialNumber = device.SerialNumber;
accessory.airInfo = null;
accessory.buildingId = building.ID;
this.log("Found device: " + device.DeviceName);
foundAccessories.push(accessory);
}
},
proxyAirInfo: function(callback, characteristic, service, homebridgeAccessory, value, operation) {
if (homebridgeAccessory.airInfo != null) {
this.log("Data already available for: " + homebridgeAccessory.name + " - " + characteristic.displayName);
operation(callback, characteristic, service, homebridgeAccessory, value);
if (this.airInfoExecutionPending.length) {
var args = this.airInfoExecutionPending.shift()
this.log("Dequeuing remote request for. " + args[3].name + " - " + args[1].displayName);
this.proxyAirInfo.apply(this, args);
}
return;
}
this.log("Getting data for: " + homebridgeAccessory.name + " - " + characteristic.displayName);
if (this.currentAirInfoExecution < 1) {
homebridgeAccessory.airInfoRequestSent = true;
this.currentAirInfoExecution++;
var url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=" + homebridgeAccessory.id + "&buildingID=" + homebridgeAccessory.buildingId;
var method = "get";
var that = this;
request({
url: url,
method: method,
headers: {
"X-MitsContextKey" : homebridgeAccessory.platform.ContextKey
}
}, function(err, response) {
if (err || response.body.search("<!DOCTYPE html>") != -1) {
that.log("There was a problem getting info from: " + url);
that.log("for device: " + homebridgeAccessory.name);
that.log("Error: " + err);
homebridgeAccessory.airInfo = null;
callback();
} else {
homebridgeAccessory.airInfo = eval("(" + response.body + ")");
operation(callback, characteristic, service, homebridgeAccessory, value);
// Cache airInfo data for 1 minutes
setTimeout( function(){
homebridgeAccessory.airInfo = null;
}, 60*1000 );
}
that.currentAirInfoExecution--;
if (that.airInfoExecutionPending.length) {
var args = that.airInfoExecutionPending.shift()
that.log("Dequeuing remote request for: " + args[3].name + " - " + args[1].displayName);
that.proxyAirInfo.apply(that, args);
}
});
} else {
this.log("Queing remote request data for: " + homebridgeAccessory.name + " - " + characteristic.displayName);
this.airInfoExecutionPending.push(arguments);
}
},
getAccessoryValue: function(callback, characteristic, service, homebridgeAccessory, value) {
var r = homebridgeAccessory.airInfo;
if (characteristic.UUID == homebridgeAccessory.platform.CurrentHeatingCoolingStateUUID) {
if (r.Power == false) {
callback(undefined, Characteristic.CurrentHeatingCoolingState.OFF);
return;
} else {
switch (r.OperationMode) {
case 1:
callback(undefined, Characteristic.CurrentHeatingCoolingState.HEAT);
return;
case 3:
callback(undefined, Characteristic.CurrentHeatingCoolingState.COOL);
return;
default:
// Melcloud can return also 2 (deumidity), 7 (Ventilation), 8 (auto)
// We try to return 5 which is undefined in homekit
callback(undefined, 5);
return;
}
}
} else if (characteristic.UUID == homebridgeAccessory.platform.TargetHeatingCoolingStateUUID) {
if (r.Power == false) {
callback(undefined, Characteristic.TargetHeatingCoolingState.OFF);
return;
} else {
switch (r.OperationMode) {
case 1:
callback(undefined, Characteristic.TargetHeatingCoolingState.HEAT);
return;
case 3:
callback(undefined, Characteristic.TargetHeatingCoolingState.COOL);
return;
case 8:
callback(undefined, Characteristic.TargetHeatingCoolingState.AUTO);
return;
default:
// Melcloud can return also 2 (deumidity), 7 (Ventilation)
// We try to return 5 which is undefined in homekit
callback(undefined, 5);
return;
}
}
} else if (characteristic.UUID == homebridgeAccessory.platform.CurrentTemperatureUUID) {
callback(undefined, r.RoomTemperature);
return;
} else if (characteristic.UUID == homebridgeAccessory.platform.TargetTemperatureUUID) {
callback(undefined, r.SetTemperature);
return;
} else if (characteristic.UUID == homebridgeAccessory.platform.TemperatureDisplayUnitsUUID) {
if (homebridgeAccessory.platform.UseFahrenheit) {
callback(undefined, Characteristic.TemperatureDisplayUnits.FAHRENHEIT);
} else {
callback(undefined, Characteristic.TemperatureDisplayUnits.CELSIUS);
}
return;
} else if (characteristic.UUID == homebridgeAccessory.platform.RotationSpeedUUID) {
var SetFanSpeed = r.SetFanSpeed,
NumberOfFanSpeeds = r.NumberOfFanSpeeds;
var fanSpeed = SetFanSpeed/NumberOfFanSpeeds*100.0;
callback(undefined, fanSpeed);
return;
} else if (characteristic.UUID == homebridgeAccessory.platform.CurrentHorizontalTiltAngleUUID ||
characteristic.UUID == homebridgeAccessory.platform.TargetHorizontalTiltAngleUUID) {
var VaneHorizontal = r.VaneHorizontal;
var HorizontalTilt = -90.0 + 45.0 * (VaneHorizontal - 1);
callback(undefined, HorizontalTilt);
return;
} else if (characteristic.UUID == homebridgeAccessory.platform.CurrentVerticalTiltAngleUUID ||
characteristic.UUID == homebridgeAccessory.platform.TargetVerticalTiltAngleUUID) {
var VaneVertical = r.VaneVertical;
var VerticallTilt = 90.0 - 45.0 * (5 - VaneVertical);
callback(undefined, VerticallTilt);
return;
} else {
callback(undefined, 0);
return;
}
},
setAccessoryValue: function(callback, characteristic, service, homebridgeAccessory, value) {
var r = homebridgeAccessory.airInfo;
if (characteristic.UUID == homebridgeAccessory.platform.TargetHeatingCoolingStateUUID) {
switch (value) {
case Characteristic.TargetHeatingCoolingState.OFF:
r.Power = false;
r.EffectiveFlags = 1;
break;
case Characteristic.TargetHeatingCoolingState.HEAT:
r.Power = true;
r.OperationMode = 1;
r.EffectiveFlags = 1 + 2;
break;
case Characteristic.TargetHeatingCoolingState.COOL:
r.Power = true;
r.OperationMode = 3;
r.EffectiveFlags = 1 + 2;
break;
case Characteristic.TargetHeatingCoolingState.AUTO:
r.Power = true;
r.OperationMode = 8;
r.EffectiveFlags = 1 + 2;
break;
default:
callback();
return;
}
} else if (characteristic.UUID == homebridgeAccessory.platform.TargetTemperatureUUID) {
r.SetTemperature = value;
r.EffectiveFlags = 4;
} else if (characteristic.UUID == homebridgeAccessory.platform.TemperatureDisplayUnitsUUID) {
var UseFahrenheit=false;
if (value == Characteristic.TemperatureDisplayUnits.FAHRENHEIT)
UseFahrenheit = true;
homebridgeAccessory.platform.updateApplicationOptions(UseFahrenheit);
homebridgeAccessory.platform.UseFahrenheit = UseFahrenheit;
callback();
return;
} else if (characteristic.UUID == homebridgeAccessory.platform.RotationSpeedUUID) {
r.SetFanSpeed = (value/100.0 * r.NumberOfFanSpeeds).toFixed(0);
r.EffectiveFlags = 8;
} else if (characteristic.UUID == homebridgeAccessory.platform.TargetHorizontalTiltAngleUUID) {
r.VaneHorizontal = ((value + 90.0)/45.0 + 1.0).toFixed(0);
r.EffectiveFlags = 256;
} else if (characteristic.UUID == homebridgeAccessory.platform.TargetVerticalTiltAngleUUID) {
r.VaneVertical = ((value + 90.0) / 45.0 + 1.0).toFixed(0);
r.EffectiveFlags = 16;
} else {
callback();
return;
}
var url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta";
var method = "post";
var body = JSON.stringify(homebridgeAccessory.airInfo);
var that = this;
request({
url: url,
method: method,
body: body,
headers: {
"X-MitsContextKey": homebridgeAccessory.platform.ContextKey,
"content-type": "application/json"
}
}, function(err, response) {
if (err) {
that.log("There was a problem setting info to: " + url);
that.log(err);
}
callback();
});
},
updateApplicationOptions: function(UseFahrenheit) {
var url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/User/UpdateApplicationOptions";
var method = "post";
var body = "{UseFahrenheit:" + UseFahrenheit + ",EmailOnCommsError:false,EmailOnUnitError:false,EmailCommsErrors:1,EmailUnitErrors:1,RestorePages:false,MarketingCommunication:false,AlternateEmailAddress:\"\",Fred:4}";
var that = this;
request({
url: url,
method: method,
body: body,
headers: {
"X-MitsContextKey": this.ContextKey,
"content-type": "application/json"
}
}, function(err, response) {
if (err) {
that.log("There was a problem setting Application Option to: " + url);
that.log(err);
}
});
},
getInformationService: function(homebridgeAccessory) {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, homebridgeAccessory.name)
.setCharacteristic(Characteristic.Manufacturer, homebridgeAccessory.manufacturer)
.setCharacteristic(Characteristic.Model, homebridgeAccessory.model)
.setCharacteristic(Characteristic.SerialNumber, homebridgeAccessory.serialNumber);
return informationService;
},
bindCharacteristicEvents: function(characteristic, service, homebridgeAccessory) {
var readOnly = true;
for (var i = 0; i < characteristic.props.perms.length; i++)
if (characteristic.props.perms[i] == "pw")
readOnly = false;
if (!readOnly) {
characteristic
.on('set', function(value, callback, context) {
if( context !== 'fromMelcloud') {
homebridgeAccessory.platform.proxyAirInfo(callback, characteristic, service, homebridgeAccessory, value, homebridgeAccessory.platform.setAccessoryValue);
}
}.bind(this) );
}
characteristic
.on('get', function(callback) {
homebridgeAccessory.platform.proxyAirInfo(callback, characteristic, service, homebridgeAccessory, null, homebridgeAccessory.platform.getAccessoryValue);
}.bind(this) );
},
getServices: function(homebridgeAccessory) {
var services = [];
var informationService = homebridgeAccessory.platform.getInformationService(homebridgeAccessory);
services.push(informationService);
for (var s = 0; s < homebridgeAccessory.services.length; s++) {
var service = homebridgeAccessory.services[s];
for (var i=0; i < service.characteristics.length; i++) {
var characteristic = service.controlService.getCharacteristic(service.characteristics[i]);
if (characteristic == undefined)
characteristic = service.controlService.addCharacteristic(service.characteristics[i]);
homebridgeAccessory.platform.bindCharacteristicEvents(characteristic, service, homebridgeAccessory);
}
services.push(service.controlService);
}
return services;
}
}
function MelcloudBridgedAccessory(services) {
this.services = services;
}
MelcloudBridgedAccessory.prototype = {
getServices: function() {
var services = [];
var informationService = this.platform.getInformationService(this);
services.push(informationService);
for (var s = 0; s < this.services.length; s++) {
var service = this.services[s];
for (var i=0; i < service.characteristics.length; i++) {
var characteristic = service.controlService.getCharacteristic(service.characteristics[i]);
if (characteristic == undefined)
characteristic = service.controlService.addCharacteristic(service.characteristics[i]);
this.platform.bindCharacteristicEvents(characteristic, service, this);
}
services.push(service.controlService);
}
return services;
}
}