-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·597 lines (512 loc) · 24.4 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
// LightWaveRF Platform Shim for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "LightWaveRF",
// "name": "LightWaveRF",
// "ip_address": "192.168.1.123",
// "email": "[email protected]",
// "pin: "1234"
// }
// ],
//
// If you do not know the IP address of your LightWaveRF ICS-1000, use an app like Fing to find it or
// check your router settings.
//
// 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.
//
/* jslint node: true */
/* globals require: false */
/* globals config: false */
"use strict";
var lightwaverf = require("./lib/lightwaverf.js");
var fs = require('fs');
var path = require('path');
var inherits = require('util').inherits;
var Service, Characteristic, BrightnessUpCharacteristic, BrightnessDownCharacteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
BrightnessUpCharacteristic = function () {
Characteristic.call(this, 'Up', '212131F4-2E14-4FF4-AE13-C97C4232499E');
this.setProps({
format: Characteristic.Formats.BOOL,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
//this.eventEnabled = true;
this.value = this.getDefaultValue();
};
inherits(BrightnessUpCharacteristic, Characteristic);
BrightnessDownCharacteristic = function () {
Characteristic.call(this, 'Down', '212131F4-2E14-4FF4-AE13-C97C5232499E');
this.setProps({
format: Characteristic.Formats.BOOL,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
//this.eventEnabled = true;
this.value = this.getDefaultValue();
};
inherits(BrightnessDownCharacteristic, Characteristic);
homebridge.registerPlatform("homebridge-lightwaverf", "LightWaveRF", LightWaveRFPlatform);
}
function LightWaveRFPlatform(log, config) {
this.log = log;
this.ip_address = config["ip_address"];
this.email = config["email"];
this.pin = config["pin"];
this.timeout = config["timeout"] || 1000;
this.devices = config["devices"];
this.api = null;
this.host = config["manager_host"] || "web.trustsmartcloud.com";
// the path of trustsmartcloud and lightwaverfhost differ
var temp_host_path = "/manager/index.php";
if(this.host == "lightwaverfhost.co.uk") temp_host_path = "/cocomanager/index.php";
this.host_path = config["manager_host_path"] || temp_host_path;
this.log("LightWaveRF Platform Plugin Version " + this.getVersion());
// Prepare to kill pyshell
process.on('SIGINT', () => { this.log("receive SIGINT. Closing"); if(this.api) this.api.close() })
process.on('SIGTERM', () => { this.log("receive SIGTERM. Closing"); if(this.api) this.api.close() })
}
function LightWaveRFAccessory(log, device, api) {
this.roomId = device.roomId;
this.deviceId = device.deviceId;
this.name = device. roomName + " " + device.deviceName;
this.device = device;
this.isDimmer = (device.deviceType.indexOf('D') > -1);
this.isLight = (device.deviceType.indexOf('L') > -1) || this.isDimmer;
this.isSwitch = (device.deviceType.indexOf('S') > -1);
this.isOutlet = (device.deviceType.indexOf('O') > -1);
this.isGarageDoor = (device.deviceType.indexOf('G') > -1);
this.isWindowCovering = (device.deviceType.indexOf('WC') > -1);
this.status = 0; // 0 = off, else on / percentage
this.previousPercentage = 50;
this.previousBlindsPosition = 0;
this.currentBlindsPosition = 0;
this.api = api;
this.log = log;
this.timeOut = device.timeOut ? device.timeOut : 2;
this.brightness_step = 7;
}
function onErr(err) {
this.log("Error: " + err);
return 1;
}
LightWaveRFPlatform.prototype = {
accessories: function(callback) {
this.log("Fetching LightWaveRF switches and dimmers...");
var that = this;
var getLights = function () {
var foundAccessories = [];
// use website
if(that.email && that.pin && that.pin != "") {
var api = new lightwaverf({ip:that.ip_address,email:that.email,pin:that.pin,host:that.host,host_path:that.host_path,timeout:that.timeout}, that.log, function(devices) {
// Add config for devices
if(that.devices) {
for(var i=0;i<that.devices.length;++i) {
var device = that.devices[i];
that.log("device = ");
that.log(device);
var accessory = new LightWaveRFAccessory(that.log, device, api);
foundAccessories.push(accessory);
}
}
if(devices == null) {
that.log("Could not obtain LightWaveRF devices from the server. Using manual configuration");
}
else{
for(var i=0;i<devices.length;++i) {
var device = api.devices[i];
that.log("device = ");
that.log(device);
// check if the device was not specified already in the config
var deviceSpecifiedInConfig = false;
if(that.devices) {
for(var j=0;j<that.devices.length;++j) {
var deviceInConfig = that.devices[j];
if(device.roomId == deviceInConfig.roomId &&
device.roomName === deviceInConfig.roomName &&
device.deviceId == deviceInConfig.deviceId &&
device.deviceName === deviceInConfig.deviceName) {
that.log("Previous device was found in the config, it will not be added");
deviceSpecifiedInConfig = true;
}
}
}
if(!deviceSpecifiedInConfig) {
var accessory = new LightWaveRFAccessory(that.log, device, api);
foundAccessories.push(accessory);
}
}
}
callback(foundAccessories);
}.bind(this));
that.api = api; // store for close()
}
else {
// Use config for devices
if(that.devices) {
var api = new lightwaverf({ip:that.ip_address}, that.log);
for(var i=0;i<that.devices.length;++i) {
var device = that.devices[i];
that.log("device = ");
that.log(device);
var accessory = new LightWaveRFAccessory(that.log, device, api);
foundAccessories.push(accessory);
}
that.api = api; // store for close()
}
callback(foundAccessories);
}
};
getLights();
} ,
getVersion: function() {
var pjPath = path.join(__dirname, './package.json');
var pj = JSON.parse(fs.readFileSync(pjPath));
return pj.version;
}
};
LightWaveRFAccessory.prototype = {
extractValue: function(characteristic, status) {
switch(characteristic.toLowerCase()) {
case 'power':
return status > 0 ? 1 : 0;
case 'brightness':
return status;
case 'door':
return status;
case 'blinds':
return status;
default:
return null;
}
},
// Create and set a light state
executeChange: function(characteristic, value, callback, option) {
this.log.debug("executeChange(" + characteristic + ", " + value + ")");
switch(characteristic.toLowerCase()) {
case 'identify':
// Turn on twice to let the light blink
this.api.turnDeviceOn(this.roomId,this.deviceId);
var that = this;
setTimeout(function () {
that.api.turnDeviceOff(that.roomId,that.deviceId);
}, 1000);
setTimeout(function () {
that.api.turnDeviceOn(that.roomId,that.deviceId);
}, 2000);
setTimeout(function () {
that.api.turnDeviceOff(that.roomId,that.deviceId);
}, 3000);
if(callback) callback();
break;
case 'power':
if (value > 0) {
if(this.isDimmer) {
if(this.previousPercentage < 3.125 ) {
this.previousPercentage = 0; // Prevent very low last states
this.api.turnDeviceOff(this.roomId,this.deviceId,callback);
this.status = 0;
}
else {
this.api.setDeviceDim(this.roomId,this.deviceId,this.previousPercentage,callback);
this.status = this.previousPercentage;
}
} else {
if(this.deviceId < 0) {
this.api.turnRoomOff(this.roomId,callback);
this.status = 1;
} else {
this.api.turnDeviceOn(this.roomId,this.deviceId,callback);
this.status = 1;
}
}
}
else {
//this.previousPercentage = 0;
if(this.deviceId < 0) {
// do nothing
if(callback) callback();
this.status = 0;
} else {
this.api.turnDeviceOff(this.roomId,this.deviceId,callback);
this.status = 0;
}
}
break;
case 'brightness':
this.previousPercentage = value;
// Only write when change is larger than 5
this.status = value;
//if((value % 5) == 0) {
if(value > 0 && this.lightbulbService && !this.lightbulbService.getCharacteristic(Characteristic.On).getValue(null)) {
this.lightbulbService.getCharacteristic(Characteristic.On).setValue(true);
}
this.api.setDeviceDim(this.roomId,this.deviceId,value,callback);
//} else {
// if(callback) callback();
//}
break;
case 'brightness_up':
if(value == 0)
{
this.log("Resetting brightness up button");
callback();
}
// else if(value > 0 && this.previousPercentage >= 100) {
// this.log("Maximum brightness reached");
// callback(); // limit the volume
// }
else {
//this.log("Starting brightness " + this.previousPercentage);
this.previousPercentage += this.brightness_step;
if(this.previousPercentage > 100) this.previousPercentage = 100;
this.log("Changing brightness " + this.previousPercentage);
var targetChar = this.lightbulbService.getCharacteristic(BrightnessUpCharacteristic);
var targetCharBrightness = this.lightbulbService.getCharacteristic(Characteristic.Brightness);
//targetCharBrightness.getValue(null);
setTimeout(function(){targetChar.setValue(0);}, 10);
//this.api.setDeviceDim(this.roomId,this.deviceId,this.previousPercentage,callback);
targetCharBrightness.setValue(this.previousPercentage, callback);
}
break;
case 'brightness_down':
if(value == 0)
{
this.log("Resetting brightness down button");
callback();
}
// else if(value > 0 && this.previousPercentage >= 100) {
// this.log("Maximum brightness reached");
// callback(); // limit the volume
// }
else {
//this.log("Starting brightness " + this.previousPercentage);
this.previousPercentage -= this.brightness_step;
if(this.previousPercentage < 0) this.previousPercentage = 0;
this.log("Changing brightness " + this.previousPercentage);
var targetChar = this.lightbulbService.getCharacteristic(BrightnessUpCharacteristic);
var targetCharBrightness = this.lightbulbService.getCharacteristic(Characteristic.Brightness);
//targetCharBrightness.getValue(null);
setTimeout(function(){targetChar.setValue(0);}, 10);
//this.api.setDeviceDim(this.roomId,this.deviceId,this.previousPercentage,callback);
targetCharBrightness.setValue(this.previousPercentage, callback);
}
break;
case 'door':
if (value == Characteristic.TargetDoorState.CLOSED) {
if(this.isGarageDoor) {
// Is the garage triggered as automatic switch or as close/stop?
if(this.isSwitch)
this.api.turnDeviceOff(this.roomId,this.deviceId, callback);
else
this.api.closeDevice(this.roomId,this.deviceId,callback);
this.status = value;
if(this.openerService) this.openerService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSING);
setTimeout(() => {
if(this.openerService) this.openerService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
// Is the garage triggered as automatic switch or as close/stop?
if(!this.isSwitch)
this.api.stopDevice(this.roomId,this.deviceId);
}, this.timeOut * 1000);
}
else if(callback) callback(1,0);
}
else {
if(this.isGarageDoor) {
// Is the garage triggered as automatic switch or as open/stop?
if(this.isSwitch)
this.api.turnDeviceOn(this.roomId,this.deviceId, callback);
else
this.api.openDevice(this.roomId,this.deviceId,callback);
this.status = value;
if(this.openerService) this.openerService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPENING);
setTimeout(() => {
if(this.openerService) this.openerService.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
// Is the garage triggered as automatic switch or as open/stop?
if(!this.isSwitch)
this.api.stopDevice(this.roomId,this.deviceId);
}, this.timeOut * 1000);
}
else if(callback) callback(1,0);
}
break;
case 'blinds':
//Command to open
// TODO: Setting Blings position from the HomeHit App silder returns more than one value
// this results in a very poor behaviour, blings start moving then stop then start again due to the time out
// differnt approach is needed for next revision
if (value < this.previousBlindsPosition) {
if(this.isWindowCovering){
this.api.closeDevice(this.roomId,this.deviceId,callback);
this.status = value;
if(this.windowOpenerService) this.windowOpenerService.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.DECREASING);
setTimeout(() => {
if(this.windowOpenerService){
this.windowOpenerService.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED);
this.windowOpenerService.getCharacteristic(Characteristic.CurrentPosition).setValue(value);
}
this.api.stopDevice(this.roomId,this.deviceId);
this.previousBlindsPosition = value;
}, this.timeOut * 1000* (this.previousBlindsPosition-value)/100); // full time out - state
}
else if(callback) callback(1,0);
}
else if(value > this.previousBlindsPosition){
if(this.isWindowCovering){
this.api.openDevice(this.roomId,this.deviceId,callback);
this.status = value;
if(this.windowOpenerService) this.windowOpenerService.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.INCREASING);
setTimeout(() => {
if(this.windowOpenerService){
this.windowOpenerService.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED);
this.windowOpenerService.getCharacteristic(Characteristic.CurrentPosition).setValue(value);
}
this.api.stopDevice(this.roomId,this.deviceId);
this.previousBlindsPosition = value;
}, this.timeOut * 1000 * (value-this.previousBlindsPosition)/100);
}
else if(callback) callback(1,0);
}
else{
if(this.isWindowCovering){
this.status = value;
this.previousBlindsPosition = value;
this.windowOpenerService.setCharacteristic(Characteristic.PositionState, Characteristic.PositionState.STOPPED);
this.windowOpenerService.getCharacteristic(Characteristic.CurrentPosition).setValue(value);
//FIXME: ideally I wouldnt be sending STOP but I didint work out how to send back info to HomeKit
// its somthing to do with callback
this.api.stopDevice(this.roomId,this.deviceId,callback);
}
else if(callback) callback(1,0);
}
break;
}//.bind(this));
},
// Read light state
// TODO: implement clever polling/update and caching
// maybe a better NodeJS hue API exists for this
getState: function(characteristic, callback) {
if (callback == null) {
return;
}
else {
var newValue = this.extractValue(characteristic, this.status);
if (newValue != undefined) {
callback(null, newValue);
} else {
//this.log("Device " + that.device.name + " does not support reading characteristic " + characteristic);
// callback(Error("Device " + that.device.name + " does not support reading characteristic " + characteristic) );
callback(1,0);
}
callback = null;
//this.log("Get " + that.device.name + ", characteristic: " + characteristic + ", value: " + value + ".");
}//.bind(this));
},
// Respond to identify request
identify: function(callback) {
this.executeChange("identify");
callback();
},
// Get Services
getServices: function() {
var that = this;
this.lightbulbService = 0;
this.switchService = 0;
this.outletService = 0;
if(this.isLight ) {
// Use HomeKit types defined in HAP node JS
var lightbulbService = new Service.Lightbulb(this.name);
// Basic light controls, common to Hue and Hue lux
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getState("power", callback);})
.on('set', function(value, callback) { that.executeChange("power", value, callback);})
.value = this.extractValue("power", this.status);
if(this.isDimmer) {
lightbulbService
.addCharacteristic(Characteristic.Brightness)
.on('get', function(callback) { that.getState("brightness", callback);})
.on('set', function(value, callback) { that.executeChange("brightness", value, callback);})
.value = this.extractValue("brightness", this.status);
lightbulbService.getCharacteristic(Characteristic.Brightness)
.setProps({ minStep: 1 })
lightbulbService
.addCharacteristic(BrightnessUpCharacteristic)
.on('get', function(callback) { callback(null, 0);})
.on('set', function(value, callback) { that.executeChange("brightness_up", value, callback);});
lightbulbService
.addCharacteristic(BrightnessDownCharacteristic)
.on('get', function(callback) { callback(null, 0);})
.on('set', function(value, callback) { that.executeChange("brightness_down", value, callback);});
}
this.lightbulbService = lightbulbService;
}
else if(this.isSwitch && !this.isGarageDoor) {
// Use HomeKit types defined in HAP node JS
var switchService = new Service.Switch(this.name);
// Basic light controls, common to Hue and Hue lux
switchService
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getState("power", callback);})
.on('set', function(value, callback) { that.executeChange("power", value, callback);})
.value = this.extractValue("power", this.status);
this.switchService = switchService;
}
else if(this.isOutlet) {
// Use HomeKit types defined in HAP node JS
var outletService = new Service.Outlet(this.name);
// Basic light controls, common to Hue and Hue lux
outletService
.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getState("power", callback);})
.on('set', function(value, callback) { that.executeChange("power", value, callback);})
.value = this.extractValue("power", this.status);
this.outletService = outletService;
}
else if(this.isGarageDoor) {
// Use HomeKit types defined in HAP node JS
var openerService = new Service.GarageDoorOpener(this.name);
// Basic light controls, common to Hue and Hue lux
openerService
.getCharacteristic(Characteristic.TargetDoorState)
.on('get', function(callback) { that.getState("door", callback);})
.on('set', function(value, callback) { that.executeChange("door", value, callback);})
.value = this.extractValue("door", this.status);
this.openerService = openerService;
}
else if(this.isWindowCovering) {
// Use HomeKit types defined in HAP node JS
var windowOpenerService = new Service.WindowCovering(this.name);
// Basic light controls, common to Hue and Hue lux
windowOpenerService
.getCharacteristic(Characteristic.TargetPosition)
.on('get', function(callback) { that.getState("blinds", callback);})
.on('set', function(value, callback) { that.executeChange("blinds", value, callback, 0);})
.value = this.extractValue("blinds", this.status);
/*
windowOpenerService
.getCharacteristic(Characteristic.CurrentPosition)
.on('get', function(callback) { that.getState("blinds", callback);})
.on('set', function(value, callback) { that.executeChange("blinds", value, callback, 1);})
.value = this.extractValue("blinds", this.status);
*/
//windowOpenerService.getCharacteristic(Characteristic.PositionState.STOPPED)
this.windowOpenerService = windowOpenerService;
}
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "LightWaveRF")
.setCharacteristic(Characteristic.Model, "ICS-1000")
.setCharacteristic(Characteristic.SerialNumber, "A1S2NASF88EW" + this.roomId + this.deviceId)//this.device.uniqueid)
.addCharacteristic(Characteristic.FirmwareRevision, "0.0.1");
if(this.lightbulbService) return [informationService, this.lightbulbService];
else if(this.switchService) return [informationService, this.switchService];
else if(this.outletService) return [informationService, this.outletService];
else if(this.openerService) return [informationService, this.openerService];
else if(this.windowOpenerService) return [informationService, this.windowOpenerService];
else return [informationService];
}
};