forked from hacctarr/homebridge-tcc-fan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
397 lines (336 loc) · 15 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
// This platform integrates Honeywell TCC's Fan 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": "Fan",
// "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-fan", "tcc-fan", 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'];
this.showFanControl = config['showFanControl'] !== false;
this.showAuto = config['showAuto'] || false;
this.showOn = config['showOn'] || false;
this.showCirculate = config['showCirculate'] || false;
this.showFollowSchedule = config['showFollowSchedule'] || false;
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,
that.showFanControl, that.showAuto, that.showOn, that.showCirculate, that.showFollowSchedule);
// 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(that, service, data) {
// var that = this;
// if (that.device.latestData.hasFan && that.device.latestData.fanData && that.device.latestData.fanData.fanModeOnAllowed) {
if (data.hasFan && data.fanData && data.fanData.fanModeOnAllowed) {
service.getCharacteristic(Characteristic.On).getValue();
}
}
tccPlatform.prototype.periodicUpdate = function(t) {
if (this.debug) this.log("periodicUpdate");
var t = updateValues(this);
}
function updateValues(that) {
if (that.debug) 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);
// TODO replace for 1.x 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) {
if (that.debug) that.log("updateValues: Device reachable", accessory.name);
// TODO replace for 1.x accessory.newAccessory.updateReachability(true);
} else {
if (that.debug) that.log("updateValues: Device not reachable", accessory.name);
// TODO replace for 1.x accessory.newAccessory.updateReachability(false);
}
if (accessory.fanService && !tcc.deepEquals(deviceData, accessory.device)) {
if (that.debug) that.log("Change", accessory.name, tcc.diff(accessory.device, deviceData));
accessory.device = deviceData;
updateStatus(that, accessory.fanService, deviceData);
} else {
if (that.debug) that.log("No change", accessory.name);
}
}
});
});
}
// give this function all the parameters needed
function tccAccessory(log, name, deviceData, username, password, deviceID, debug, showFanControl, showAuto, showOn, showCirculate, showFollowSchedule) {
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;
this.showFanControl = showFanControl;
this.showAuto = showAuto;
this.showOn = showOn;
this.showCirculate = showCirculate;
this.showFollowSchedule = showFollowSchedule;
// return newAccessory;
}
tccAccessory.prototype = {
getName: function(callback) {
var that = this;
that.log("requesting name of", this.name);
callback(this.name);
},
setState: function(value, callback) {
var that = this;
if (!updating) {
updating = true;
that.log("Setting fan switch for", this.name, "to", value);
// TODO:
// verify that the task did succeed
tcc.login(this.username, this.password).then(function(session) {
session.setFanSwitch(that.deviceID, 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
}
},
getState: function(callback) {
var that = this;
// Homekit allowed values
// Characteristic.TargetFanState.MANUAL = 0;
// Characteristic.TargetFanState.AUTO = 1;
var TargetFanState = tcc.toHomeBridgeFanSystem(this.device.latestData.fanData.fanMode);
this.log("getTargetFanState is ", TargetFanState, this.name);
callback(null, Boolean(TargetFanState));
},
getServices: function() {
var that = this;
that.log("getServices", this.name);
// Information Service
const 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, this.deviceID); // need to stringify the this.serial
const returnServices = [];
// Fan Service
if (this.showFanControl) {
this.log("Creating fan service...");
this.fanService = new Service.Fan(this.name);
this.fanService
.getCharacteristic(Characteristic.Name)
.on('get', this.getName.bind(this));
this.log("Fan service created!")
}
if (this.device.latestData.hasFan && this.device.latestData.fanData && this.device.latestData.fanData.fanModeOnAllowed) {
if (this.showFanControl) {
this.log("Updating fan service...");
this.fanService
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
this.log("Fan service updated.")
}
if (this.showAuto) {
this.log("Creating switch for \'Auto\'...");
this.myMomemtarySwitchAuto = new Service.Switch("Auto", "mode-auto");
this.myMomemtarySwitchAuto
.getCharacteristic(Characteristic.On)
.on("get", (callback) => {
callback(false)
})
.on("set", (s, callback) => {
if (s === true) {
if (myAccessories && myAccessories[0]) {
myAccessories[0].setState(0, callback);
}
// reset switch to off
setTimeout(function() {
this.myMomemtarySwitchAuto.setCharacteristic(Characteristic.On, false);
}.bind(this), 500);
} else {
callback(null, s);
}
});
this.log("\'Auto\' switch created!");
}
if (this.showOn) {
this.log("Creating switch for \'On\'...");
this.myMomemtarySwitchOn = new Service.Switch("On", "mode-on");
this.myMomemtarySwitchOn
.getCharacteristic(Characteristic.On)
.on("get", (callback) => {
callback(false)
})
.on("set", (s, callback) => {
if (s === true) {
if (myAccessories && myAccessories[0]) {
myAccessories[0].setState(1, callback);
}
// reset switch to off
setTimeout(function() {
this.myMomemtarySwitchOn.setCharacteristic(Characteristic.On, false);
}.bind(this), 500);
} else {
callback(null, s);
}
});
this.log("\'On\' switch created!");
}
if (this.showCirculate) {
this.log("Creating switch for \'Circulate\'...");
this.myMomemtarySwitchCirculate = new Service.Switch("Circulate", "mode-circulate");
this.myMomemtarySwitchCirculate
.getCharacteristic(Characteristic.On)
.on("get", (callback) => {
callback(false)
})
.on("set", (s, callback) => {
if (s === true) {
if (myAccessories && myAccessories[0]) {
myAccessories[0].setState(2, callback);
}
// reset switch to off
setTimeout(function() {
this.myMomemtarySwitchCirculate.setCharacteristic(Characteristic.On, false);
}.bind(this), 500);
} else {
callback(null, s);
}
});
this.log("\'Circulate\' switch created!");
}
if (this.showFollowSchedule) {
this.log("Creating switch for \'Follow Schedule\'...")
this.myMomemtarySwitchFollowSchedule = new Service.Switch("Follow Schedule", "mode-follow-schedule");
this.myMomemtarySwitchFollowSchedule
.getCharacteristic(Characteristic.On)
.on("get", (callback) => {
callback(false)
})
.on("set", (s, callback) => {
if (s === true) {
if (myAccessories && myAccessories[0]) {
myAccessories[0].setState(3, callback);
}
// reset switch to off
setTimeout(function() {
this.myMomemtarySwitchFollowSchedule.setCharacteristic(Characteristic.On, false);
}.bind(this), 500);
} else {
callback(null, s);
}
});
this.log("\'Follow Schedule\' switch created!");
}
}
returnServices.push(informationService);
if (this.showFanControl) {
returnServices.push(this.fanService);
}
if (this.showOn && this.myMomemtarySwitchOn) {
returnServices.push(this.myMomemtarySwitchOn);
}
if (this.showAuto && this.myMomemtarySwitchAuto) {
returnServices.push(this.myMomemtarySwitchAuto);
}
if (this.showCirculate && this.myMomemtarySwitchCirculate) {
returnServices.push(this.myMomemtarySwitchCirculate);
}
if (this.showFollowSchedule && this.myMomemtarySwitchFollowSchedule) {
returnServices.push(this.myMomemtarySwitchFollowSchedule);
}
return returnServices;
}
}