forked from bdraco/homebridge-nexia-thermostat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
547 lines (493 loc) · 20.6 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
var Service, Characteristic;
var rp = require('request-promise');
var debounce = require('lodash.debounce');
var Promise = require('bluebird');
var util = require('util');
//require('request-debug')(rp);
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-nexia-thermostat", "NexiaThermostat", NexiaThermostat);
};
function NexiaThermostat(log, config) {
this.log = log;
this.name = config.name;
this.apiroute = config.apiroute;
this.houseId = config.houseId;
this.zone = config.zone || 0;
this.thermostatId = config.thermostatId;
this.xMobileId = config.xMobileId;
this.xApiKey = config.xApiKey;
this.manufacturer = config.manufacturer;
this.model = config.model;
this.serialNumber = config.serialNumber;
this.service = new Service.Thermostat(this.name);
this.blockRefresh = 0;
this.TStatData = {};
this._currentData = {};
}
NexiaThermostat.prototype = {
//Start
identify: function(callback) {
this.log("Identify requested!");
callback(null);
},
// Required
getCurrentHeatingCoolingState: function(callback) {
this.log("getCurrentHeatingCoolingState");
if (!this._currentData) {
callback("getCurrentHeatingCoolingState: data not yet loaded");
}
var thisTStat = this._findTStatInNexiaResponse();
var characteristic = this._findCurrentState(thisTStat);
this.log("current State: " + characteristic);
return callback(null, characteristic);
},
getTargetHeatingCoolingState: function(callback) {
this.log("getTargetHeatingCoolingState");
if (!this._currentData) {
callback("getCurrentHeatingCoolingState: data not yet loaded");
}
var thisTStat = this._findTStatInNexiaResponse();
var characteristic = this._findTargetState(thisTStat);
var minTemperature = 12.77;
var maxTemperature = 37.22;
if (characteristic == Characteristic.TargetHeatingCoolingState.HEAT) {
minTemperature = 12.77;
maxTemperature = 32.22;
} else if (characteristic == Characteristic.TargetHeatingCoolingState.COOL) {
minTemperature = 15.55;
maxTemperature = 37.22;
}
this.service
.getCharacteristic(Characteristic.TargetTemperature)
.setProps({
minValue: minTemperature,
maxValue: maxTemperature
});
return callback(null, characteristic);
},
setTargetHeatingCoolingState: function(value, callback) {
this.log("setTargetHeatingCoolingState");
if (!this._currentData) {
callback("setTargetHeatingCoolingState: data not yet loaded");
}
var thisTStat = this._findTStatInNexiaResponse();
return this._setHVACMode(thisTStat, value, callback);
},
getCurrentTemperature: function(callback) {
this.log("getCurrentTemperature");
if (!this._currentData) {
callback("getCurrentHeatingCoolingState: data not yet loaded");
}
var thisTStat = this._findTStatInNexiaResponse();
var f = this._findCurrentTemp(thisTStat);
var c = (f - 32.0) / 1.8;
this.log("Zone current template is: %f F", f);
this.log("Zone current template is: %f C", c);
callback(null, c);
},
getTargetTemperature: function(callback) {
this.log("getTargetTemperature");
if (!this._currentData) {
callback("getCurrentHeatingCoolingState: data not yet loaded");
}
var thisTStat = this._findTStatInNexiaResponse();
var f = this._findCurrentSetPoint(thisTStat);
var c = (f - 32.0) / 1.8;
this.log("Zone target template is: %f F", f);
this.log("Zone target template is: %f C", c);
callback(null, c);
},
setTargetTemperature: function(value, callback) {
this.log("setTargetTemperature");
if (!this._currentData) {
callback("setTargetTemperature: data not yet loaded");
}
// TODO: We need to debounce this so there is a 3s delay before calling
// this in case they are sliding
this.log("setTargetTemperature: target=[%f]", value);
var thisTStat = this._findTStatInNexiaResponse();
return this._setTemp(thisTStat, value, callback);
},
getTemperatureDisplayUnits: function(callback) {
this.log("getTemperatureDisplayUnits");
var error = null;
callback(null, Characteristic.TemperatureDisplayUnits.FAHRENHEIT);
},
setTemperatureDisplayUnits: function(value, callback) {
this.log("setTemperatureDisplayUnits");
callback(null);
},
getName: function(callback) {
this.log("getName :", this.name);
if (!this._currentData) {
callback("getName: data not yet loaded");
}
var thisTStat = this._findTStatInNexiaResponse();
this.name = thisTStat.name;
callback(null, this.name);
},
getServices: function() {
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber);
// Required Characteristics
this.service
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentHeatingCoolingState.bind(this));
this.service
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetHeatingCoolingState.bind(this))
.on('set', this.setTargetHeatingCoolingState.bind(this));
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getCurrentTemperature.bind(this));
this.service
.getCharacteristic(Characteristic.TargetTemperature)
.on('get', this.getTargetTemperature.bind(this))
.on('set', this.setTargetTemperature.bind(this));
this.service
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', this.getTemperatureDisplayUnits.bind(this))
.on('set', this.setTemperatureDisplayUnits.bind(this));
this.service
.getCharacteristic(Characteristic.Name)
.on('get', this.getName.bind(this));
this._refreshData();
setInterval(this._refreshData.bind(this), 90 * 1000);
return [informationService, this.service];
},
_refreshData: function() {
if (this.blockRefresh) {
this.log("refresh is blocked");
return;
}
this._get("houses/" + this.houseId).promise().bind(this)
.then(function(body) {
this.log("Refreshed Data!");
var parse = JSON.parse(body);
if (parse.error) {
this.log("There was an error fetching data: " + parse.error);
return;
}
this._currentData = parse;
this._updateData();
return;
}).catch(function(err) {
this.log("Error from get: %j", err);
});
},
_get: function(url) {
return rp({
uri: this._calculateUrl(url),
headers: {
'X-MobileId': this.xMobileId,
'X-ApiKey': this.xApiKey
}
})
},
_post: function(url, body) {
return rp({
method: 'POST',
uri: this._calculateUrl(url),
headers: {
'X-MobileId': this.xMobileId,
'X-ApiKey': this.xApiKey
},
body: body,
json: true
})
},
_put: function(url, body) {
return rp({
method: 'PUT',
uri: this._calculateUrl(url),
headers: {
'X-MobileId': this.xMobileId,
'X-ApiKey': this.xApiKey
},
body: body,
json: true
})
},
_calculateUrl: function(url) {
return (url.indexOf('http://') == 0 || url.indexOf('https://') == 0) ? url : (this.apiroute + url);
},
_setTemp: function(thisTStat, value, callback) {
this.log("We are setting temp though _setTempDebounced to: %f" , value);
this.blockRefresh = 1;
this._setTempDebounced(thisTStat, value, function() {
this.log('Temperature set to %f: ' , value);
}.bind(this));
return Promise.resolve().asCallback(callback);
},
_setTempDebounced: debounce(function(thisTStat, value, callback) {
this.log("_setTempDebounced entered with value:" + value);
var f = Math.round(value * 1.8 + 32.0);
// should search settings for hvac_mode and not just
// assume settings[0]
// *** url may be undef here because the stat has gone from Off to Cool/Heat
// and we haven't updated the data yet so we don't have the url to set it
var key_name;
var url;
if (thisTStat.hasOwnProperty("zones")) {
this.log("this zone actions: %j", thisTStat.zones[this.zone].features[0].actions);
key_name = Object.keys(thisTStat.zones[this.zone].features[0].actions)[0];
url = thisTStat.zones[this.zone].features[0].actions[key_name].href;
} else if (thisTStat.hasOwnProperty("features")) {
key_name = Object.keys(thisTStat.features[0].actions)[0];
url = thisTStat.features[0].actions[key_name].href;
} else {
this.log("zones and features missing: %j", thisTStat);
}
var targetState = this._findTargetState(thisTStat);
this.log("_findTargetState: " + targetState);
var json_struct;
switch (targetState) {
case Characteristic.TargetHeatingCoolingState.AUTO:
if (f <= this._findCurrentTemp(thisTStat)) {
json_struct = {
"heat": f + 3,
"cool": f
};
} else {
json_struct = {
"heat": f,
"cool": f - 3
};
}
break;
case Characteristic.TargetHeatingCoolingState.HEAT:
json_struct = {
"heat": f
};
break;
default:
json_struct = {
"cool": f
};
}
this.log("JSON: %j", json_struct);
return this._post(url, json_struct).promise().bind(this)
.then(function(body) {
this.log("Set Temp!");
//this.log(body);
if (callback) {
callback(null, value);
}
//this.service.getCharacteristic(Characteristic.TargetTemperature).setValue(f);
// TODO -- the body may be able to reused for refreshData to avoid hitting
// the server again
setTimeout(function () {
this.blockRefresh = 0;
this._refreshData();
}.bind(this), 5 * 1000);
}).catch(function(err) {
this.blockRefresh = 0;
this.log("Error from _post to %s: %j", url, err);
});
}, 5000),
_setHVACMode: function(thisTStat, value, callback) {
// should search settings for hvac_mode and not just
// assume settings[0]
this.blockRefresh = 1;
var f = this._findCurrentSetPoint(thisTStat);
var c = (f - 32.0) / 1.8;
var configRoot;
if (thisTStat.hasOwnProperty("zones")) {
configRoot = thisTStat.zones[this.zone];
} else if (thisTStat.hasOwnProperty("settings")) {
// should search settings for hvac_mode and not just
// assume settings[0]
configRoot = thisTStat.settings[0];
}
this.log("Finding zone mode");
var zonemode = 1;
for(var i = 0;i < configRoot.settings.length;i++) {
this.log("setting [%d] = %j", i, configRoot.settings[i]);
if (configRoot.settings[i].type == "zone_mode" || configRoot.settings[i].type == "hvac_mode") {
zonemode = i;
break;
}
}
var url = configRoot.settings[zonemode]._links.self.href;
var txt_value = this.ConfigKeyForheatingCoolingState(value);
var json_struct = {
"value": txt_value
};
//this.log("JSON:" + json_struct);
return this._post(url, json_struct).promise().bind(this)
.then(function(body) {
callback(null, value);
//this.log("Set State!");
//this.log(body);
// Since data is out of sync (HVAC state is wrong the set temp will fail)
// If we can use the body of the response to update the current Data
// this will fix it
return this._setTemp(thisTStat, c);
}).catch(function(err) {
this.blockRefresh = 0;
this.log("Error from _post to %s: %j", url, err);
});
},
_findTStatInNexiaResponse: function() {
var data = this._currentData;
var all_items = data.result._links.child[0].data.items;
var want_tStatId = this.thermostatId;
var tStatId = -1;
for (var index = 0; index < all_items.length; index++) {
if (all_items[index].type.indexOf('thermostat') > -1) {
if (all_items[index].id === want_tStatId) {
// console.log(all_items[index]);
//
this.log("Found themostat id: %d and zone %d with name: %s", this.thermostatId, this.zone, this._findName(all_items[index]));
return all_items[index];
}
}
}
throw new Error("The tStatId is missing");
},
_findTargetState: function(thisTStat) {
var rawState = "unknown";
if (thisTStat.hasOwnProperty("zones")) {
rawState = thisTStat.zones[this.zone].current_zone_mode;
} else if (thisTStat.hasOwnProperty("settings")) {
// should search settings for hvac_mode and not just
// assume settings[0]
rawState = thisTStat.settings[0].current_value;
} else {
this.log("no state");
}
this.log("_findTargetState: %s", rawState);
return this.TargetHeatingCoolingStateForConfigKey(rawState);
},
_findName: function(thisTStat) {
var rawName = "unknown";
if (thisTStat.hasOwnProperty("zones")) {
rawName = thisTStat.zones[this.zone].name;
} else if (thisTStat.hasOwnProperty("features")) {
// should search settings for hvac_mode and not just
// assume settings[0]
rawName = thisTStat.features[this.zone].name;
this.log("_findName:" + rawName);
} else {
this.log("no state");
}
return rawName;
},
_findCurrentState: function(thisTStat) {
var rawState = "unknown";
if (thisTStat.hasOwnProperty("zones")) {
var currentTargetState = this._findTargetState(thisTStat);
rawState = thisTStat.zones[this.zone].operating_state;
this.log("zone operating_state: %s", rawState);
if (!rawState) {
this.log("zoneState missing: %s", rawState);
return this.CurrentHeatingCoolingStateForConfigKey("off");
} else if (rawState == "Damper Closed" || rawState == "Relieving Air") {
this.log("zoneState: %s - return off", rawState);
return this.CurrentHeatingCoolingStateForConfigKey("off");
}
this.log("zoneState: %s - return current TargetState: %d", rawState, currentTargetState);
return currentTargetState;
} else if (thisTStat.hasOwnProperty("features")) {
// should search settings for hvac_mode and not just
// assume settings[0]
rawState = thisTStat.features[this.zone].status;
this.log("_findCurrentState:" + rawState);
} else {
this.log("no state");
}
return this.CurrentHeatingCoolingStateForConfigKey(rawState);
},
_findCurrentSetPoint: function(thisTStat) {
var target_state = this._findTargetState(thisTStat);
if (thisTStat.hasOwnProperty("zones")) {
var this_zone = thisTStat.zones[this.zone];
var coolPoint = this_zone.setpoints.cool || this_zone.cooling_setpoint;
var heatPoint = this_zone.setpoints.heat || this_zone.heating_setpoint;
if (target_state === Characteristic.TargetHeatingCoolingState.COOL) {
return coolPoint;
} else if (target_state === Characteristic.TargetHeatingCoolingState.HEAT) {
return heatPoint;
} else if (target_state === Characteristic.TargetHeatingCoolingState.AUTO) {
if (coolPoint <= this._findCurrentTemp(thisTStat)) {
return coolPoint;
}
return heatPoint;
} else {
this.log("no current setpoint: %j", thisTStat.zones[this.zone]);
}
} else if (thisTStat.hasOwnProperty("features")) {
var features_node = thisTStat.features[this.zone];
if (target_state === Characteristic.TargetHeatingCoolingState.COOL && features_node.hasOwnProperty("setpoint_cool")) {
return features_node.setpoint_cool;
} else if (target_state === Characteristic.TargetHeatingCoolingState.HEAT && features_node.hasOwnProperty("setpoint_heat")) {
return features_node.setpoint_heat;
} else if (features_node.hasOwnProperty("setpoint_cool")) {
return features_node.setpoint_cool;
} else if (features_node.hasOwnProperty("setpoint_heat")) {
return features_node.setpoint_heat;
}
this.log("no current setpoint");
}
return 0; /* should error */
},
_findCurrentTemp: function(thisTStat) {
if (thisTStat.hasOwnProperty("zones")) {
return thisTStat.zones[this.zone].temperature;
} else if (thisTStat.hasOwnProperty("features")) {
return thisTStat.features[this.zone].temperature;
}
this.log("no state");
return 0; /* should error */
},
_updateData: function() {
this.service.getCharacteristic(Characteristic.CurrentTemperature).getValue();
this.service.getCharacteristic(Characteristic.TargetTemperature).getValue();
this.service.getCharacteristic(Characteristic.CurrentHeatingCoolingState).getValue();
this.service.getCharacteristic(Characteristic.TargetHeatingCoolingState).getValue();
return 1;
},
ConfigKeyForheatingCoolingState: function(state) {
switch (state) {
case Characteristic.TargetHeatingCoolingState.AUTO:
return "AUTO";
case Characteristic.TargetHeatingCoolingState.COOL:
return "cool";
case Characteristic.TargetHeatingCoolingState.HEAT:
return "heat";
default:
return "off";
}
},
TargetHeatingCoolingStateForConfigKey: function(configKey) {
switch (configKey.toLowerCase()) {
case 'auto':
return Characteristic.TargetHeatingCoolingState.AUTO;
case 'cool':
return Characteristic.TargetHeatingCoolingState.COOL;
case 'heat':
return Characteristic.TargetHeatingCoolingState.HEAT;
default:
return Characteristic.TargetHeatingCoolingState.OFF;
}
},
CurrentHeatingCoolingStateForConfigKey: function(configKey) {
switch (configKey.toLowerCase()) {
case 'auto':
return Characteristic.CurrentHeatingCoolingState.AUTO;
case 'cool':
return Characteristic.CurrentHeatingCoolingState.COOL;
case 'heat':
return Characteristic.CurrentHeatingCoolingState.HEAT;
default:
return Characteristic.CurrentHeatingCoolingState.OFF;
}
}
};