forked from luc-ass/homebridge-evohome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
337 lines (260 loc) · 11.7 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
// This platform integrates Honeywell Evohome into homebridge
// As I only own a few thermostats and no window sensors I have not yet integrated them.
//
// The configuration is stored inside the ../config.json
// {
// "platform": "Evohome",
// "name" : "Evohome",
// "username" : "username/email",
// "password" : "password",
// "appId" : "91db1612-73fd-4500-91b2-e63b069b185c"
// }
//
// not yet sure wether application-id-hex (appID) is really changing so I put it here as default.
'use strict';
var evohome = require('./lib/evohome.js');
var Service, Characteristic;
var config;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-evohome", "Evohome", EvohomePlatform);
}
function EvohomePlatform(log, config){
this.username = config['username'];
this.password = config['password'];
this.appId = config['appId'] || "91db1612-73fd-4500-91b2-e63b069b185c";
this.cache_timeout = 890; // seconds
this.log = log;
this.updating = false;
}
EvohomePlatform.prototype = {
accessories: function(callback) {
this.log("Logging into Evohome...");
var that = this;
// create the myAccessories array
this.myAccessories = [];
evohome.login(that.username, that.password, that.appId).then(function(session) {
this.log("Logged into Evohome!");
session.getLocations().then(function(locations){
this.log('You have', locations.length, 'location(s). Only the first one will be used!');
this.log('You have', locations[0].devices.length, 'device(s).')
// iterate through the devices
for (var deviceId in locations[0].devices) {
// print name of the device
this.log(deviceId + ": " + locations[0].devices[deviceId].name + " (" + locations[0].devices[deviceId].thermostat.indoorTemperature + "°)");
// store device in var
var device = locations[0].devices[deviceId];
// store name of device
var name = locations[0].devices[deviceId].name + " Thermostat";
// create accessory (only if it is "EMEA_ZONE")
if (device.thermostatModelType = "EMEA_ZONE") {
var accessory = new EvohomeThermostatAccessory(that.log, name, device, deviceId, this.username, this.password, this.appId);
// store accessory in myAccessories
this.myAccessories.push(accessory);
}
}
callback(this.myAccessories);
setInterval(that.periodicUpdate.bind(this), this.cache_timeout * 1000);
}.bind(this)).fail(function(err){
that.log('Evohome Failed:', err);
});
}.bind(this)).fail(function(err) {
// tell me if login did not work!
that.log("Error during Login:", err);
});
}
};
EvohomePlatform.prototype.periodicUpdate = function(session,myAccessories) {
if(!this.updating && this.myAccessories){
this.updating = true;
evohome.login(this.username, this.password, this.appId).then(function(session) {
session.getLocations().then(function(locations){
for(var i=0; i<this.myAccessories.length; ++i) {
var device = locations[0].devices[this.myAccessories[i].deviceId];
if(device) {
// Check if temp has changed
var oldCurrentTemp = this.myAccessories[i].device.thermostat.indoorTemperature;
var newCurrentTemp = device.thermostat.indoorTemperature;
var service = this.myAccessories[i].thermostatService;
if(oldCurrentTemp!=newCurrentTemp && service) {
this.log("Updating: " + device.name + " currentTempChange from: " + oldCurrentTemp + " to: " + newCurrentTemp);
var charCT = service.getCharacteristic(Characteristic.CurrentTemperature);
if(charCT) charCT.setValue(newCurrentTemp);
else this.log("No Characteristic.CurrentTemperature found " + service);
}
var oldTargetTemp = this.myAccessories[i].device.thermostat.changeableValues.heatSetpoint['value'];
var newTargetTemp = device.thermostat.changeableValues.heatSetpoint['value'];
if(oldTargetTemp!=newTargetTemp && service) {
this.log("Updating: " + device.name + " targetTempChange from: " + oldTargetTemp + " to: " + newTargetTemp);
var charTT = service.getCharacteristic(Characteristic.TargetTemperature);
if(charTT) charCT.setValue(newTargetTemp);
else this.log("No Characteristic.TargetTemperature found " + service);
}
this.myAccessories[i].device = device;
}
}
}.bind(this)).fail(function(err){
this.log('Evohome Failed:', err);
});
}.bind(this)).fail(function(err){
this.log('Evohome Failed:', err);
});
this.updating = false;
}
}
// give this function all the parameters needed
function EvohomeThermostatAccessory(log, name, device, deviceId, username, password, appId) {
this.name = name;
this.device = device;
this.model = device.thermostatModelType;
this.serial = device.deviceID;
this.deviceId = deviceId;
this.username = username;
this.password = password;
this.appId = appId;
this.log = log;
}
EvohomeThermostatAccessory.prototype = {
getCurrentTemperature: function(callback) {
var that = this;
// need to refresh data if outdated!!
var currentTemperature = this.device.thermostat.indoorTemperature;
callback(null, Number(currentTemperature));
that.log("Current temperature of " + this.name + " is " + currentTemperature + "°");
},
getCurrentHeatingCoolingState: function(callback) {
var that = this;
that.log("getCurrentHeatingCooling");
// TODO:
// fixed until it can be requested from Evohome...
// OFF = 0
// HEAT = 1
// COOL = 2
// AUTO = 3
callback(null, Number(1));
},
getName: function(callback) {
var that = this;
that.log("requesting name of", this.name);
callback(this.name);
},
setTargetHeatingCooling: function(value, callback) {
var that = this;
// not implemented
that.log("attempted to change targetHeatingCooling: " + value +" - not yet implemented");
callback();
},
getTargetHeatingCooling: function(callback) {
var that = this;
// TODO:
// fixed until it can be requested from Evohome...
// OFF = 0
// HEAT = 1
// COOL = 2
// AUTO = 3
callback(null, Number(1));
},
setTargetTemperature: function(value, callback) {
var that = this;
that.log("Setting target temperature for", this.name, "to", value + "°");
var minutes = 10; // The number of minutes the new target temperature will be effective
// TODO:
// verify that the task did succeed
evohome.login(this.username, this.password, this.appId).then(function (session) {
session.setHeatSetpoint(that.serial, value, minutes).then(function (taskId) {
that.log("Successfully changed temperature!");
that.log(taskId);
// returns taskId if successful
// nothing else here...
callback(null, Number(1));
});
}).fail(function (err) {
that.log('Evohome Failed:', err);
callback(null, Number(0));
});
callback(null, Number(0));
},
getTargetTemperature: function(callback) {
var that = this;
// gives back the target temperature of thermostat
// crashes the plugin IF there is no value defined (like
// with DOMESTIC_HOT_WATER) so we need to chek if it
// is defined first
if (this.model = "EMEA_ZONE"){
var targetTemperature = this.device.thermostat.changeableValues.heatSetpoint['value'];
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 + "°");
}
callback(null, Number(targetTemperature));
},
getTemperatureDisplayUnits: function(callback) {
var that = this;
var temperatureUnits = 0;
switch(this.device.thermostat.units) {
case "Fahrenheit":
that.log("Temperature unit for", this.name, "is set to", this.device.thermostat.units);
temperatureUnits = 1;
break;
case "Celsius":
that.log("Temperature unit for", this.name, "is set to", this.device.thermostat.units);
temperatureUnits = 0;
break;
default:
temperatureUnits = 0;
}
callback(null, Number(temperatureUnits));
},
setTemperatureDisplayUnits: function(value, callback) {
var that = this;
that.log("set temperature units to", value);
callback();
},
getServices: function() {
var that = this;
// Information Service
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Identify, this.name)
.setCharacteristic(Characteristic.Manufacturer, "Honeywell")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.SerialNumber, "123456"); // need to stringify the this.serial
// Thermostat Service
this.thermostatService = new Service.Thermostat("Honeywell Thermostat");
// Required Characteristics /////////////////////////////////////////////////////////////
// this.addCharacteristic(Characteristic.CurrentHeatingCoolingState); READ
this.thermostatService
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentHeatingCoolingState.bind(this));
// this.addCharacteristic(Characteristic.TargetHeatingCoolingState); READ WRITE
this.thermostatService
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetHeatingCooling.bind(this))
.on('set', this.setTargetHeatingCooling.bind(this));
// this.addCharacteristic(Characteristic.CurrentTemperature); READ
this.thermostatService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getCurrentTemperature.bind(this));
// this.addCharacteristic(Characteristic.TargetTemperature); READ WRITE
this.thermostatService
.getCharacteristic(Characteristic.TargetTemperature)
.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);
// this.addOptionalCharacteristic(Characteristic.TargetRelativeHumidity);
// this.addOptionalCharacteristic(Characteristic.CoolingThresholdTemperature);
// this.addOptionalCharacteristic(Characteristic.HeatingThresholdTemperature);
// this.addOptionalCharacteristic(Characteristic.Name);
return [informationService, this.thermostatService];
}
}