forked from NorthernMan54/homebridge-tcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
617 lines (503 loc) · 23.1 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// This platform integrates Honeywell tcc into homebridge
// As I only own single thermostat, so this only works with one, but it is
// conceivable to handle mulitple with additional coding.
//
// The configuration is stored inside the ../config.json
// {
// "platform": "tcc",
// "name": "Thermostat",
// "username" : "username/email",
// "password" : "password",
// "debug" : "True", - Optional
// "refresh": "60", - Optional
// "devices" : [
// { "deviceID": "123456789", "name" : "Main Floor Thermostat" },
// { "deviceID": "123456789", "name" : "Upper Floor Thermostat" }
// ]
// }
//
/*jslint node: true */
'use strict';
var tcc = require('./lib/tcc.js');
var Accessory, Service, Characteristic, UUIDGen, CommunityTypes;
var myAccessories = [];
var session; // reuse the same login session
var updating; // Only one change at a time!!!!
module.exports = function(homebridge) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
homebridge.registerPlatform("homebridge-tcc", "tcc", tccPlatform);
}
function tccPlatform(log, config, api) {
this.username = config['username'];
this.password = config['password'];
this.refresh = config['refresh'] || 60; // Update every minute
this.debug = config['debug'] || false;
this.log = log;
this.devices = config['devices'];
updating = false;
}
tccPlatform.prototype = {
accessories: function(callback) {
this.log("Logging into tcc...");
var that = this;
tcc.setCharacteristic(Characteristic);
tcc.setDebug(this.debug);
tcc.login(that.username, that.password).then(function(login) {
this.log("Logged into tcc!", this.devices);
session = login;
let requests = this.devices.map((device) => {
return new Promise((resolve) => {
session.CheckDataSession(device.deviceID,
function(err, deviceData) {
if (err) {
that.log("Create Device Error", err);
resolve();
} else {
var newAccessory = new tccAccessory(that.log, device.name,
deviceData, that.username, that.password, device.deviceID, that.debug);
// store accessory in myAccessories
myAccessories.push(newAccessory);
resolve();
}
});
});
})
// Need to wait for all devices to be configured
Promise.all(requests).then(() => {
callback(myAccessories);
that.periodicUpdate();
setInterval(that.periodicUpdate.bind(this), this.refresh * 1000);
});
// End of login section
}.bind(this)).fail(function(err) {
// tell me if login did not work!
that.log("Error during Login:", err);
callback(err);
});
}
};
function updateStatus(service, data) {
service.getCharacteristic(Characteristic.TargetTemperature)
.getValue();
service.getCharacteristic(Characteristic.CurrentTemperature)
.getValue();
service.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.getValue();
service.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.getValue();
if (data.latestData.uiData.IndoorHumiditySensorAvailable && data.latestData.uiData.IndoorHumiditySensorNotFault)
service.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.getValue();
if (data.latestData.uiData.SwitchAutoAllowed) {
service.getCharacteristic(Characteristic.CoolingThresholdTemperature)
.getValue();
service.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.getValue();
}
}
tccPlatform.prototype.periodicUpdate = function(t) {
this.log("periodicUpdate");
var t = updateValues(this);
}
function updateValues(that) {
that.log("updateValues", myAccessories.length);
myAccessories.forEach(function(accessory) {
session.CheckDataSession(accessory.deviceID, function(err, deviceData) {
if (err) {
that.log("ERROR: UpdateValues", accessory.name, err);
that.log("updateValues: Device not reachable", accessory.name);
// accessory.newAccessory.updateReachability(false);
tcc.login(that.username, that.password).then(function(login) {
that.log("Logged into tcc!");
session = login;
}.bind(this)).fail(function(err) {
// tell me if login did not work!
that.log("Error during Login:", err);
});
} else {
if (that.debug)
that.log("Update Values", accessory.name, deviceData);
// Data is live
if (deviceData.deviceLive) {
// that.log("updateValues: Device reachable", accessory.name);
// accessory.newAccessory.updateReachability(true);
} else {
that.log("updateValues: Device not reachable", accessory.name);
// accessory.newAccessory.updateReachability(false);
}
if (!tcc.deepEquals(deviceData, accessory.device)) {
that.log("Change", accessory.name, tcc.diff(accessory.device, deviceData));
accessory.device = deviceData;
updateStatus(accessory.thermostatService, deviceData);
} else {
that.log("No change", accessory.name);
}
}
});
});
}
// give this function all the parameters needed
function tccAccessory(log, name, deviceData, username, password, deviceID, debug) {
var uuid = UUIDGen.generate(name);
this.newAccessory = new Accessory(name, uuid);
// newAccessory.name = name;
this.log = log;
this.log("Adding TCC Device", name, deviceID);
this.name = name;
this.device = deviceData;
this.device.deviceLive = "false";
this.username = username;
this.password = password;
this.deviceID = deviceID;
this.debug = debug;
// return newAccessory;
}
tccAccessory.prototype = {
getName: function(callback) {
var that = this;
that.log("requesting name of", this.name);
callback(this.name);
},
getCurrentRelativeHumidity: function(callback) {
var that = this;
var currentRelativeHumidity = this.device.latestData.uiData.IndoorHumidity;
callback(null, Number(currentRelativeHumidity));
that.log("Current relative humidity of " + this.name + " is " + currentRelativeHumidity + "%");
},
// This is showing what the HVAC unit is doing
getCurrentHeatingCoolingState: function(callback) {
var that = this;
// OFF = 0
// HEAT = 1
// COOL = 2
// EquipmentOutputStatus is 1 when HVAC is running in heat mode, and 2
// when running in cool mode
var CurrentHeatingCoolingState = this.device.latestData.uiData.EquipmentOutputStatus;
that.log("getCurrentHeatingCoolingState is", CurrentHeatingCoolingState, this.name);
if (CurrentHeatingCoolingState > 2)
// Maximum value is 2
CurrentHeatingCoolingState = 2;
// if (this.newAccessory.reachable) {
callback(null, Number(CurrentHeatingCoolingState));
// } else {
// that.log("getCurrentHeatingCoolingState: Device not reachable");
// callback(new Error("Device not reachable"));
// }
},
// This is to change the system switch to a different position
setTargetHeatingCooling: function(value, callback) {
var that = this;
if (!updating) {
updating = true;
that.log("Setting system switch for", this.name, "to", value);
// TODO:
// verify that the task did succeed
tcc.login(this.username, this.password).then(function(session) {
session.setSystemSwitch(that.deviceID, tcc.toTCCHeadingCoolingSystem(value)).then(function(taskId) {
that.log("Successfully changed system!");
that.log(taskId);
// Update all information
// TODO: call periodicUpdate to refresh all data elements
updateValues(that);
callback(null, Number(1));
});
}).fail(function(err) {
that.log('tcc Failed:', err);
callback(null, Number(0));
});
callback(null, Number(0));
updating = false
}
},
// This is to read the system switch
getTargetHeatingCooling: function(callback) {
var that = this;
// Homekit allowed values
// OFF = 0
// HEAT = 1
// COOL = 2
// AUTO = 3
var TargetHeatingCooling = tcc.toHomeBridgeHeatingCoolingSystem(this.device.latestData.uiData.SystemSwitchPosition);
this.log("getTargetHeatingCooling is ", TargetHeatingCooling, this.name);
callback(null, Number(TargetHeatingCooling));
},
getCurrentTemperature: function(callback) {
var that = this;
var currentTemperature = tcc.toHBTemperature(this, this.device.latestData.uiData.DispTemperature);
that.log("Current temperature of " + this.name + " is " + currentTemperature + "°");
callback(null, Number(currentTemperature));
},
setTargetTemperature: function(value, callback) {
var that = this;
if (!updating) {
updating = true;
// maxValue: 38,
// minValue: 10,
that.log("Setting target temperature for", this.name, "to", value + "°");
if (value < 10)
value = 10;
if (value > 38)
value = 38;
value = tcc.toTCCTemperature(that, value);
// TODO:
// verify that the task did succeed
// tcc.login(this.username, this.password).then(function(session) {
var heatSetPoint, coolSetPoint = null;
switch (tcc.toHomeBridgeHeatingCoolingSystem(that.device.latestData.uiData.SystemSwitchPosition)) {
case 0:
break;
case 1:
heatSetPoint = value;
break;
case 2:
coolSetPoint = value;
break;
case 3:
if (value < that.device.latestData.uiData.HeatSetpoint)
heatSetPoint = value;
else if (value > that.device.latestData.uiData.CoolSetpoint)
coolSetPoint = value;
else if ((that.device.latestData.uiData.HeatSetpoint - value) < (value - that.device.latestData.uiData.CoolSetpoint))
coolSetPoint = value;
else
heatSetPoint = value;
break;
default:
break;
}
that.log("setHeatCoolSetpoint", that.name, that.device.latestData.uiData.StatusHeat, that.device.latestData.uiData.StatusCool);
session.setHeatCoolSetpoint(that.deviceID, heatSetPoint, coolSetPoint).then(function(taskId) {
that.log("Successfully changed temperature!", that.name, taskId);
if (taskId.success) {
that.log("Successfully changed temperature!", taskId);
callback();
} else {
that.log("Error: Unsuccessfully changed temperature!", that.name, taskId);
callback(new Error("Error: setHeatCoolSetpoint"));
}
updateValues(that); // refresh
}.bind(this)).fail(function(err) {
that.log('Error: setHeatCoolSetpoint', that.name, err);
callback(err);
});
updating = false;
}
},
getTargetTemperature: function(callback) {
var that = this;
// maxValue: 38,
// minValue: 10,
// Homebridge expects temperatures in C, but Honeywell will return F if configured.
if (this.model = "EMEA_ZONE") {
switch (tcc.toHomeBridgeHeatingCoolingSystem(that.device.latestData.uiData.SystemSwitchPosition)) {
case Characteristic.TargetHeatingCoolingState.OFF:
// Not sure what to do here, so will display current temperature
var targetTemperature = tcc.toHBTemperature(that, this.device.latestData.uiData.DispTemperature);
break;
case Characteristic.TargetHeatingCoolingState.HEAT:
var targetTemperature = tcc.toHBTemperature(that, this.device.latestData.uiData.HeatSetpoint);
break;
case Characteristic.TargetHeatingCoolingState.COOL:
var targetTemperature = tcc.toHBTemperature(that, this.device.latestData.uiData.CoolSetpoint);
break;
case Characteristic.TargetHeatingCoolingState.AUTO:
// Not sure what to do here, so will display current temperature
var targetTemperature = tcc.toHBTemperature(that, this.device.latestData.uiData.DispTemperature);
break;
default:
// Not sure what to do here, so will display current temperature
var targetTemperature = tcc.toHBTemperature(that, this.device.latestData.uiData.DispTemperature);
break
}
// that.log("Device type is: " + this.model + ". Target temperature should be there.");
that.log("Target temperature for", this.name, "is", targetTemperature + "°");
} else {
var targetTemperature = 0;
that.log("Device type is: " + this.model + ". Target temperature is probably NOT there (this is normal).");
that.log("Will set target temperature for", this.name, "to " + targetTemperature + "°");
}
if (targetTemperature < 10)
targetTemperature = 10;
if (targetTemperature > 38)
targetTemperature = 38;
callback(null, Number(targetTemperature));
},
getTemperatureDisplayUnits: function(callback) {
var that = this;
var temperatureUnits = 0;
that.log("getTemperatureDisplayUnits", this.name);
switch (this.device.latestData.uiData.DisplayUnits) {
case "F":
that.log("Temperature unit for", this.name, "is set to", this.device.latestData.uiData.DisplayUnits);
temperatureUnits = 1;
break;
case "C":
that.log("Temperature unit for", this.name, "is set to", this.device.latestData.uiData.DisplayUnits);
temperatureUnits = 0;
break;
default:
temperatureUnits = 0;
}
callback(null, Number(temperatureUnits));
},
getCoolingThresholdTemperature: function(callback) {
var that = this;
var coolingthresholdTemperature = tcc.toHBTemperature(this, this.device.latestData.uiData.CoolSetpoint);
that.log("Cool Setpoint temperature of " + this.name + " is " + coolingthresholdTemperature + "°");
callback(null, Number(coolingthresholdTemperature));
},
setCoolingThresholdTemperature: function(value, callback) {
var that = this;
if (!updating) {
updating = true;
// maxValue: 38,
// minValue: 10,
that.log("Setting cooling threshold temperature for", this.name, "to", value + "°");
if (value < 10)
value = 10;
if (value > 38)
value = 38;
value = tcc.toTCCTemperature(that, value);
// TODO:
// verify that the task did succeed
tcc.login(this.username, this.password).then(function(session) {
session.setHeatCoolSetpoint(that.deviceID, null, value).then(function(taskId) {
that.log("Successfully changed cooling threshold!");
that.log(taskId);
// returns taskId if successful
// nothing else here...
updateValues(that);
callback(null, Number(1));
});
}).fail(function(err) {
that.log('tcc Failed:', err);
callback(null, Number(0));
});
callback(null, Number(0));
updating = false;
}
},
getHeatingThresholdTemperature: function(callback) {
var that = this;
var heatingthresholdTemperature = tcc.toHBTemperature(this, this.device.latestData.uiData.HeatSetpoint);
that.log("Heat Setpoint temperature of " + this.name + " is " + heatingthresholdTemperature + "°");
callback(null, Number(heatingthresholdTemperature));
},
setHeatingThresholdTemperature: function(value, callback) {
var that = this;
if (!updating) {
updating = true;
// maxValue: 38,
// minValue: 10,
that.log("Setting heating threshold temperature for", this.name, "to", value + "°");
if (value < 10)
value = 10;
if (value > 38)
value = 38;
value = tcc.toTCCTemperature(that, value);
// TODO:
// verify that the task did succeed
tcc.login(this.username, this.password).then(function(session) {
session.setHeatCoolSetpoint(that.deviceID, value, null).then(function(taskId) {
that.log("Successfully changed heating threshold!");
that.log(taskId);
// returns taskId if successful
// nothing else here...
updateValues(that);
callback(null, Number(1));
});
}).fail(function(err) {
that.log('tcc Failed:', err);
callback(null, Number(0));
});
callback(null, Number(0));
updating = false;
}
},
setTemperatureDisplayUnits: function(value, callback) {
var that = this;
that.log("set temperature units to", value);
callback();
},
getServices: function() {
var that = this;
that.log("getServices", this.name);
// Information Service
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Honeywell")
.setCharacteristic(Characteristic.SerialNumber, this.deviceID); // need to stringify the this.serial
// Thermostat Service
this.thermostatService = new Service.Thermostat(this.name);
// Required Characteristics /////////////////////////////////////////////////////////////
// this.addCharacteristic(Characteristic.CurrentHeatingCoolingState); READ
this.thermostatService
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentHeatingCoolingState.bind(this));
// this.addCharacteristic(Characteristic.TargetHeatingCoolingState); READ WRITE
if (this.device.latestData.uiData.SwitchAutoAllowed) {
this.thermostatService
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetHeatingCooling.bind(this))
.on('set', this.setTargetHeatingCooling.bind(this));
} else {
// don't display Auto if it isn't supported
this.thermostatService
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.setProps({
validValues: [0, 1, 2]
})
.on('get', this.getTargetHeatingCooling.bind(this))
.on('set', this.setTargetHeatingCooling.bind(this));
}
// this.addCharacteristic(Characteristic.CurrentTemperature); READ
this.thermostatService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100
})
.on('get', this.getCurrentTemperature.bind(this));
// this.addCharacteristic(Characteristic.TargetTemperature); READ WRITE
this.thermostatService
.getCharacteristic(Characteristic.TargetTemperature)
.setProps({
minStep: 0.5
})
.on('get', this.getTargetTemperature.bind(this))
.on('set', this.setTargetTemperature.bind(this));
// this.addCharacteristic(Characteristic.TemperatureDisplayUnits); READ WRITE
this.thermostatService
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', this.getTemperatureDisplayUnits.bind(this));
// Optional Characteristics /////////////////////////////////////////////////////////////
// this.addOptionalCharacteristic(Characteristic.CurrentRelativeHumidity);
if (this.device.latestData.uiData.IndoorHumiditySensorAvailable && this.device.latestData.uiData.IndoorHumiditySensorNotFault) {
this.thermostatService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getCurrentRelativeHumidity.bind(this));
}
// this.addOptionalCharacteristic(Characteristic.TargetRelativeHumidity);
// this.addOptionalCharacteristic(Characteristic.CoolingThresholdTemperature);
if (this.device.latestData.uiData.SwitchAutoAllowed) {
// Only available on models with an Auto Mode
this.thermostatService
.getCharacteristic(Characteristic.CoolingThresholdTemperature)
.on('get', this.getCoolingThresholdTemperature.bind(this))
.on('set', this.setCoolingThresholdTemperature.bind(this))
// this.addOptionalCharacteristic(Characteristic.HeatingThresholdTemperature);
this.thermostatService
.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.on('get', this.getHeatingThresholdTemperature.bind(this))
.on('set', this.setHeatingThresholdTemperature.bind(this));
}
// this.addOptionalCharacteristic(Characteristic.Name);
this.thermostatService
.getCharacteristic(Characteristic.Name)
.on('get', this.getName.bind(this));
return [informationService, this.thermostatService];
}
}