forked from nitaybz/homebridge-delay-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (74 loc) · 3.06 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
var Service, Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-delay-switch", "DelaySwitch", delaySwitch);
}
function delaySwitch(log, config, api) {
let UUIDGen = api.hap.uuid;
this.log = log;
this.name = config['name'];
this.delay = config['delay'];
this.disableSensor = config['disableSensor'] || false;
this.startOnReboot = config['startOnReboot'] || false;
this.timer;
this.switchOn = false;
this.motionTriggered = false;
this.uuid = UUIDGen.generate(this.name)
}
delaySwitch.prototype.getServices = function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Delay Manufacturer")
.setCharacteristic(Characteristic.Model, "Delay Model")
.setCharacteristic(Characteristic.SerialNumber, this.uuid);
this.switchService = new Service.Switch(this.name);
this.switchService.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
if (this.startOnReboot)
this.switchService.setCharacteristic(Characteristic.On, true)
var services = [informationService, this.switchService]
if (!this.disableSensor){
this.motionService = new Service.MotionSensor(this.name + ' Trigger');
this.motionService
.getCharacteristic(Characteristic.MotionDetected)
.on('get', this.getMotion.bind(this));
services.push(this.motionService)
}
return services;
}
delaySwitch.prototype.setOn = function (on, callback) {
if (!on) {
this.log('Stopping the Timer.');
this.switchOn = false;
clearTimeout(this.timer);
this.motionTriggered = false;
if (!this.disableSensor) this.motionService.getCharacteristic(Characteristic.MotionDetected).updateValue(false);
} else {
this.log('Starting the Timer.');
this.switchOn = true;
clearTimeout(this.timer);
this.timer = setTimeout(function() {
this.log('Time is Up!');
this.switchService.getCharacteristic(Characteristic.On).updateValue(false);
this.switchOn = false;
if (!this.disableSensor) {
this.motionTriggered = true;
this.motionService.getCharacteristic(Characteristic.MotionDetected).updateValue(true);
this.log('Triggering Motion Sensor');
setTimeout(function() {
this.motionService.getCharacteristic(Characteristic.MotionDetected).updateValue(false);
this.motionTriggered = false;
}.bind(this), 3000);
}
}.bind(this), this.delay);
}
callback();
}
delaySwitch.prototype.getOn = function (callback) {
callback(null, this.switchOn);
}
delaySwitch.prototype.getMotion = function(callback) {
callback(null, this.motionTriggered);
}