forked from rudders/homebridge-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
682 lines (592 loc) · 21.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
var Service, Characteristic;
var request = require("request");
var pollingtoevent = require("polling-to-event");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-http", "Http", HttpAccessory);
};
function HttpAccessory(log, config) {
this.log = log;
// accessory details
this.manufacturer = config["manufacturer"] || "HTTP Manufacturer";
this.model = config["model"] || "HTTP Model";
this.serial_number = config["serial_number"] || "HTTP Serial Number";
// define methods
this.name = config["name"];
this.http_method = config["http_method"] || "GET";
this.http_brightness_method = config["http_brightness_method"] || this.http_method;
this.http_hue_method = config["http_hue_method"] || this.http_method;
this.http_saturation_method = config["http_saturation_method"] || this.http_method;
// switch, light and non-switch handling accessories (sensors)
this.service = config["service"] || "Switch";
this.switchHandling = config["switchHandling"] || "no";
this.on_url = config["on_url"];
this.on_body = config["on_body"];
this.off_url = config["off_url"];
this.off_body = config["off_body"];
this.status_url = config["status_url"];
this.status_on = config["status_on"];
this.status_off = config["status_off"];
// brightness (light)
this.brightnessHandling = config["brightnessHandling"] || "no";
this.brightness_url = config["brightness_url"];
this.brightnesslvl_url = config["brightnesslvl_url"];
// color (light)
this.colorHandling = config["colorHandling"] || "no";
this.hue_url = config["hue_url"];
this.hueset_url = config["hueset_url"];
this.saturation_url = config["saturation_url"];
this.saturationset_url = config["saturationset_url"];
// additional
this.sendimmediately = config["sendimmediately"] || "";
this.username = config["username"] || "";
this.password = config["password"] || "";
// realtime polling info
this.state = false;
this.enableSet = true;
this.currentlevel = 0;
this.currenthue = 0;
this.currentsaturation = 0;
var that = this;
// Status Polling, if you want to add additional services that don't use switch handling you can add something like this || (this.service=="Smoke" || this.service=="Motion"))
if ((this.status_url && this.switchHandling === "realtime") || (this.status_url && this.service === "Smoke") || (this.status_url && this.service === "Motion") || (this.status_url && this.service === "Occupancy") || (this.status_url && this.service === "Leak") || (this.status_url && this.service === "StatelessProgrammableSwitch")) {
var powerurl = this.status_url;
var statusemitter = pollingtoevent(function(done) {
that.httpRequest(powerurl, "", "GET", that.username, that.password, that.sendimmediately, function(error, response, body) {
if (error) {
that.log("HTTP get power function failed: %s", error.message);
try {
done(new Error("Network failure that must not stop homebridge!"));
} catch (err) {
that.log(err.message);
}
} else {
done(null, body);
}
})
}, {longpolling: true, interval: 300, longpollEventName: "statuspoll"});
function compareStates(customStatus, stateData) {
var objectsEqual = true;
for (var param in customStatus) {
if (!stateData.hasOwnProperty(param) || customStatus[param] !== stateData[param]) {
objectsEqual = false;
break;
}
}
// that.log("Equal", objectsEqual);
return objectsEqual;
}
statusemitter.on("statuspoll", function(responseBody) {
var binaryState;
if (that.status_on && that.status_off) {//Check if custom status checks are set
var customStatusOn = that.status_on;
var customStatusOff = that.status_off;
var statusOn, statusOff;
// Check to see if custom states are a json object and if so compare to see if either one matches the state response
if (responseBody.startsWith("{")) {
statusOn = compareStates(customStatusOn, JSON.parse(responseBody));
statusOff = compareStates(customStatusOff, JSON.parse(responseBody));
} else {
statusOn = responseBody.includes(customStatusOn);
statusOff = responseBody.includes(customStatusOff);
}
that.log("Status On Status Poll", statusOn);
if (statusOn) binaryState = 1;
//else binaryState = 0;
if (statusOff) binaryState = 0;
} else {
binaryState = parseInt(responseBody.replace(/\D/g, ""));
}
that.state = binaryState > 0;
that.log(that.service, "received power", that.status_url, "state is currently", binaryState);
// switch used to easily add additonal services
that.enableSet = false;
switch (that.service) {
case "Switch":
if (that.switchService) {
that.switchService
.getCharacteristic(Characteristic.On)
.setValue(that.state);
}
break;
case "Light":
if (that.lightbulbService) {
that.lightbulbService
.getCharacteristic(Characteristic.On)
.setValue(that.state);
}
break;
case "Smoke":
if (that.smokeService) {
that.smokeService
.getCharacteristic(Characteristic.SmokeDetected)
.setValue(that.state);
}
break;
case "Motion":
if (that.motionService) {
that.motionService
.getCharacteristic(Characteristic.MotionDetected)
.setValue(that.state);
}
break;
case "Occupancy":
if (that.motionService) {
that.motionService
.getCharacteristic(Characteristic.OccupancyDetected)
.setValue(that.state);
}
break;
}
that.enableSet = true;
});
}
// Brightness Polling
if (this.brightnesslvl_url && this.brightnessHandling === "realtime") {
var brightnessurl = this.brightnesslvl_url;
var levelemitter = pollingtoevent(function(done) {
that.httpRequest(brightnessurl, "", "GET", that.username, that.password, that.sendimmediately, function(error, response, responseBody) {
if (error) {
that.log("HTTP get power function failed: %s", error.message);
return;
} else {
done(null, responseBody);
}
})// set longer polling as slider takes longer to set value
}, { longpolling: true, interval: 300, longpollEventName: "levelpoll" });
levelemitter.on("levelpoll", function(responseBody) {
that.currentlevel = parseInt(responseBody);
that.enableSet = false;
if (that.lightbulbService) {
that.log(that.service, "received brightness", that.brightnesslvl_url, "level is currently", that.currentlevel);
that.lightbulbService
.getCharacteristic(Characteristic.Brightness)
.setValue(that.currentlevel);
}
that.enableSet = true;
});
}
// Hue Polling
if (this.hue_url && this.colorHandling === "realtime") {
var hueurl = this.hue_url;
var levelemitter = pollingtoevent(function(done) {
that.httpRequest(hueurl, "", "GET", that.username, that.password, that.sendimmediately, function(error, response, responseBody) {
if (error) {
that.log('HTTP get hue function failed: %s', error.message);
return;
} else {
done(null, responseBody);
}
}) // set longer polling as slider takes longer to set value
}, {longpolling: true, interval: 2000, longpollEventName: "huepoll"});
levelemitter.on("huepoll", function(data) {
that.currenthue = parseInt(data);
if (that.lightbulbService) {
that.log(that.service, "received hue",that.hue_url, "level is currently", that.currenthue);
that.lightbulbService
.getCharacteristic(Characteristic.Hue)
.setValue(that.currenthue);
}
});
}
// Saturation Polling
if (this.saturation_url && this.colorHandling === "realtime") {
var saturationurl = this.saturation_url;
var levelemitter = pollingtoevent(function(done) {
that.httpRequest(saturationurl, "", "GET", that.username, that.password, that.sendimmediately, function(error, response, responseBody) {
if (error) {
that.log('HTTP get saturation function failed: %s', error.message);
return;
} else {
done(null, responseBody);
}
}) // set longer polling as slider takes longer to set value
}, {longpolling: true, interval: 2000, longpollEventName: "saturationpoll"});
levelemitter.on("saturationpoll", function(data) {
that.currentsaturation = parseInt(data);
if (that.lightbulbService) {
that.log(that.service, "received saturation",that.hue_url, "level is currently", that.currentsaturation);
that.lightbulbService
.getCharacteristic(Characteristic.Saturation)
.setValue(that.currentsaturation);
}
});
}
}
HttpAccessory.prototype = {
httpRequest: function(url, body, method, username, password, sendimmediately, callback) {
request({
url: url,
body: body,
method: method,
rejectUnauthorized: false,
auth: {
user: username,
pass: password,
sendImmediately: sendimmediately
}
},
function(error, response, body) {
callback(error, response, body)
})
},
// for switch & sensor (smoke, motion, occupancy)
getPowerState: function(callback) {
if (!this.status_url) {
this.log.warn("Ignoring request; No status url defined.");
callback(new Error("No status url defined."));
return;
}
var url = this.status_url;
this.log("Getting power state");
this.httpRequest(url, "", "GET", this.username, this.password, this.sendimmediately, function (error, response, responseBody) {
if (error) {
this.log("HTTP get power function failed: %s", error.message);
callback(error);
} else {
var binaryState;
this.log("Status Config On", this.status_on);
if (this.status_on && this.status_off) { //Check if custom status checks are set
var customStatusOn = that.status_on;
var customStatusOff = that.status_off;
var statusOn, statusOff;
// Check to see if custom states are a json object and if so compare to see if either one matches the state response
if (responseBody.startsWith("{")) {
statusOn = compareStates(customStatusOn, JSON.parse(responseBody));
statusOff = compareStates(customStatusOff, JSON.parse(responseBody));
} else {
statusOn = responseBody.includes(customStatusOn);
statusOff = responseBody.includes(customStatusOff);
}
that.log("Status On Get Power State", statusOn);
if (statusOn) binaryState = 1;
// else binaryState = 0;
if (statusOff) binaryState = 0;
} else {
binaryState = parseInt(responseBody.replace(/\D/g, ""));
}
var powerOn = binaryState > 0;
this.log("Power state is currently %s", binaryState);
callback(null, powerOn);
}
}.bind(this));
},
setPowerState: function(powerState, callback) {
this.log("Power On", powerState);
this.log("Enable Set", this.enableSet);
this.log("Current Level", this.currentlevel);
if (this.enableSet === true) {
var url;
var body;
if (!this.on_url || !this.off_url) {
this.log.warn("Ignoring request; No power url defined.");
callback(new Error("No power url defined."));
return;
}
if (powerState) {
url = this.on_url;
body = this.on_body;
this.log("Setting power state to on");
} else {
url = this.off_url;
body = this.off_body;
this.log("Setting power state to off");
}
this.httpRequest(url, body, this.http_method, this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
if (error) {
this.log("HTTP set power function failed: %s", error.message);
callback(error);
} else {
this.log("HTTP set power function succeeded!");
callback();
}
}.bind(this));
} else {
callback();
}
},
// for light (brightness, hue, saturation)
getBrightness: function(callback) {
if (!this.brightnesslvl_url) {
this.log.warn("Ignoring request; No brightness level url defined.");
callback(new Error("No brightness level url defined."));
return;
}
var url = this.brightnesslvl_url;
this.log("Getting Brightness level");
this.httpRequest(url, "", "GET", this.username, this.password, this.sendimmediately, function (error, response, responseBody) {
if (error) {
this.log("HTTP get brightness function failed: %s", error.message);
callback(error);
} else {
var binaryState = parseInt(responseBody.replace(/\D/g, ""));
var level = binaryState;
this.log("brightness state is currently %s", binaryState);
callback(null, level);
}
}.bind(this));
},
setBrightness: function(level, callback) {
if (this.enableSet === true) {
if (!this.brightness_url) {
this.log.warn("Ignoring request; No brightness url defined.");
callback(new Error("No brightness url defined."));
return;
}
var url = this.brightness_url.replace("%b", level);
this.log("Setting brightness to %s", level);
this.httpRequest(url, "", this.http_brightness_method, this.username, this.password, this.sendimmediately, function (error, response, body) {
if (error) {
this.log("HTTP brightness function failed: %s", error);
callback(error);
} else {
this.log("HTTP brightness function succeeded!");
callback();
}
}.bind(this));
} else {
callback();
}
},
getHue: function(callback) {
if (!this.hue_url) {
this.log.warn("Ignoring request; No hue level url defined.");
callback(new Error("No hue level url defined."));
return;
}
var url = this.hue_url;
this.log("Getting Hue level");
this.httpRequest(url, "", "GET", this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
if (error) {
this.log('HTTP get hue function failed: %s', error.message);
callback(error);
} else {
var binaryState = parseInt(responseBody);
var level = binaryState;
this.log("hue state is currently %s", binaryState);
callback(null, level);
}
}.bind(this));
},
setHue: function(level, callback) {
if (!this.hueset_url) {
this.log.warn("Ignoring request; No hue url defined.");
callback(new Error("No hue url defined."));
return;
}
var url = this.hueset_url.replace("%b", level)
this.log("Setting hue to %s", level);
this.httpRequest(url, "", this.http_hue_method, this.username, this.password, this.sendimmediately, function(error, response, body) {
if (error) {
this.log('HTTP hue function failed: %s', error);
callback(error);
} else {
this.log('HTTP hue function succeeded!');
callback();
}
}.bind(this));
},
getSaturation: function(callback) {
if (!this.saturation_url) {
this.log.warn("Ignoring request; No hue level url defined.");
callback(new Error("No saturation level url defined."));
return;
}
var url = this.saturation_url;
this.log("Getting Saturation level");
this.httpRequest(url, "", "GET", this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
if (error) {
this.log('HTTP get saturation function failed: %s', error.message);
callback(error);
} else {
var binaryState = parseInt(responseBody);
var level = binaryState;
this.log("saturation state is currently %s", binaryState);
callback(null, level);
}
}.bind(this));
},
setSaturation: function(level, callback) {
if (!this.saturationset_url) {
this.log.warn("Ignoring request; No saturation url defined.");
callback(new Error("No saturation url defined."));
return;
}
var url = this.saturationset_url.replace("%b", level)
this.log("Setting saturation to %s", level);
this.httpRequest(url, "", this.http_saturation_method, this.username, this.password, this.sendimmediately, function(error, response, body) {
if (error) {
this.log('HTTP saturation function failed: %s', error);
callback(error);
} else {
this.log('HTTP saturation function succeeded!');
callback();
}
}.bind(this));
},
identify: function(callback) {
this.log("Identify requested!");
callback(); //success
},
getServices: function() {
var that = this;
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial_number);
switch (this.service) {
case "Switch":
this.switchService = new Service.Switch(this.name);
switch (this.switchHandling) {
//Power Polling
case "yes":
this.switchService
.getCharacteristic(Characteristic.On)
.on("get", this.getPowerState.bind(this))
.on("set", this.setPowerState.bind(this));
break;
case "realtime":
this.switchService
.getCharacteristic(Characteristic.On)
.on("get", function (callback) {callback(null, that.state)})
.on("set", this.setPowerState.bind(this));
break;
default:
this.switchService
.getCharacteristic(Characteristic.On)
.on("set", this.setPowerState.bind(this));
break;
}
return [this.switchService];
break;
case "Light":
this.lightbulbService = new Service.Lightbulb(this.name);
switch (this.switchHandling) {
//Power Polling
case "yes":
this.lightbulbService
.getCharacteristic(Characteristic.On)
.on("get", this.getPowerState.bind(this))
.on("set", this.setPowerState.bind(this));
break;
case "realtime":
this.lightbulbService
.getCharacteristic(Characteristic.On)
.on("get", function(callback) {callback(null, that.state)})
.on("set", this.setPowerState.bind(this));
break;
default:
this.lightbulbService
.getCharacteristic(Characteristic.On)
.on("set", this.setPowerState.bind(this));
break;
}
// Brightness Polling
switch (this.brightnessHandling) {
case "yes":
this.lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on("get", this.getBrightness.bind(this))
.on("set", this.setBrightness.bind(this));
break;
case "realtime":
this.lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on("get", function(callback) {callback(null, that.currentlevel)})
.on("set", this.setBrightness.bind(this));
break;
}
// Color Polling
switch (this.colorHandling) {
case "yes":
this.lightbulbService
.addCharacteristic(new Characteristic.Hue())
.on('get', this.getHue.bind(this))
.on('set', this.setHue.bind(this));
this.lightbulbService
.addCharacteristic(new Characteristic.Saturation())
.on('get', this.getSaturation.bind(this))
.on('set', this.setSaturation.bind(this));
break;
case "realtime":
this.lightbulbService
.addCharacteristic(new Characteristic.Hue())
.on('get', function(callback) {callback(null, that.currenthue)})
.on('set', this.setHue.bind(this));
this.lightbulbService
.addCharacteristic(new Characteristic.Saturation())
.on('get', function(callback) {callback(null, that.currentsaturation)})
.on('set', this.setSaturation.bind(this));
break;
}
return [informationService, this.lightbulbService];
break;
case "Smoke":
this.smokeService = new Service.SmokeSensor(this.name);
this.switchHandling === "realtime";
this.smokeService
.getCharacteristic(Characteristic.SmokeDetected)
.on('get', function(callback) {callback(null, that.state)});
return [this.smokeService];
break;
case "Motion":
this.motionService = new Service.MotionSensor(this.name);
this.switchHandling === "realtime";
this.motionService
.getCharacteristic(Characteristic.MotionDetected)
.on('get', function(callback) {callback(null, that.state)});
return [this.motionService];
break;
case "Occupancy":
this.occupancyService = new Service.OccupancySensor(this.name);
this.switchHandling === "realtime";
this.occupancyService
.getCharacteristic(Characteristic.OccupancyDetected)
.on('get', function(callback) {callback(null, that.state)});
return [this.occupancyService];
break;
case "Leak":
this.leakService = new Service.LeakSensor(this.name);
this.switchHandling === "realtime";
this.leakService
.getCharacteristic(Characteristic.LeakDetected)
.on('get', function(callback) {callback(null, that.state)});
return [this.leakService];
break;
case "StatelessProgrammableSwitch":
this.statelessProgrammableSwitchService = new Service.StatelessProgrammableSwitch(this.name);
/*this.switchHandling === "realtime";
this.statelessProgrammableSwitchService
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.on('get', function(callback) {callback(null, that.state)});*/
switch (this.switchHandling) {
case "yes":
this.statelessProgrammableSwitchService
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.on("get", this.getPowerState.bind(this))
.on("set", this.setPowerState.bind(this));
break;
case "realtime":
this.statelessProgrammableSwitchService
.getCharacteristic(Characteristic.ProgrammableSwitchEvent)
.on('get', function(callback) {callback(null, that.state)})
.on("set", this.setPowerState.bind(this));
break;
}
return [this.statelessProgrammableSwitchService];
break;
/*case "Obstruction":
this.obstructionService = new Service.ContactSensor(this.name);
this.switchHandling === "realtime";
this.obstructionService
.getCharacteristic(Characteristic.ObstructionDetected)
.on('get', function(callback) {callback(null, that.state)});
return [this.obstructionService];
break;*/
}
}
};