-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
752 lines (614 loc) · 26.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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* jshint node: true */
// Wemo Platform Plugin for HomeBridge (https://github.com/nfarina/homebridge)
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "BelkinWeMo",
// "name": "Belkin WeMo",
// "no_motion_timer": 60 // optional: [WeMo Motion only] a timer (in seconds) which is started no motion is detected, defaults to 60
// }
// ],
"use strict";
var Wemo = require('wemo-client'),
debug = require('debug')('homebridge-platform-wemo');
var Accessory, Characteristic, Consumption, Service, TotalConsumption, UUIDGen;
var wemo = new Wemo();
var noMotionTimer;
module.exports = function (homebridge) {
Accessory = homebridge.platformAccessory;
Characteristic = homebridge.hap.Characteristic;
Service = homebridge.hap.Service;
UUIDGen = homebridge.hap.uuid;
Consumption = function() {
Characteristic.call(this, 'Consumption', 'E863F10D-079E-48FF-8F27-9C2605A29F52');
this.setProps({
format: Characteristic.Formats.UINT16,
unit: 'W',
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
require('util').inherits(Consumption, Characteristic);
Consumption.UUID = 'E863F10D-079E-48FF-8F27-9C2605A29F52';
TotalConsumption = function() {
Characteristic.call(this, 'Total Consumption', 'E863F10C-079E-48FF-8F27-9C2605A29F52');
this.setProps({
format: Characteristic.Formats.UINT32,
unit: 'kWh',
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
require('util').inherits(TotalConsumption, Characteristic);
TotalConsumption.UUID = 'E863F10C-079E-48FF-8F27-9C2605A29F52';
homebridge.registerPlatform("homebridge-platform-wemo", "BelkinWeMo", WemoPlatform, true);
};
function WemoPlatform(log, config, api) {
this.config = config || {};
var self = this;
this.api = api;
this.accessories = {};
this.log = log;
noMotionTimer = this.config.no_motion_timer || 60;
var addDiscoveredDevice = function(device) {
var uuid = UUIDGen.generate(device.UDN);
var accessory;
if (device.deviceType === Wemo.DEVICE_TYPE.Bridge) {
var client = this.client(device , self.log);
client.getEndDevices(function (err, enddevices) {
for (var i = 0, tot = enddevices.length; i < tot; i++) {
uuid = UUIDGen.generate(enddevices[i].deviceId);
accessory = self.accessories[uuid];
if (accessory === undefined) {
self.addLinkAccessory(device, enddevices[i]);
}
else {
self.log("Online: %s [%s]", accessory.displayName, device.deviceId);
self.accessories[uuid] = new WemoLinkAccessory(self.log, accessory, device, enddevices[i]);
}
}
});
}
else {
accessory = self.accessories[uuid];
if (accessory === undefined) {
self.addAccessory(device);
}
else if (accessory instanceof WemoAccessory) {
self.log("Online and can update device: %s [%s]", accessory.displayName, device.macAddress);
accessory.setupDevice(device);
accessory.observeDevice(device);
}
else {
self.log("Online: %s [%s]", accessory.displayName, device.macAddress);
self.accessories[uuid] = new WemoAccessory(self.log, accessory, device);
}
}
}
this.api.on('didFinishLaunching', function() {
wemo.discover(addDiscoveredDevice);
});
setInterval(
function(){
wemo.discover(addDiscoveredDevice);
},
60000
);
}
WemoPlatform.prototype.addAccessory = function(device) {
this.log("Found: %s [%s]", device.friendlyName, device.macAddress);
var serviceType = getServiceType(device.deviceType);
if (serviceType === undefined) {
return;
}
var accessory = new Accessory(device.friendlyName, UUIDGen.generate(device.UDN));
var service = accessory.addService(serviceType, device.friendlyName);
switch(device.deviceType) {
case Wemo.DEVICE_TYPE.Insight:
service.addCharacteristic(Characteristic.OutletInUse);
service.addCharacteristic(Consumption);
service.addCharacteristic(TotalConsumption);
break;
case Wemo.DEVICE_TYPE.Maker:
service.addCharacteristic(Characteristic.ContactSensorState);
break;
}
this.accessories[accessory.UUID] = new WemoAccessory(this.log, accessory, device);
this.api.registerPlatformAccessories("homebridge-platform-wemo", "BelkinWeMo", [accessory]);
}
WemoPlatform.prototype.addLinkAccessory = function(link, device) {
this.log("Found: %s [%s]", device.friendlyName, device.deviceId);
var accessory = new Accessory(device.friendlyName, UUIDGen.generate(device.deviceId));
accessory.addService(Service.Lightbulb, device.friendlyName).addCharacteristic(Characteristic.Brightness);
this.accessories[accessory.UUID] = new WemoLinkAccessory(this.log, accessory, link, device);
this.api.registerPlatformAccessories("homebridge-platform-wemo", "BelkinWeMo", [accessory]);
}
WemoPlatform.prototype.configureAccessory = function(accessory) {
this.accessories[accessory.UUID] = accessory;
}
WemoPlatform.prototype.configurationRequestHandler = function(context, request, callback) {
var self = this;
var respDict = {};
if (request && request.type === "Terminate") {
context.onScreen = null;
}
var sortAccessories = function() {
context.sortedAccessories = Object.keys(self.accessories).map(
function(k){return this[k] instanceof Accessory ? this[k] : this[k].accessory},
self.accessories
).sort(function(a,b) {if (a.displayName < b.displayName) return -1; if (a.displayName > b.displayName) return 1; return 0});
return Object.keys(context.sortedAccessories).map(function(k) {return this[k].displayName}, context.sortedAccessories);
}
switch(context.onScreen) {
case "DoRemove":
if (request.response.selections) {
for (var i in request.response.selections.sort()) {
this.removeAccessory(context.sortedAccessories[request.response.selections[i]]);
}
respDict = {
"type": "Interface",
"interface": "instruction",
"title": "Finished",
"detail": "Accessory removal was successful."
}
context.onScreen = null;
callback(respDict);
}
else {
context.onScreen = null;
callback(respDict, "platform", true, this.config);
}
break;
case "Menu":
context.onScreen = "Remove";
case "Remove":
respDict = {
"type": "Interface",
"interface": "list",
"title": "Select accessory to " + context.onScreen.toLowerCase(),
"allowMultipleSelection": context.onScreen == "Remove",
"items": sortAccessories()
}
context.onScreen = "Do" + context.onScreen;
callback(respDict);
break;
default:
if (request && (request.response || request.type === "Terminate")) {
context.onScreen = null;
callback(respDict, "platform", true, this.config);
}
else {
respDict = {
"type": "Interface",
"interface": "list",
"title": "Select option",
"allowMultipleSelection": false,
"items": ["Remove Accessory"]
}
context.onScreen = "Menu";
callback(respDict);
}
}
}
WemoPlatform.prototype.removeAccessory = function(accessory) {
this.log("Remove: %s", accessory.displayName);
if (this.accessories[accessory.UUID]) {
delete this.accessories[accessory.UUID];
}
this.api.unregisterPlatformAccessories("homebridge-platform-wemo", "BelkinWeMo", [accessory]);
}
function WemoAccessory(log, accessory, device) {
var self = this;
this.accessory = accessory;
this.device = device;
this.log = log;
this.service = this.getService();
this.setupDevice(device);
this.updateReachability(true);
this.accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Belkin WeMo")
.setCharacteristic(Characteristic.Model, device.modelName)
.setCharacteristic(Characteristic.SerialNumber, device.serialNumber)
.setCharacteristic(Characteristic.FirmwareRevision, device.firmwareVersion);
this.accessory.on('identify', function(paired, callback) {
self.log("%s - identify", self.accessory.displayName);
callback();
});
if (this.service.testCharacteristic(Characteristic.Name) === false) {
this.service.addCharacteristic(Characteristic.Name);
}
if (this.service.getCharacteristic(Characteristic.Name).value === undefined) {
this.service.getCharacteristic(Characteristic.Name).setValue(device.friendlyName);
}
this.observeDevice(device);
}
WemoAccessory.prototype.getAttributes = function(callback) {
callback = callback || function() {};
this.client.getAttributes(function(err, attributes) {
if (err) {
callback();
return;
}
this.updateSwitchState(attributes.Switch);
this.updateSensorState(attributes.Sensor);
callback();
}.bind(this));
}
WemoAccessory.prototype.getSwitchState = function(callback) {
if (this.device.deviceType === Wemo.DEVICE_TYPE.Maker) {
this.getAttributes(function() {
callback(null, this.service.getCharacteristic(Characteristic.On).value);
}.bind(this));
}
else {
this.client.getBinaryState(function(err, state) {
if (err) {
callback(null, this.service.getCharacteristic(Characteristic.On).value);
return;
}
callback(null, this.updateSwitchState(state));
}.bind(this));
}
}
WemoAccessory.prototype.getService = function() {
var service = getServiceType(this.device.deviceType);
if (service === undefined) {
return;
}
return this.accessory.getService(service);
}
WemoAccessory.prototype.observeDevice = function (device) {
if (device.deviceType === Wemo.DEVICE_TYPE.Maker) {
this.getAttributes();
this.client.on('attributeList', function(name, value, prevalue, timestamp) {
switch(name) {
case 'Switch':
this.updateSwitchState(value);
break;
case 'Sensor':
this.updateSensorState(value);
break;
}
}.bind(this));
}
else {
this.client.on('binaryState', function(state) {
if (this.device.deviceType === Wemo.DEVICE_TYPE.Motion || this.device.deviceType === "urn:Belkin:device:NetCamSensor:1") {
this.updateMotionDetected(state);
}
else {
this.updateSwitchState(state);
}
}.bind(this));
}
if (device.deviceType === Wemo.DEVICE_TYPE.Insight) {
this.client.on('insightParams', this.updateInsightParams.bind(this));
}
}
WemoAccessory.prototype.setSwitchState = function (state, callback) {
var value = state > 0;
var switchState = this.service.getCharacteristic(Characteristic.On);
callback = callback || function() {};
if (switchState.value != value) { //remove redundent calls to setBinaryState when requested state is already achieved
this.client.setBinaryState(value | 0, function (err) {
if(!err) {
this.log("%s - Set state: %s", this.accessory.displayName, (value ? "on" : "off"));
//this.onState = value;
callback(null);
}
else {
this.log("%s - Set state FAILED: %s. Error: %s", this.accessory.displayName, (value ? "on" : "off"), err.code);
callback(new Error(err));
}
}.bind(this));
}
else {
callback(null);
}
}
WemoAccessory.prototype.setupDevice = function(device) {
this.device = device;
this.client = wemo.client(device);
this.client.on('error', function(err) {
this.log('%s reported error %s', self.accessory.displayName, err.code);
}.bind(this));
}
WemoAccessory.prototype.updateConsumption = function(raw) {
var value = Math.round(raw / 1000);
if (this.service.getCharacteristic(Consumption).value !== value) {
this.log("%s - Consumption: %sw", this.accessory.displayName, value);
this.service.getCharacteristic(Consumption).setValue(value);
}
return value;
}
WemoAccessory.prototype.updateEventHandlers= function(characteristic) {
if (this.service === undefined) {
return;
}
if (this.service.testCharacteristic(characteristic) === false) {
return;
}
this.service.getCharacteristic(characteristic).removeAllListeners();
if (this.accessory.reachable !== true) {
return;
}
switch(characteristic) {
case Characteristic.On:
this.service
.getCharacteristic(characteristic)
.on('get', this.getSwitchState.bind(this))
.on('set', this.setSwitchState.bind(this));
break;
}
}
WemoAccessory.prototype.updateInsightParams = function(state, power, data) {
this.updateOutletInUse(state);
this.updateConsumption(power);
this.updateTotalConsumption(data.TodayConsumed);
}
WemoAccessory.prototype.updateOutletInUse = function(state) {
var value = state == 1;
var outletInUse = this.service.getCharacteristic(Characteristic.OutletInUse);
if (outletInUse.value !== value) {
this.log("%s - Outlet In Use: %s", this.accessory.displayName, (value ? "Yes" : "No"));
outletInUse.setValue(value);
}
return value;
}
WemoAccessory.prototype.updateMotionDetected = function(state) {
var value = state > 0;
var motionDetected = this.service.getCharacteristic(Characteristic.MotionDetected);
if (value === motionDetected.value || (value === false && this.motionTimer)) {
return;
}
if (value === true || noMotionTimer == 0) {
if (this.motionTimer) {
this.log("%s - no motion timer stopped", this.accessory.displayName);
clearTimeout(self.motionTimer);
this.motionTimer = null;
}
this.log("%s - Motion Sensor: %s", this.accessory.displayName, (value ? "Detected" : "Clear"));
motionDetected.setValue(value);
}
else {
this.log("%s - no motion timer started [%d secs]", this.accessory.displayName, noMotionTimer);
clearTimeout(this.motionTimer);
this.motionTimer = setTimeout(function(self) {
self.log("%s - Motion Sensor: Clear; no motion timer completed", self.accessory.displayName);
self.service.getCharacteristic(Characteristic.MotionDetected).setValue(false);
self.motionTimer = null;
}, noMotionTimer * 1000, this);
}
}
WemoAccessory.prototype.updateReachability = function(reachable) {
this.accessory.updateReachability(reachable);
switch(this.device.deviceType) {
case Wemo.DEVICE_TYPE.Insight:
case Wemo.DEVICE_TYPE.LightSwitch:
case Wemo.DEVICE_TYPE.Maker:
case Wemo.DEVICE_TYPE.Switch:
this.updateEventHandlers(Characteristic.On);
break;
case Wemo.DEVICE_TYPE.Motion:
case "urn:Belkin:device:NetCamSensor:1":
break;
default:
console.log("Not implemented");
}
}
WemoAccessory.prototype.updateSensorState = function(state) {
var value = !(state > 0);
var sensorState = this.service.getCharacteristic(Characteristic.ContactSensorState);
if (sensorState.value !== value) {
this.log("%s - Sensor: %s", this.accessory.displayName, (value ? "Detected" : "Not detected"));
sensorState.setValue(value ? Characteristic.ContactSensorState.CONTACT_DETECTED: Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
}
return value;
}
WemoAccessory.prototype.updateSwitchState = function(state) {
var value = state > 0;
var switchState = this.service.getCharacteristic(Characteristic.On)
if (switchState.value !== value) {
this.log("%s - Get state: %s", this.accessory.displayName, (value ? "On" : "Off"));
switchState.setValue(value);
if(value === false && this.device.deviceType === Wemo.DEVICE_TYPE.Insight) {
this.updateOutletInUse(0);
this.updateConsumption(0);
}
}
return value;
}
WemoAccessory.prototype.updateTotalConsumption = function(raw) {
var value = Math.round(raw / 10000 * 6) / 100;
var totalConsumption = this.service.getCharacteristic(TotalConsumption);
if (totalConsumption.value !== value) {
this.log("%s - Total Consumption: %skwh", this.accessory.displayName, value);
totalConsumption.setValue(value);
}
return value;
}
function WemoLinkAccessory(log, accessory, link, device) {
var self = this;
this.accessory = accessory;
this.link = link;
this.device = device;
this.log = log;
this.client = wemo.client(link, log);
this.onState = false;
this.brightness = null;
this.client.on('error', function(err) {
self.log('%s reported error %s', self.accessory.displayName, err.code);
});
this.updateReachability(true);
this.accessory.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, "Belkin WeMo")
.setCharacteristic(Characteristic.SerialNumber, device.deviceId);
this.accessory.on('identify', function(paired, callback) {
self.log("%s - identify", self.accessory.displayName);
callback();
});
var service = this.accessory.getService(Service.Lightbulb);
if (service.testCharacteristic(Characteristic.Name) === false) {
service.addCharacteristic(Characteristic.Name);
}
if (service.getCharacteristic(Characteristic.Name).value === undefined) {
service.getCharacteristic(Characteristic.Name).setValue(device.friendlyName);
}
// we can't depend on the capabilities returned from Belkin so we'll go ask expliciitly.
this.getStatus(function (err) {
self.onState = (self.device.capabilities[WemoLinkAccessory.OPTIONS.Switch].substr(0,1) === '1') ? true : false ;
self.log("%s (bulb) reported as %s", self.accessory.displayName, self.onState ? "on" : "off");
self.brightness = Math.round(self.device.capabilities[WemoLinkAccessory.OPTIONS.Brightness].split(':').shift() * 100 / 255 );
self.log("%s (bulb) reported as at %s%% brightness", self.accessory.displayName, self.brightness);
});
// register eventhandler
this.client.on('statusChange', function(deviceId, capabilityId, value) {
self.statusChange(deviceId, capabilityId, value);
});
}
WemoLinkAccessory.OPTIONS = {
Brightness: '10008',
Color: '10300',
Switch: '10006'
}
WemoLinkAccessory.prototype.getStatus = function (callback) {
// this function is called on initialisation of a Bulbs because we can't rely on Belkin's
// capabilities structure on initialisation so we'll explicity retrieve it here.
callback = callback || function() {};
this.client.getDeviceStatus(this.device.deviceId, function (err, capabilities) {
if(err) {
callback("unknown error getting device status (getStatus)", capabilities);
return;
}
if (!capabilities[WemoLinkAccessory.OPTIONS.Switch].length) { // we've get no data in the capabilities array, so it's off
this.log("%s appears to be off, i.e. at the power!", this.name);
}
else {
//this.log("getStatus: %s is ", this.name, capabilities);
this._capabilities = capabilities;
}
callback(null)
}.bind(this));
}
WemoLinkAccessory.prototype.setBrightness = function (value, callback) {
this._brightness = value;
callback = callback || function() {};
//defer the actual update by 100 milliseconds to smooth out changes from sliders
setTimeout(function(brightness, caller) {
//check that we actually have a change to make and that something
//hasn't tried to update the brightness again in the last 100 milliseconds
if(caller.brightness !== brightness && caller._brightness == brightness) {
caller.client.setDeviceStatus(caller.device.deviceId, WemoLinkAccessory.OPTIONS.Brightness, brightness * 255 / 100);
caller.log("setBrightness: %s to %s%%", caller.accessory.displayName, brightness);
caller.brightness = brightness;
}
}, 100, value, this);
callback(null);
}
WemoLinkAccessory.prototype.setOnStatus = function (value, callback) {
this.log("this.Onstate currently %s, value is %s", this.onState, value );
callback = callback || function() {};
if(this.onState !== value) { // if we have nothing to do so lets leave it at that.
this.onState = value;
this.log("this.Onstate now: %s", this.onState);
this.log("setOnStatus: %s to %s", this.accessory.displayName, value > 0 ? "on" : "off");
this.client.setDeviceStatus(this.device.deviceId, WemoLinkAccessory.OPTIONS.Switch, value | 0, function(err, response) {
callback(null);
});
}
}
WemoLinkAccessory.prototype.statusChange = function(deviceId, capabilityId, value) {
// We receive this notification if the wemo's are changed by Homekit (i.e us) or
// some other trigger (i.e. any of the pethora of wemo apps).
// We want to update homekit with these changes,
// to do that we need to use setValue which triggers another call back to here which
// we need to ignore - much of this function deals with the idiosyncrasies around this issue.
if (this.device.deviceId !== deviceId){
// we get called for every bulb on the link so lets get out of here if the call is for a differnt bulb
this.log('statusChange Ignored (device): ', this.device.deviceId, deviceId, capabilityId, value);
return;
}
if (this.device.capabilities[capabilityId] === value) {
// nothing's changed - lets get out of here to stop an endless loop as
// this callback was probably triggered by us updating HomeKit
this.log('statusChange Ignored (capability): ', deviceId, capabilityId, value);
return;
}
this.log('statusChange processing: ', deviceId, capabilityId, value);
// update our internal array with newly passed value.
this.device.capabilities[capabilityId] = value;
switch(capabilityId) {
case WemoLinkAccessory.OPTIONS.Brightness: // this is a brightness change
// update our convenience variable ASAP to minimise race condition possibiities
var newbrightness = Math.round(this.device.capabilities[WemoLinkAccessory.OPTIONS.Brightness].split(':').shift() * 100 / 255 );
// changing wemo bulb brightness always turns them on so lets reflect this locally and in homekit.
// do we really need this or do we get both status change messages from wemo?
if (!this.onState){ // if off
this.log('Update homekit onState: %s is %s', this.accessory.displayName, true);
this.device.capabilities[WemoLinkAccessory.OPTIONS.Switch] = '1';
this.accessory.getService(Service.Lightbulb).getCharacteristic(Characteristic.On).setValue(true);
}
// call setValue to update HomeKit and iOS (this generates another statusChange that will get ignored)
this.log('Update homekit brightness: %s is %s', this.accessory.displayName, newbrightness);
this.accessory.getService(Service.Lightbulb).getCharacteristic(Characteristic.Brightness).setValue(newbrightness);
break;
case WemoLinkAccessory.OPTIONS.Switch: // on/off/etc
// reflect change of onState from potentially and external change (from Wemo App for instance)
var newState = (this.device.capabilities[WemoLinkAccessory.OPTIONS.Switch].substr(0,1) === '1') ? true : false;
// similarly we need to update iOS with this change - which will trigger another state shange which we'll ignore
this.log('Update homekit onState: %s is %s', this.accessory.displayName, newState);
this.accessory.getService(Service.Lightbulb).getCharacteristic(Characteristic.On).setValue(newState);
break;
default:
console.log("This capability (%s) not implemented", capabilityId);
}
}
WemoLinkAccessory.prototype.updateEventHandlers= function (characteristic) {
var self = this;
var service = this.accessory.getService(Service.Lightbulb)
if (service.testCharacteristic(characteristic) === false) {
return;
}
service.getCharacteristic(characteristic).removeAllListeners();
if (this.accessory.reachable !== true) {
return;
}
switch(characteristic) {
case Characteristic.On:
service
.getCharacteristic(Characteristic.On)
.on('set', this.setOnStatus.bind(this))
.on('get', function(callback) {callback(null, self.onState)});
break;
case Characteristic.Brightness:
service
.getCharacteristic(Characteristic.Brightness)
.on('set', this.setBrightness.bind(this))
.on('get', function(callback) {callback(null, self.brightness)});
break;
}
}
WemoLinkAccessory.prototype.updateReachability = function(reachable) {
this.accessory.updateReachability(reachable);
this.updateEventHandlers(Characteristic.On);
this.updateEventHandlers(Characteristic.Brightness);
}
function getServiceType(deviceType) {
var service;
switch(deviceType) {
case Wemo.DEVICE_TYPE.Insight:
case Wemo.DEVICE_TYPE.LightSwitch:
case Wemo.DEVICE_TYPE.Maker:
case Wemo.DEVICE_TYPE.Switch:
service = Service.Switch;
break;
case Wemo.DEVICE_TYPE.Motion:
case "urn:Belkin:device:NetCamSensor:1":
service = Service.MotionSensor;
break;
default:
console.log("Not Supported: %s", deviceType);
}
return service;
}