forked from webdeck/homebridge-indigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1043 lines (902 loc) · 36.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
/*
Indigo platform shim for HomeBridge
Written by Mike Riccio (https://github.com/webdeck/homebridge-indigo)
See http://www.indigodomo.com/ for more info on Indigo
See http://forums.indigodomo.com/viewtopic.php?f=9&t=15008 for installation instructions
Configuration example for your Homebridge config.json:
"platforms": [
{
"platform": "Indigo",
"name": "My Indigo Server",
"protocol": "http",
"host": "127.0.0.1",
"port": "8176",
"path": "",
"username": "myusername",
"password": "mypassword",
"includeActions": true,
"consolidateDevices": true,
"includeIds": [ "12345", "67890" ],
"excludeIds": [ "98765", "43210" ],
"treatAsSwitchIds": [ "13579", "24680" ],
"treatAsLockIds": [ "112233", "445566" ],
"treatAsDoorIds": [ "224466", "664422" ],
"treatAsGarageDoorIds": [ "223344", "556677" ],
"thermostatsInCelsius": false,
"accessoryNamePrefix": ""
}
]
Fields:
"platform": Must always be "Indigo" (required)
"name": Can be anything (required)
"protocol": "http" or "https" (optional, defaults to "http" if not specified)
"host": Hostname or IP Address of your Indigo web server (required)
"port": Port number of your Indigo web server (optional, defaults to "8176" if not specified)
"path": The path to the root of your Indigo web server (optional, defaults to "" if not specified, only needed if you have a proxy in front of your Indigo web server)
"username": Username to log into Indigo web server, if applicable (optional)
"password": Password to log into Indigo web server, if applicable (optional)
"includeActions": If true, creates HomeKit switches for your actions (optional, defaults to false)
"consolidateDevices": If true, devices with the same device address will be seen in HomeKit as one accessory with multiple services (optional, defaults to false)
"includeIds": Array of Indigo IDs to include (optional - if provided, only these Indigo IDs will map to HomeKit devices)
"excludeIds": Array of Indigo IDs to exclude (optional - if provided, these Indigo IDs will not be mapped to HomeKit devices)
"treatAsSwitchIds": Array of Indigo IDs to treat as switches (instead of lightbulbs) - devices must support on/off to qualify
"treatAsLockIds": Array of Indigo IDs to treat as locks (instead of lightbulbs) - devices must support on/off to qualify (on = locked)
"treatAsDoorIds": Array of Indigo IDs to treat as doors (instead of lightbulbs) - devices must support on/off to qualify (on = open)
"treatAsGarageDoorIds": Array of Indigo IDs to treat as garage door openers (instead of lightbulbs) - devices must support on/off to qualify (on = open)
"thermostatsInCelsius": If true, thermostats in Indigo are reporting temperatures in celsius (optional, defaults to false)
"accessoryNamePrefix": Prefix all accessory names with this string (optional, useful for testing)
Note that if you specify both "includeIds" and "excludeIds", then only the IDs that are in
"includeIds" and missing from "excludeIds" will be mapped to HomeKit devices. Typically,
you would only specify one or the other, not both of these lists. If you just want to
expose everything, then omit both of these keys from your configuration.
Also note that any Indigo devices or actions that have Remote Display unchecked in Indigo
will NOT be exposed to HomeKit, because Indigo excludes those devices from its RESTful API.
*/
var request = require("request");
var async = require("async");
var inherits = require('util').inherits;
var Service, Characteristic, Accessory, uuid;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Accessory = homebridge.hap.Accessory;
uuid = homebridge.hap.uuid;
fixInheritance(IndigoAccessory, Accessory);
fixInheritance(IndigoSwitchAccessory, IndigoAccessory);
fixInheritance(IndigoLockAccessory, IndigoAccessory);
fixInheritance(IndigoDoorAccessory, IndigoAccessory);
fixInheritance(IndigoGarageDoorAccessory, IndigoAccessory);
fixInheritance(IndigoLightAccessory, IndigoAccessory);
fixInheritance(IndigoFanAccessory, IndigoAccessory);
fixInheritance(IndigoThermostatAccessory, IndigoAccessory);
fixInheritance(IndigoActionAccessory, IndigoAccessory);
homebridge.registerPlatform("homebridge-indigo", "Indigo", IndigoPlatform);
};
// Necessary because Accessory is defined after we have defined all of our classes
function fixInheritance(subclass, superclass) {
var proto = subclass.prototype;
inherits(subclass, superclass);
subclass.prototype.parent = superclass.prototype;
for (var mn in proto) {
subclass.prototype[mn] = proto[mn];
}
}
function IndigoPlatform(log, config) {
this.log = log;
var protocol = "http";
if (config.protocol) {
protocol = config.protocol;
}
var port = "8176";
if (config.port) {
port = config.port;
}
this.path = "";
if (config.path) {
this.path = config.path;
// Make sure path doesn't end with a slash
if (this.path.length > 0 && this.path.charAt(this.path.length -1) == '/') {
this.path = this.path.substr(0, this.path.length - 1);
}
// Make sure path begins with a slash
if (this.path.length > 0 && this.path.charAt(0) != "/") {
this.path = "/" + this.path;
}
this.log("Path prefix is %s", this.path);
}
this.baseURL = protocol + "://" + config.host + ":" + port;
this.log("Indigo base URL is %s", this.baseURL);
if (config.username && config.password) {
this.auth = {
user: config.username,
pass: config.password,
sendImmediately: false
};
}
this.includeActions = config.includeActions;
this.includeIds = config.includeIds;
this.excludeIds = config.excludeIds;
this.treatAsSwitchIds = config.treatAsSwitchIds;
this.treatAsLockIds = config.treatAsLockIds;
this.treatAsDoorIds = config.treatAsDoorIds;
this.treatAsGarageDoorIds = config.treatAsGarageDoorIds;
this.thermostatsInCelsius = config.thermostatsInCelsius;
if (config.accessoryNamePrefix) {
this.accessoryNamePrefix = config.accessoryNamePrefix;
} else {
this.accessoryNamePrefix = "";
}
}
// Invokes callback(accessories[])
IndigoPlatform.prototype.accessories = function(callback) {
this.foundAccessories = [];
var requestURLs = [ this.path + "/devices.json/" ];
if (this.includeActions) {
requestURLs.push(this.path + "/actions.json/");
}
async.eachSeries(requestURLs,
function(requestURL, asyncCallback) {
this.discoverAccessories(requestURL, asyncCallback);
}.bind(this),
function (asyncError) {
if (asyncError) {
this.log(asyncError);
}
if (this.foundAccessories.length > 99) {
this.log("*** WARNING *** you have %s accessories.",
this.foundAccessories.length);
this.log("*** Limiting to the first 99 discovered. ***");
this.log("*** See README.md for how to filter your list. ***");
this.foundAccessories = this.foundAccessories.slice(0, 99);
}
this.log("Created %s accessories", this.foundAccessories.length);
callback(this.foundAccessories.sort(
function (a, b) {
return (a.name > b.name) - (a.name < b.name);
}
));
}.bind(this)
);
};
// Invokes callback(error), error is undefined if no error occurred
IndigoPlatform.prototype.discoverAccessories = function(requestURL, callback) {
this.indigoRequestJSON(requestURL, "GET", null,
function(error, json) {
if (error) {
callback(error);
}
else {
async.eachSeries(json, this.addAccessory.bind(this),
function(asyncError) {
if (asyncError) {
callback(asyncError);
} else {
callback();
}
}
);
}
}.bind(this),
// jsonFixer: Indigo has a bug that if the first item has remote display
// disabled, the returned JSON array has an extra comma at the beginning
function(body) {
var firstComma = body.indexOf(",");
if (firstComma > 0 && firstComma < 5) {
body = body.substr(0, firstComma) + body.substr(firstComma + 1);
}
return (body);
}
);
};
// Invokes callback(error), error is always undefined as we want to ignore errors
IndigoPlatform.prototype.addAccessory = function(item, callback) {
this.indigoRequestJSON(item.restURL, "GET", null,
function(error, json) {
if (error) {
this.log("Ignoring accessory %s due to error", item.restURL);
callback();
}
else {
if (json.restParent == "actions") {
json.type = "Action";
}
this.log("Discovered %s (ID %s): %s", json.type, json.id, json.name);
if (this.includeItemId(json.id)) {
var accessory = this.createAccessoryFromJSON(item.restURL, json);
if (accessory) {
var deviceLinked = false;
if (this.consolidateDevices) {
for (var key in this.foundAccessories) {
if (this.foundAccessories.hasOwnProperty(key)) {
it = this.foundAccessories[key];
if (accessory.addressStr != "unknown" && accessory.addressStr === it.addressStr) {
this.log("Consolidating: " + accessory.name + " Link to: " + it.name);
it.linkedDevices.push(accessory);
var deviceLinked = true;
break;
}
}
}
}
if (deviceLinked === false) {
this.foundAccessories.push(accessory);
}
} else {
this.log("Ignoring unknown accessory type %s", json.type);
}
}
else {
this.log("Ignoring excluded ID %s", json.id);
}
callback();
}
}.bind(this)
);
};
// Returns true if the item id should be included in the accessory list
IndigoPlatform.prototype.includeItemId = function(id) {
if (this.includeIds && (this.includeIds.indexOf(String(id)) < 0)) {
return false;
}
if (this.excludeIds && (this.excludeIds.indexOf(String(id)) >= 0)) {
return false;
}
return true;
};
// Invokes callback(error, response, body) with result of HTTP request
IndigoPlatform.prototype.indigoRequest = function(path, method, qs, callback) {
// seems to be a bug in request that if followRedirect is false and auth is
// required, it crashes because redirects is missing, so I include it here
var options = {
url: this.baseURL + path,
method: method,
followRedirect: false,
redirects: []
};
if (this.auth) {
options.auth = this.auth;
}
if (qs) {
options.qs = qs;
}
this.log("Indigo request: %s", this.baseURL + path);
request(options, callback);
};
// Invokes callback(error, json) with JSON object returned by HTTP request
IndigoPlatform.prototype.indigoRequestJSON = function(path, method, qs, callback, jsonFixer) {
this.indigoRequest(path, method, qs,
function(error, response, body) {
if (error) {
var msg = "Error for Indigo request " + path + ": " + error;
this.log(msg);
callback(msg);
}
else {
if (jsonFixer) {
body = jsonFixer(body);
}
var json;
try {
var json = JSON.parse(body);
} catch (e) {
var msg2 = "Error parsing Indigo response for " + path +
"\nException: " + e + "\nResponse: " + body;
this.log(msg2);
callback(msg2);
return;
}
callback(undefined, json);
}
}.bind(this)
);
};
// Returns subclass of IndigoAccessory based on json, or null if unsupported type
IndigoPlatform.prototype.createAccessoryFromJSON = function(deviceURL, json) {
if (json.restParent == "actions") {
return new IndigoActionAccessory(this, deviceURL, json);
} else if (json.typeSupportsOnOff && this.treatAsSwitchIds &&
(this.treatAsSwitchIds.indexOf(String(json.id)) >= 0)) {
return new IndigoSwitchAccessory(this, deviceURL, json);
} else if (json.typeSupportsOnOff && this.treatAsLockIds &&
(this.treatAsLockIds.indexOf(String(json.id)) >= 0)) {
return new IndigoLockAccessory(this, deviceURL, json);
} else if (json.typeSupportsOnOff && this.treatAsDoorIds &&
(this.treatAsDoorIds.indexOf(String(json.id)) >= 0)) {
return new IndigoDoorAccessory(this, deviceURL, json);
} else if (json.typeSupportsOnOff && this.treatAsGarageDoorIds &&
(this.treatAsGarageDoorIds.indexOf(String(json.id)) >= 0)) {
return new IndigoGarageDoorAccessory(this, deviceURL, json);
} else if (json.typeSupportsHVAC) {
return new IndigoThermostatAccessory(this, deviceURL, json, this.thermostatsInCelsius);
} else if (json.typeSupportsSpeedControl) {
return new IndigoFanAccessory(this, deviceURL, json);
} else if (json.typeSupportsDim || json.typeSupportsOnOff) {
return new IndigoLightAccessory(this, deviceURL, json);
} else {
return null;
}
};
//
// Generic Indigo Accessory
//
function IndigoAccessory(platform, deviceURL, json) {
this.platform = platform;
this.log = platform.log;
this.deviceURL = deviceURL;
this.linkedDevices = [];
this.updateFromJSON(json);
Accessory.call(this, this.name, uuid.generate(String(this.id)));
var s = this.getService(Service.AccessoryInformation);
s.setCharacteristic(Characteristic.Manufacturer, "Indigo")
.setCharacteristic(Characteristic.SerialNumber, String(this.id));
if (this.type) {
s.setCharacteristic(Characteristic.Model, this.type);
}
if (this.versByte) {
s.setCharacteristic(Characteristic.FirmwareRevision, this.versByte);
}
}
IndigoAccessory.prototype.getServices = function() {
var services = this.services;
if (this.linkedDevices.length > 0) {
for (var key in this.linkedDevices) {
if (this.linkedDevices.hasOwnProperty(key)) {
it = this.linkedDevices[key];
services = services.concat(it.getServices().slice(1));
}
}
}
return services;
};
// Updates object fields with values from json
IndigoAccessory.prototype.updateFromJSON = function(json) {
for (var prop in json) {
if (json.hasOwnProperty(prop)) {
this[prop] = json[prop];
}
}
if (json.name !== undefined) {
this.name = this.platform.accessoryNamePrefix + String(json.name);
}
};
// Invokes callback(error), error is undefined if no error occurred
IndigoAccessory.prototype.getStatus = function(callback) {
this.platform.indigoRequestJSON(this.deviceURL, "GET", null,
function(error, json) {
if (error) {
callback(error);
} else {
this.updateFromJSON(json);
callback();
}
}.bind(this)
);
};
// Invokes callback(error), error is undefined if no error occurred
IndigoAccessory.prototype.updateStatus = function(qs, callback) {
this.log("updateStatus of %s: %s", this.name, JSON.stringify(qs));
this.platform.indigoRequest(this.deviceURL, "PUT", qs,
function(error, response, body) {
if (error) {
callback(error);
} else {
callback();
}
}.bind(this)
);
};
// Invokes callback(error, value), error is undefined if no error occurred
IndigoAccessory.prototype.query = function(key, callback) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
this.log("%s: query(%s) => %s", this.name, key, this[key]);
callback(undefined, this[key]);
}
}.bind(this)
);
};
IndigoAccessory.prototype.getOnState = function(callback) {
if (this.typeSupportsOnOff) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var onState = (this.isOn) ? true : false;
this.log("%s: getOnState() => %s", this.name, onState);
callback(undefined, onState);
}
}.bind(this)
);
}
};
IndigoAccessory.prototype.setOnState = function(onState, callback) {
this.log("%s: setOnState(%s)", this.name, onState);
if (this.typeSupportsOnOff) {
this.updateStatus({ isOn: (onState) ? 1 : 0 }, callback);
} else {
callback("Accessory does not support on/off");
}
};
//
// Indigo Switch Accessory
//
function IndigoSwitchAccessory(platform, deviceURL, json) {
IndigoAccessory.call(this, platform, deviceURL, json);
var s = this.addService(Service.Switch);
s.getCharacteristic(Characteristic.On)
.on('get', this.getOnState.bind(this))
.on('set', this.setOnState.bind(this));
}
//
// Indigo Lock Accessory
//
function IndigoLockAccessory(platform, deviceURL, json) {
IndigoAccessory.call(this, platform, deviceURL, json);
var s = this.addService(Service.LockMechanism);
s.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getLockCurrentState.bind(this));
s.getCharacteristic(Characteristic.LockTargetState)
.on('get', this.getLockTargetState.bind(this))
.on('set', this.setLockTargetState.bind(this));
}
IndigoLockAccessory.prototype.getLockCurrentState = function(callback) {
if (this.typeSupportsOnOff) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var lockState = (this.isOn) ? Characteristic.LockCurrentState.SECURED : Characteristic.LockCurrentState.UNSECURED;
this.log("%s: getLockCurrentState() => %s", this.name, lockState);
callback(undefined, lockState);
}
}.bind(this)
);
}
};
IndigoLockAccessory.prototype.getLockTargetState = function(callback) {
if (this.typeSupportsOnOff) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var lockState = (this.isOn) ? Characteristic.LockTargetState.SECURED : Characteristic.LockTargetState.UNSECURED;
this.log("%s: getLockTargetState() => %s", this.name, lockState);
callback(undefined, lockState);
}
}.bind(this)
);
}
};
IndigoLockAccessory.prototype.setLockTargetState = function(lockState, callback) {
this.log("%s: setLockTargetState(%s)", this.name, lockState);
if (this.typeSupportsOnOff) {
this.updateStatus({ isOn: (lockState == Characteristic.LockTargetState.SECURED) ? 1 : 0 }, callback);
} else {
callback("Accessory does not support on/off");
}
};
//
// Indigo Door Accessory
//
function IndigoDoorAccessory(platform, deviceURL, json) {
IndigoAccessory.call(this, platform, deviceURL, json);
var s = this.addService(Service.Door);
s.getCharacteristic(Characteristic.CurrentPosition)
.on('get', this.getPosition.bind(this));
s.getCharacteristic(Characteristic.PositionState)
.on('get', this.getPositionState.bind(this));
s.getCharacteristic(Characteristic.TargetPosition)
.on('get', this.getPosition.bind(this))
.on('set', this.setTargetPosition.bind(this));
}
IndigoDoorAccessory.prototype.getPosition = function(callback) {
if (this.typeSupportsOnOff) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var position = (this.isOn) ? 100 : 0;
this.log("%s: getPosition() => %s", this.name, position);
callback(undefined, position);
}
}.bind(this)
);
}
};
IndigoDoorAccessory.prototype.getPositionState = function(callback) {
if (this.typeSupportsOnOff) {
this.log("%s: getPositionState() => %s", this.name, Characteristic.PositionState.STOPPED);
callback(undefined, Characteristic.PositionState.STOPPED);
}
};
IndigoDoorAccessory.prototype.setTargetPosition = function(position, callback) {
this.log("%s: setTargetPosition(%s)", this.name, position);
if (this.typeSupportsOnOff) {
this.updateStatus({ isOn: (position > 0) ? 1 : 0 }, callback);
} else {
callback("Accessory does not support on/off");
}
};
//
// Indigo Garage Door Accessory
//
function IndigoGarageDoorAccessory(platform, deviceURL, json) {
IndigoAccessory.call(this, platform, deviceURL, json);
var s = this.addService(Service.GarageDoorOpener);
s.getCharacteristic(Characteristic.CurrentDoorState)
.on('get', this.getCurrentDoorState.bind(this));
s.getCharacteristic(Characteristic.TargetDoorState)
.on('get', this.getTargetDoorState.bind(this))
.on('set', this.setTargetDoorState.bind(this));
s.getCharacteristic(Characteristic.ObstructionDetected)
.on('get', this.getObstructionDetected.bind(this));
}
IndigoGarageDoorAccessory.prototype.getCurrentDoorState = function(callback) {
if (this.typeSupportsOnOff) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var doorState = (this.isOn) ? Characteristic.CurrentDoorState.OPEN : Characteristic.CurrentDoorState.CLOSED;
this.log("%s: getPosition() => %s", this.name, doorState);
callback(undefined, doorState);
}
}.bind(this)
);
}
};
IndigoGarageDoorAccessory.prototype.getTargetDoorState = function(callback) {
if (this.typeSupportsOnOff) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var doorState = (this.isOn) ? Characteristic.TargetDoorState.OPEN : Characteristic.TargetDoorState.CLOSED;
this.log("%s: getPosition() => %s", this.name, doorState);
callback(undefined, doorState);
}
}.bind(this)
);
}
};
IndigoGarageDoorAccessory.prototype.setTargetDoorState = function(doorState, callback) {
this.log("%s: setTargetPosition(%s)", this.name, doorState);
if (this.typeSupportsOnOff) {
this.updateStatus({ isOn: (doorState == Characteristic.TargetDoorState.OPEN) ? 1 : 0 }, callback);
} else {
callback("Accessory does not support on/off");
}
};
IndigoGarageDoorAccessory.prototype.getObstructionDetected = function(callback) {
if (this.typeSupportsOnOff) {
this.log("%s: getObstructionDetected() => %s", this.name, false);
callback(undefined, false);
}
};
//
// Indigo Light Accessory
//
function IndigoLightAccessory(platform, deviceURL, json) {
IndigoAccessory.call(this, platform, deviceURL, json);
var s = this.addService(Service.Lightbulb);
s.getCharacteristic(Characteristic.On)
.on('get', this.getOnState.bind(this))
.on('set', this.setOnState.bind(this));
if (this.typeSupportsDim) {
s.getCharacteristic(Characteristic.Brightness)
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
}
}
IndigoLightAccessory.prototype.getBrightness = function(callback) {
if (this.typeSupportsDim) {
this.query("brightness", callback);
} else {
callback("Accessory does not support brightness");
}
};
IndigoLightAccessory.prototype.setBrightness = function(brightness, callback) {
if (this.typeSupportsDim && brightness >= 0 && brightness <= 100) {
this.updateStatus({ brightness: brightness }, callback);
} else {
callback("Accessory does not support brightness");
}
};
//
// Indigo Fan Accessory
//
function IndigoFanAccessory(platform, deviceURL, json) {
IndigoAccessory.call(this, platform, deviceURL, json);
var s = this.addService(Service.Fan);
s.getCharacteristic(Characteristic.On)
.on('get', this.getOnState.bind(this))
.on('set', this.setOnState.bind(this));
s.getCharacteristic(Characteristic.RotationSpeed)
.on('get', this.getRotationSpeed.bind(this))
.on('set', this.setRotationSpeed.bind(this));
}
IndigoFanAccessory.prototype.getRotationSpeed = function(callback) {
if (this.typeSupportsSpeedControl) {
this.query("speedIndex",
function(error, speedIndex) {
if (error) {
callback(error);
} else {
callback(undefined, (speedIndex / 3.0) * 100.0);
}
}
);
}
else {
callback("Accessory does not support rotation speed");
}
};
IndigoFanAccessory.prototype.setRotationSpeed = function(rotationSpeed, callback) {
if (this.typeSupportsSpeedControl && rotationSpeed >= 0.0 && rotationSpeed <= 100.0) {
var speedIndex = 0;
if (rotationSpeed > 66.6) {
speedIndex = 3;
} else if (rotationSpeed > 33.3) {
speedIndex = 2;
} else if (rotationSpeed > 0) {
speedIndex = 1;
}
this.updateStatus({ speedIndex: speedIndex }, callback);
}
else {
callback("Accessory does not support rotation speed");
}
};
//
// Indigo Thermostat Accessory
//
function IndigoThermostatAccessory(platform, deviceURL, json, thermostatsInCelsius) {
IndigoAccessory.call(this, platform, deviceURL, json);
this.thermostatsInCelsius = thermostatsInCelsius;
this.temperatureDisplayUnits = (thermostatsInCelsius) ?
Characteristic.TemperatureDisplayUnits.CELSIUS :
Characteristic.TemperatureDisplayUnits.FAHRENHEIT;
var s = this.addService(Service.Thermostat);
s.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentHeatingCooling.bind(this));
s.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetHeatingCooling.bind(this))
.on('set', this.setTargetHeatingCooling.bind(this));
s.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getCurrentTemperature.bind(this));
s.getCharacteristic(Characteristic.TargetTemperature)
.on('get', this.getTargetTemperature.bind(this))
.on('set', this.setTargetTemperature.bind(this));
s.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', this.getTemperatureDisplayUnits.bind(this))
.on('set', this.setTemperatureDisplayUnits.bind(this));
s.getCharacteristic(Characteristic.CoolingThresholdTemperature)
.on('get', this.getCoolingThresholdTemperature.bind(this))
.on('set', this.setCoolingThresholdTemperature.bind(this));
s.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.on('get', this.getHeatingThresholdTemperature.bind(this))
.on('set', this.setHeatingThresholdTemperature.bind(this));
if (this.displayHumidityInRemoteUI) {
s.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getCurrentRelativeHumidity.bind(this));
}
}
IndigoThermostatAccessory.prototype.getCurrentHeatingCooling = function(callback) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var mode = Characteristic.CurrentHeatingCoolingState.OFF;
if (this.hvacHeaterIsOn) {
mode = Characteristic.CurrentHeatingCoolingState.HEAT;
} else if (this.hvacCoolerIsOn) {
mode = Characteristic.CurrentHeatingCoolingState.COOL;
}
this.log("%s: getCurrentHeatingCooling() => %s", this.name, mode);
callback(undefined, mode);
}
}.bind(this)
);
};
IndigoThermostatAccessory.prototype.getTargetHeatingCooling = function(callback) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var mode = Characteristic.TargetHeatingCoolingState.OFF;
if (this.hvacOperationModeIsHeat || this.hvacOperationModeIsProgramHeat) {
mode = Characteristic.TargetHeatingCoolingState.HEAT;
} else if (this.hvacOperationModeIsCool || this.hvacOperationModeIsProgramCool) {
mode = Characteristic.TargetHeatingCoolingState.COOL;
} else if (this.hvacOperationModeIsAuto || this.hvacOperationModeIsProgramAuto) {
mode = Characteristic.TargetHeatingCoolingState.AUTO;
}
this.log("%s: getTargetHeatingCooling() => %s", this.name, mode);
callback(undefined, mode);
}
}.bind(this)
);
};
IndigoThermostatAccessory.prototype.setTargetHeatingCooling = function(mode, callback) {
this.log("%s: setTargetHeatingCooling(%s)", this.name, mode);
var qs;
if (mode == Characteristic.TargetHeatingCoolingState.OFF) {
qs = { hvacOperationModeIsOff: "true" };
}
else if (mode == Characteristic.TargetHeatingCoolingState.HEAT) {
qs = { hvacOperationModeIsHeat: "true" };
}
else if (mode == Characteristic.TargetHeatingCoolingState.COOL) {
qs = { hvacOperationModeIsCool: "true" };
}
else if (mode == Characteristic.TargetHeatingCoolingState.AUTO) {
qs = { hvacOperationModeIsAuto: "true" };
}
if (qs) {
this.updateStatus(qs, callback);
} else {
callback("Unknown target heating/cooling state");
}
};
// Note: HomeKit wants all temperature values in celsius, so convert if needed
IndigoThermostatAccessory.prototype.celsiusToIndigoTemp = function(temperature) {
if (this.thermostatsInCelsius) {
return (temperature);
} else {
return (Math.round(((temperature * 9.0 / 5.0) + 32.0) * 10.0) / 10.0);
}
}
IndigoThermostatAccessory.prototype.indigoTempToCelsius = function(temperature) {
if (this.thermostatsInCelsius) {
return (temperature);
} else {
return (Math.round(((temperature - 32.0) * 5.0 / 9.0) * 10.0) / 10.0);
}
}
IndigoThermostatAccessory.prototype.getTemperatureValue = function(key, callback) {
this.query(key,
function(error, temperature) {
if (error) {
callback(error);
} else {
var t = this.indigoTempToCelsius(temperature);
this.log("%s: getTemperatureValue(%s) => %s", this.name, key, t);
callback(undefined, t);
}
}.bind(this)
);
};
IndigoThermostatAccessory.prototype.setTemperatureValue = function(key, temperature, callback) {
this.log("%s: setTemperatureValue(%s, %s)", this.name, key, temperature);
var qs = { };
qs[key] = this.celsiusToIndigoTemp(temperature);
this.updateStatus(qs, callback);
};
IndigoThermostatAccessory.prototype.getCurrentTemperature = function(callback) {
this.getTemperatureValue("inputTemperatureVals", callback);
};
IndigoThermostatAccessory.prototype.getTargetTemperature = function(callback) {
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var temperature;
if (this.hvacOperationModeIsHeat || this.hvacOperationModeIsProgramHeat) {
temperature = this.setpointHeat;
}
else if (this.hvacOperationModeIsCool || this.hvacOperationModeIsProgramCool) {
temperature = this.setpointCool;
}
else {
temperature = (this.setpointHeat + this.setpointCool) / 2.0;
}
var t = this.indigoTempToCelsius(temperature);
this.log("%s: getTargetTemperature() => %s", this.name, t);
callback(undefined, t);
}
}.bind(this)
);
};
IndigoThermostatAccessory.prototype.setTargetTemperature = function(temperature, callback) {
this.log("%s: setTargetTemperature(%s)", this.name, temperature);
var t = this.celsiusToIndigoTemp(temperature);
this.getStatus(
function(error) {
if (error) {
callback(error);
} else {
var qs;
if (this.hvacOperationModeIsHeat) {
qs = { setpointHeat: t };
}
else if (this.hvacOperationModeIsCool) {
qs = { setpointCool: t };
}
else {
var adjust = (this.thermostatsInCelsius) ? 2 : 5;
qs = { setpointCool: t + adjust, setpointHeat: t - adjust };
}
this.updateStatus(qs, callback);
}
}.bind(this)
);
};
IndigoThermostatAccessory.prototype.getTemperatureDisplayUnits = function(callback) {
this.log("%s: getTemperatureDisplayUnits() => %s", this.name, this.temperatureDisplayUnits);
callback(undefined, this.temperatureDisplayUnits);
};
IndigoThermostatAccessory.prototype.setTemperatureDisplayUnits = function(units, callback) {
this.log("%s: setTemperatureDisplayUnits(%s)", this.name, units);
this.temperatureDisplayUnits = units;
callback();
};
IndigoThermostatAccessory.prototype.getCoolingThresholdTemperature = function(callback) {
this.getTemperatureValue("setpointCool", callback);
};
IndigoThermostatAccessory.prototype.setCoolingThresholdTemperature = function(temperature, callback) {
this.setTemperatureValue("setpointCool", temperature, callback);
};
IndigoThermostatAccessory.prototype.getHeatingThresholdTemperature = function(callback) {
this.getTemperatureValue("setpointHeat", callback);
};
IndigoThermostatAccessory.prototype.setHeatingThresholdTemperature = function(temperature, callback) {
this.setTemperatureValue("setpointHeat", temperature, callback);
};
IndigoThermostatAccessory.prototype.getCurrentRelativeHumidity = function(callback) {
if (this.displayHumidityInRemoteUI) {
this.query("inputHumidityVals", callback);
} else {
callback("Accessory does not support current relative humidity");
}
};
//
// Indigo Action Accessory