forked from jrhubott/homebridge-homeseer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1490 lines (1299 loc) · 65.3 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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
//
// HomeSeer Platform Shim for HomeBridge by Jean-Michel Joudrier - (stipus at stipus dot com)
// V0.1 - 2015/10/07
// - Initial version
// V0.2 - 2015/10/10
// - Occupancy sensor fix
// V0.3 - 2015/10/11
// - Added TemperatureUnit=F|C option to temperature sensors
// - Added negative temperature support to temperature sensors
// V0.4 - 2015/10/12
// - Added thermostat support
// V0.5 - 2015/10/12
// - Added Humidity sensor support
// V0.6 - 2015/10/12
// - Added Battery support
// - Added low battery support for all sensors
// - Added HomeSeer event support (using HomeKit switches...)
// V0.7 - 2015/10/13
// - You can add multiple HomeKit devices for the same HomeSeer device reference
// - Added CarbonMonoxide sensor
// - Added CarbonDioxide sensor
// - Added onValues option to all binary sensors
// V0.8 - 2015/10/14
// - Added uuid_base parameter to all accessories
// V0.9 - 2015/10/16
// - Smoke sensor battery fix
// - Added offEventGroup && offEventName to events (turn <event> on launches one HS event. turn <event> off can launch another HS event)
// - Added GarageDoorOpener support
// - Added Lock support
// V0.10 - 2015/10/29
// - Added Security System support
// - Added Window support
// - Added Window Covering support
// - Added obstruction support to doors, windows, and windowCoverings
//
//
// Remember to add platform to config.json.
//
// You can get HomeSeer Device References by clicking a HomeSeer device name, then
// choosing the Advanced Tab.
//
// The uuid_base parameter is valid for all events and accessories.
// If you set this parameter to some unique identifier, the HomeKit accessory ID will be based on uuid_base instead of the accessory name.
// It is then easier to change the accessory name without messing the HomeKit database.
//
//
// Example:
// "platforms": [
// {
// "platform": "HomeSeer", // Required
// "name": "HomeSeer", // Required
// "host": "http://192.168.3.4:81", // Required - If you did setup HomeSeer authentication, use "http://user:password@ip_address:port"
//
// "events":[ // Optional - List of Events - Currently they are imported into HomeKit as switches
// {
// "eventGroup":"My Group", // Required - The HomeSeer event group
// "eventName":"My On Event", // Required - The HomeSeer event name
// "offEventGroup":"My Group", // Optional - The HomeSeer event group for turn-off <event>
// "offEventName":"My Off Event", // Optional - The HomeSeer event name for turn-off <event>
// "name":"Test", // Optional - HomeSeer event name is the default
// "uuid_base":"SomeUniqueId" // Optional - HomeKit identifier will be derived from this parameter instead of the name
// }
// ],
//
// "accessories":[ // Required - List of Accessories
// {
// "ref":8, // Required - HomeSeer Device Reference (To get it, select the HS Device - then Advanced Tab)
// "type":"Lightbulb", // Optional - Lightbulb is the default
// "name":"My Light", // Optional - HomeSeer device name is the default
// "offValue":"0", // Optional - 0 is the default
// "onValue":"100", // Optional - 100 is the default
// "can_dim":true, // Optional - true is the default - false for a non dimmable lightbulb
// "uuid_base":"SomeUniqueId2" // Optional - HomeKit identifier will be derived from this parameter instead of the name. You SHOULD add this parameter to all accessories !
// },
// {
// "ref":9 // This is a dimmable Lightbulb by default
// },
// {
// "ref":58, // This is a controllable outlet
// "type":"Outlet"
// },
// {
// "ref":111, // Required - HomeSeer Device Reference for your sensor
// "type":"TemperatureSensor", // Required for a temperature sensor
// "temperatureUnit":"F", // Optional - C is the default
// "name":"Bedroom temp", // Optional - HomeSeer device name is the default
// "batteryRef":112, // Optional - HomeSeer device reference for the sensor battery level
// "batteryThreshold":15 // Optional - If sensor battery level is below this value, the HomeKit LowBattery characteristic is set to 1. Default is 10
// },
// {
// "ref":34, // Required - HomeSeer Device Reference for your sensor
// "type":"SmokeSensor", // Required for a smoke sensor
// "name":"Kichen smoke detector", // Optional - HomeSeer device name is the default
// "batteryRef":35, // Optional - HomeSeer device reference for the sensor battery level
// "batteryThreshold":15, // Optional - If sensor battery level is below this value, the HomeKit LowBattery characteristic is set to 1. Default is 10
// "onValues":[1,1.255] // Optional - List of all HomeSeer values triggering a "ON" sensor state - Default is any value different than 0
// },
// {
// "ref":34, // Required - HomeSeer Device Reference for your sensor (Here it's the same device as the SmokeSensor above)
// "type":"CarbonMonoxideSensor", // Required for a carbon monoxide sensor
// "name":"Kichen CO detector", // Optional - HomeSeer device name is the default
// "batteryRef":35, // Optional - HomeSeer device reference for the sensor battery level
// "batteryThreshold":15, // Optional - If sensor battery level is below this value, the HomeKit LowBattery characteristic is set to 1. Default is 10
// "onValues":[2,2.255] // Optional - List of all HomeSeer values triggering a "ON" sensor state - Default is any value different than 0
// },
// {
// "ref":113, // Required - HomeSeer Device Reference of the Current Temperature Device
// "type":"Thermostat", // Required for a Thermostat
// "name":"Température Salon", // Optional - HomeSeer device name is the default
// "temperatureUnit":"C", // Optional - F for Fahrenheit, C for Celsius, C is the default
// "setPointRef":167, // Required - HomeSeer device reference for your thermostat Set Point.
// "setPointReadOnly":true, // Optional - Set to false if your SetPoint is read/write. true is the default
// "stateRef":166, // Required - HomeSeer device reference for your thermostat current state
// "stateOffValues":[0,4,5], // Required - List of the HomeSeer device values for a HomeKit state=OFF
// "stateHeatValues":[1], // Required - List of the HomeSeer device values for a HomeKit state=HEAT
// "stateCoolValues":[2], // Required - List of the HomeSeer device values for a HomeKit state=COOL
// "stateAutoValues":[3], // Required - List of the HomeSeer device values for a HomeKit state=AUTO
// "controlRef":168, // Required - HomeSeer device reference for your thermostat mode control (It can be the same as stateRef for some thermostats)
// "controlOffValue":0, // Required - HomeSeer device control value for OFF
// "controlHeatValue":1, // Required - HomeSeer device control value for HEAT
// "controlCoolValue":2, // Required - HomeSeer device control value for COOL
// "controlAutoValue":3, // Required - HomeSeer device control value for AUTO
// "coolingThresholdRef":169, // Optional - Not-implemented-yet - HomeSeer device reference for your thermostat cooling threshold
// "heatingThresholdRef":170 // Optional - Not-implemented-yet - HomeSeer device reference for your thermostat heating threshold
// },
// {
// "ref":200, // Required - HomeSeer Device Reference of a garage door opener
// "type":"GarageDoorOpener", // Required for a Garage Door Opener
// "name":"Garage Door", // Optional - HomeSeer device name is the default
// "stateRef":201, // Required - HomeSeer device reference for your garage door opener current state (can be the same as ref)
// "stateOpenValues":[0], // Required - List of the HomeSeer device values for a HomeKit state=OPEN
// "stateClosedValues":[1], // Required - List of the HomeSeer device values for a HomeKit state=CLOSED
// "stateOpeningValues":[2], // Optional - List of the HomeSeer device values for a HomeKit state=OPENING
// "stateClosingValues":[3], // Optional - List of the HomeSeer device values for a HomeKit state=CLOSING
// "stateStoppedValues":[4], // Optional - List of the HomeSeer device values for a HomeKit state=STOPPED
// "controlRef":201, // Required - HomeSeer device reference for your garage door opener control (can be the same as ref and stateRef)
// "controlOpenValue":0, // Required - HomeSeer device control value for OPEN
// "controlCloseValue":1, // Required - HomeSeer device control value for CLOSE
// "obstructionRef":201, // Optional - HomeSeer device reference for your garage door opener obstruction state (can be the same as ref)
// "obstructionValues":[5], // Optional - List of the HomeSeer device values for a HomeKit obstruction state=OBSTRUCTION
// "lockRef":202, // Optional - HomeSeer device reference for your garage door lock (can be the same as ref)
// "lockUnsecuredValues":[0], // Optional - List of the HomeSeer device values for a HomeKit lock state=UNSECURED
// "lockSecuredValues":[1], // Optional - List of the HomeSeer device values for a HomeKit lock state=SECURED
// "lockJammedValues":[2], // Optional - List of the HomeSeer device values for a HomeKit lock state=JAMMED
// "unlockValue":0, // Optional - HomeSeer device control value to unlock the garage door opener
// "lockValue":1 // Optional - HomeSeer device control value to lock the garage door opener
// },
// {
// "ref":210, // Required - HomeSeer Device Reference of a Lock
// "type":"Lock", // Required for a Lock
// "name":"Main Door Lock", // Optional - HomeSeer device name is the default
// "lockUnsecuredValues":[0], // Required - List of the HomeSeer device values for a HomeKit lock state=UNSECURED
// "lockSecuredValues":[1], // Required - List of the HomeSeer device values for a HomeKit lock state=SECURED
// "lockJammedValues":[2], // Optional - List of the HomeSeer device values for a HomeKit lock state=JAMMED
// "unlockValue":0, // Required - HomeSeer device control value to unlock
// "lockValue":1 // Required - HomeSeer device control value to lock
// },
// {
// "ref":230, // Required - HomeSeer Device Reference of a Security System
// "type":"SecuritySystem", // Required for a security system
// "name":"Home alarm", // Optional - HomeSeer device name is the default
// "armedStayValues":[0], // Optional - List of the HomeSeer device values for a HomeKit security state=ARMED-STAY
// "armedAwayValues":[1], // Optional - List of the HomeSeer device values for a HomeKit security state=ARMED-AWAY
// "armedNightValues":[2], // Optional - List of the HomeSeer device values for a HomeKit security state=ARMED-NIGHT
// "disarmedValues":[3], // Optional - List of the HomeSeer device values for a HomeKit security state=DISARMED
// "alarmValues":[4], // Optional - List of the HomeSeer device values for a HomeKit security state=ALARM
// "armStayValue":0, // Required - HomeSeer device control value to arm in stay mode. If you don't have this mode, select any value that arms your system
// "armAwayValue":1, // Required - HomeSeer device control value to arm in away mode. If you don't have this mode, select any value that arms your system
// "armNightValue":2, // Required - HomeSeer device control value to arm in night mode. If you don't have this mode, select any value that arms your system
// "disarmValue":3 // Required - HomeSeer device control value to disarm security system
// },
// {
// "ref":115, // Required - HomeSeer Device Reference for a device holding battery level (0-100)
// "type":"Battery", // Required for a Battery
// "name":"Roomba battery", // Optional - HomeSeer device name is the default
// "batteryThreshold":15 // Optional - If the level is below this value, the HomeKit LowBattery characteristic is set to 1. Default is 10
// },
// {
// "ref":240, // Required - HomeSeer Device Reference for a door - HomeSeer values must go from 0 (closed) to 100 (open)
// "type":"Door", // Required for a Door
// "name":"Main door", // Optional - HomeSeer device name is the default
// "obstructionRef":241, // Optional - HomeSeer device reference for your door obstruction state (can be the same as ref)
// "obstructionValues":[1] // Optional - List of the HomeSeer device values for a HomeKit obstruction state=OBSTRUCTION
// }
// ]
// }
// ],
//
//
// SUPORTED TYPES:
// - Lightbulb (can_dim, onValue, offValue options)
// - Fan (onValue, offValue options)
// - Switch (onValue, offValue options)
// - Outlet (onValue, offValue options)
// - Thermostat (temperatureUnit, setPoint, state, control options)
// - TemperatureSensor (temperatureUnit=C|F)
// - HumiditySensor (HomeSeer device value in % - batteryRef, batteryThreshold options)
// - LightSensor (HomeSeer device value in Lux - batteryRef, batteryThreshold options)
// - ContactSensor (onValues, batteryRef, batteryThreshold options)
// - MotionSensor (onValues, batteryRef, batteryThreshold options)
// - LeakSensor (onValues, batteryRef, batteryThreshold options)
// - OccupancySensor (onValues, batteryRef, batteryThreshold options)
// - SmokeSensor (onValues, batteryRef, batteryThreshold options)
// - CarbonMonoxideSensor (onValues, batteryRef, batteryThreshold options)
// - CarbonDioxideSensor (onValues, batteryRef, batteryThreshold options)
// - Battery (batteryThreshold option)
// - GarageDoorOpener (state, control, obstruction, lock options)
// - Lock (unsecured, secured, jammed options)
// - SecuritySystem (arm, disarm options)
// - Door (obstruction option)
// - Window (obstruction option)
// - WindowCovering (obstruction option)
//var Service = require("../api").homebridge.hap.Service;
//var Characteristic = require("../api").homebridge.hap.Characteristic;
var request = require("request");
var pollingtoevent = require("polling-to-event")
var http = require('http');
var Accessory, Service, Characteristic, UUIDGen;
var pollingOffsetCounter=0;
var _services = [];
module.exports = function (homebridge) {
console.log("homebridge API version: " + homebridge.version);
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
// For platform plugin to be considered as dynamic platform plugin,
// registerPlatform(pluginName, platformName, constructor, dynamic), dynamic must be true
homebridge.registerPlatform("homebridge-HomeSeerPlatform", "HomeSeer", HomeSeerPlatform, true);
}
function httpRequest(url, method, callback) {
request({
url: url,
method: method
},
function (error, response, body) {
callback(error, response, body)
})
}
function HomeSeerPlatform(log, config, api) {
this.log = log;
this.config = config;
if(config && this.config["poll"]==null)
{
this.config["poll"] = 60;
}
if(config)
this.log("System default periodic polling rate set to " + this.config["poll"] + ' seconds');
}
HomeSeerPlatform.prototype = {
accessories: function (callback) {
var that = this;
var foundAccessories = [];
if (this.config.events) {
this.log("Creating HomeSeer events.");
for (var i = 0; i < this.config.events.length; i++) {
var event = new HomeSeerEvent(that.log, that.config, that.config.events[i]);
foundAccessories.push(event);
}
}
this.log("Fetching HomeSeer devices.");
var refList = "";
for (var i = 0; i < this.config.accessories.length; i++) {
refList = refList + this.config.accessories[i].ref;
if (i < this.config.accessories.length - 1)
refList = refList + ",";
}
var url = this.config["host"] + "/JSON?request=getstatus&ref=" + refList;
httpRequest(url, "GET", function (error, response, body) {
if (error) {
this.log('HomeSeer status function failed: %s', error.message);
callback(foundAccessories);
}
else {
this.log('HomeSeer status function succeeded!');
var response = JSON.parse(body);
for (var i = 0; i < this.config.accessories.length; i++) {
for (var j = 0; j < response.Devices.length; j++) {
if (this.config.accessories[i].ref == response.Devices[j].ref) {
var accessory = new HomeSeerAccessory(that.log, that.config, this.config.accessories[i], response.Devices[j]);
foundAccessories.push(accessory);
break;
}
}
}
callback(foundAccessories);
}
}.bind(this));
//Update the status of all accessory's
// for (var i = 0, len = foundAccessories.length; i < len; i++) {
// foundAccessories[i].updateStatus(function(){}.bind(this));
// }
}
}
function HomeSeerAccessory(log, platformConfig, accessoryConfig, status) {
this.log = log;
this.config = accessoryConfig;
this.ref = status.ref;
this.name = status.name
this.model = status.device_type_string;
this.onValue = 100;
this.offValue = 0;
this.statusCharacteristic = null;
this.access_url = platformConfig["host"] + "/JSON?";
this.control_url = this.access_url + "request=controldevicebyvalue&ref=" + this.ref + "&value=";
this.status_url = this.access_url + "request=getstatus&ref=" + this.ref;
if (this.config.name)
this.name = this.config.name;
if (this.config.uuid_base)
this.uuid_base = this.config.uuid_base;
if (this.config.onValue)
this.onValue = this.config.onValue;
if (this.config.offValue)
this.offValue = this.config.offValue;
var that = this;
if (this.config.poll==null)
{
//Default to 1 minute polling cycle
this.config.poll = platformConfig["poll"];
}
}
HomeSeerAccessory.prototype = {
identify: function (callback) {
callback();
},
updateStatus: function (callback) {
if (this.statusCharacteristic != null) {
this.statusCharacteristic.getValue();
}
if (callback != null)
callback();
},
updateStatusByPolling: function () {
this.statusUpdateCount++;
if(this.statusUpdateCount <= this.config.statusUpdateCount)
{
this.updateStatus(null);
}
else
{
this.log(this.name + ": Completed polling for status update");
this.pollForStatusUpdate.pause();
}
},
pollForUpdate: function () {
this.log(this.name + ": Polling for status update " + this.config.statusUpdateCount + " times");
this.statusUpdateCount=0;
this.pollForStatusUpdate.resume();
},
setPowerState: function (powerOn, callback) {
var url;
if (powerOn) {
url = this.control_url + this.onValue;
this.log(this.name + ": Setting power state to on");
}
else {
url = this.control_url + this.offValue;
this.log(this.name + ": Setting power state to off");
}
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': HomeSeer power function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': HomeSeer power function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getPowerState: function (callback) {
var url = this.status_url;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getPowerState function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getPowerState function succeeded: value=' + value);
if (value == 0)
callback(null, 0);
else
callback(null, 1);
}
}.bind(this));
},
getBinarySensorState: function (callback) {
var url = this.status_url;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getBinarySensorState function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getBinarySensorState function succeeded: value=' + value);
if (this.config.onValues) {
if (this.config.onValues.indexOf(value) != -1)
callback(null, 1);
else
callback(null, 0);
}
else {
if (value != 0)
callback(null, 1);
else
callback(null, 0);
}
}
}.bind(this));
},
/* Duplicate brightness set, this one without a delay
setBrightness: function (level, callback) {
var url = this.control_url + level;
this.log(this.name + ": Setting value to %s", level);
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setBrightness function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setBrightness function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
*/
getValue: function (callback) {
var url = this.status_url;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getValue function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getValue function succeeded: value=' + value);
callback(null, value);
}
}.bind(this));
},
setBrightness: function (level, callback) {
var url = this.control_url + level;
this.log(this.name + ": Setting brightness to %s", level);
var that=this;
// Assume this delay is used to counter the HomeKit sending a full ON before the brightness level?
setTimeout(function() {
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setBrightness function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setBrightness function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
}.bind(this), 300);
},
setRotationSpeed: function (rotationSpeed, callback) {
var url = this.control_url + rotationSpeed;
this.log(this.name + ": Setting rotation speed to %s", rotationSpeed);
var that=this;
// Assume this delay is used to counter the HomeKit sending a full ON before the speed?
setTimeout(function() {
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setRotationSpeed function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setRotationSpeed function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
}.bind(this), 300);
},
setTemperature: function (temperature, callback) {
this.log(this.name + ": Setting temperature to %s", temperature);
if (this.config.temperatureUnit == "F") {
temperature = temperature * 9 / 5 + 32;
}
var url = this.control_url + temperature;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setTemperature function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setTemperature function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getTemperature: function (callback) {
var url = this.status_url;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getTemperature function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getTemperature function succeeded: value=' + value);
if (this.config.temperatureUnit == "F") {
value = (value - 32) * 5 / 9;
}
callback(null, value);
}
}.bind(this));
},
getThermostatCurrentHeatingCoolingState: function (callback) {
var ref = this.config.stateRef;
var url = this.access_url + "request=getstatus&ref=" + ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getThermostatCurrentHeatingCoolingState function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getThermostatCurrentHeatingCoolingState function succeeded: value=' + value);
if (this.config.stateOffValues.indexOf(value) != -1)
callback(null, 0);
else if (this.config.stateHeatValues.indexOf(value) != -1)
callback(null, 1);
else if (this.config.stateCoolValues.indexOf(value) != -1)
callback(null, 2);
else if (this.config.stateAutoValues.indexOf(value) != -1)
callback(null, 3);
else {
this.log(this.name + ': Error: value for thermostat current heating cooling state not in offValues, heatValues, coolValues or autoValues');
callback(null, 0);
}
}
}.bind(this));
},
setThermostatCurrentHeatingCoolingState: function (state, callback) {
this.log(this.name + ': Setting thermostat current heating cooling state to %s', state);
var ref = this.config.controlRef;
var value = 0;
if (state == 0)
value = this.config.controlOffValue;
else if (state == 1)
value = this.config.controlHeatValue;
else if (state == 2)
value = this.config.controlCoolValue;
else if (state == 3)
value = this.config.controlAutoValue;
var url = this.access_url + "request=controldevicebyvalue&ref=" + ref + "&value=" + value;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setThermostatCurrentHeatingCoolingState function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setThermostatCurrentHeatingCoolingState function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getThermostatTargetTemperature: function (callback) {
var ref = this.config.setPointRef;
var url = this.access_url + "request=getstatus&ref=" + ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getThermostatTargetTemperature function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getThermostatTargetTemperature function succeeded: value=' + value);
if (this.config.temperatureUnit == "F") {
value = (value - 32) * 5 / 9;
}
callback(null, value);
}
}.bind(this));
},
setThermostatTargetTemperature: function (temperature, callback) {
this.log(this.name + ': Setting thermostat target temperature to %s', temperature);
if (this.config.temperatureUnit == "F") {
temperature = temperature * 9 / 5 + 32;
}
var ref = this.config.setPointRef;
var url = this.access_url + "request=controldevicebyvalue&ref=" + ref + "&value=" + temperature;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setThermostatTargetTemperature function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setThermostatTargetTemperature function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getThermostatTemperatureDisplayUnits: function (callback) {
if (this.config.temperatureUnit == "F")
callback(null, 1);
else
callback(null, 0);
},
getLowBatteryStatus: function (callback) {
var ref = this.config.batteryRef;
var url = this.access_url + "request=getstatus&ref=" + ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getLowBatteryStatus function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
var minValue = 10;
this.log(this.name + ': getLowBatteryStatus function succeeded: value=' + value);
if (this.config.batteryThreshold) {
minValue = this.config.batteryThreshold;
}
if (value > minValue)
callback(null, 0);
else
callback(null, 1);
}
}.bind(this));
},
getCurrentDoorState: function (callback) {
var ref = this.config.stateRef;
var url = this.access_url + "request=getstatus&ref=" + ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getCurrentDoorState function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getCurrentDoorState function succeeded: value=' + value);
if (this.config.stateOpenValues.indexOf(value) != -1)
callback(null, Characteristic.CurrentDoorState.OPEN);
else if (this.config.stateClosedValues.indexOf(value) != -1)
callback(null, Characteristic.CurrentDoorState.CLOSED);
else if (this.config.stateOpeningValues && this.config.stateOpeningValues.indexOf(value) != -1)
callback(null, 2);
else if (this.config.stateClosingValues && this.config.stateClosingValues.indexOf(value) != -1)
callback(null, 3);
else if (this.config.stateStoppedValues && this.config.stateStoppedValues.indexOf(value) != -1)
callback(null, 4);
else {
this.log(this.name + ': Error: value for current door state not in stateO0penValues, stateClosedValues, stateOpeningValues, stateClosingValues, stateStoppedValues');
callback(null, 0);
}
}
}.bind(this));
},
setTargetDoorState: function (state, callback) {
this.log(this.name + ': Setting target door state state to %s', state);
var ref = this.config.controlRef;
var value = 0;
if (state == 0)
value = this.config.controlOpenValue;
else if (state == 1)
value = this.config.controlCloseValue;
var url = this.access_url + "request=controldevicebyvalue&ref=" + ref + "&value=" + value;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setTargetDoorState function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setTargetDoorState function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getObstructionDetected: function (callback) {
if (this.config.obstructionRef) {
var ref = this.config.obstructionRef;
var url = this.access_url + "request=getstatus&ref=" + ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getObstructionDetected function failed: %s', error.message);
callback(error, 0);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getObstructionDetected function succeeded: value=' + value);
if (this.config.obstructionValues && this.config.obstructionValues.indexOf(value) != -1)
callback(null, 1);
else {
callback(null, 0);
}
}
}.bind(this));
}
else {
callback(null, 0);
}
},
getLockCurrentState: function (callback) {
var ref = this.config.lockRef;
var url = this.access_url + "request=getstatus&ref=" + ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getLockCurrentState function failed: %s', error.message);
callback(error, 3);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getLockCurrentState function succeeded: value=' + value);
if (this.config.lockUnsecuredValues && this.config.lockUnsecuredValues.indexOf(value) != -1)
callback(null, 0);
else if (this.config.lockSecuredValues && this.config.lockSecuredValues.indexOf(value) != -1)
callback(null, 1);
else if (this.config.lockJammedValues && this.config.lockJammedValues.indexOf(value) != -1)
callback(null, 2);
else {
callback(null, 3);
}
}
}.bind(this));
},
setLockTargetState: function (state, callback) {
this.log(this.name + ': Setting target lock state to %s', state);
var ref = this.config.lockRef;
var value = 0;
if (state == 0 && this.config.unlockValue)
value = this.config.unlockValue;
else if (state == 1 && this.config.lockValue)
value = this.config.lockValue;
var url = this.access_url + "request=controldevicebyvalue&ref=" + ref + "&value=" + value;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setLockTargetState function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setLockTargetState function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getSecuritySystemCurrentState: function (callback) {
var url = this.access_url + "request=getstatus&ref=" + this.ref;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': getSecuritySystemCurrentState function failed: %s', error.message);
callback(error, 3);
}
else {
var status = JSON.parse(body);
var value = status.Devices[0].value;
this.log(this.name + ': getSecuritySystemCurrentState function succeeded: value=' + value);
if (this.config.armedStayValues && this.config.armedStayValues.indexOf(value) != -1)
callback(null, 0);
else if (this.config.armedAwayValues && this.config.armedAwayValues.indexOf(value) != -1)
callback(null, 1);
else if (this.config.armedNightValues && this.config.armedNightValues.indexOf(value) != -1)
callback(null, 2);
else if (this.config.disarmedValues && this.config.disarmedValues.indexOf(value) != -1)
callback(null, 3);
else if (this.config.alarmValues && this.config.alarmValues.indexOf(value) != -1)
callback(null, 4);
else
callback(null, 0);
}
}.bind(this));
},
setSecuritySystemTargetState: function (state, callback) {
this.log("Setting security system state to %s", state);
var value = 0;
if (state == 0 && this.config.armStayValue)
value = this.config.armStayValue;
else if (state == 1 && this.config.armAwayValue)
value = this.config.armAwayValue;
else if (state == 2 && this.config.armNightValue)
value = this.config.armNightValue;
else if (state == 3 && this.config.disarmValue)
value = this.config.disarmValue;
var url = this.access_url + "request=controldevicebyvalue&ref=" + this.ref + "&value=" + value;
httpRequest(url, 'GET', function (error, response, body) {
if (error) {
this.log(this.name + ': setSecuritySystemTargetState function failed: %s', error.message);
callback(error);
}
else {
this.log(this.name + ': setSecuritySystemTargetState function succeeded!');
callback();
}
}.bind(this));
//Poll for updated status
this.pollForUpdate();
},
getPositionState: function (callback) {
callback(null, 2); // Temporarily return STOPPED. TODO: full door support
},
getServices: function () {
var services = []
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "HomeSeer")
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, "HS " + this.config.type + " ref " + this.ref);
services.push(informationService);
switch (this.config.type) {
case "Lightbulb": {
//Better default
if (this.config.statusUpdateCount == null)
this.config.statusUpdateCount = 5;
var lightbulbService = new Service.Lightbulb();
lightbulbService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this))
.on('get', this.getPowerState.bind(this));
if (this.config.can_dim == null || this.config.can_dim == true) {
lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on('set', this.setBrightness.bind(this))
.on('get', this.getValue.bind(this));
}
this.statusCharacteristic = lightbulbService.getCharacteristic(Characteristic.On);
services.push(lightbulbService);
break;
}
case "Fan": {
var fanService = new Service.Fan();
fanService
.getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this))
.on('get', this.getPowerState.bind(this));
if (this.config.can_dim) {
fanService
.addCharacteristic(new Characteristic.RotationSpeed())
.on('set', this.setRotationSpeed.bind(this))
.on('get', this.getValue.bind(this));
}
this.statusCharacteristic = fanService.getCharacteristic(Characteristic.On);
services.push(fanService);
break;