This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (230 loc) · 8.75 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
"use strict";
const inherits = require('util').inherits;
var Service, Characteristic, UUID;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUID = homebridge.hap.uuid;
/**
* Characteristic "Delay Time Remaining"
*/
Characteristic.DelayTimeRemaining = function() {
Characteristic.call(this, 'Delay Time Remaining', '1000006D-0000-1000-8000-0026BB765291');
this.setProps({
format: Characteristic.Formats.UINT64,
unit: Characteristic.Units.SECONDS,
maxValue: 3600,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(Characteristic.DelayTimeRemaining, Characteristic);
Characteristic.DelayTimeRemaining.UUID = '1000006D-0000-1000-8000-0026BB765291';
/**
* Characteristic "Delay Time in Seconds"
*/
Characteristic.DelayTime = function() {
Characteristic.call(this, 'Delay Time', '1100006D-0000-1000-8000-0026BB765291');
this.setProps({
format: Characteristic.Formats.UINT64,
unit: Characteristic.Units.SECONDS,
maxValue: 3600,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(Characteristic.DelayTime, Characteristic);
Characteristic.DelayTime.UUID = '1100006D-0000-1000-8000-0026BB765291';
homebridge.registerAccessory("@djelibeybi/homebridge-delayed-occupancy", "delayed-occupancy-sensor", DelayedOccupancy);
};
/**
* This accessory publishes an Occupancy Sensor as well as 1 or more activation
* switches to control the status of the sensor. If any of the slaves are on
* then this sensor registers as "Occupancy Detected" ("Occupied). When all
* slaves are turned off this will remain "Occupied" for as long as the
* specified delay.
*
* Config:
*
* name: The name of this Occupancy Sensor and it's activation switches. If there are
* more than one slaves they will become "name 1", "name 2", etc.
* switchCount (optional): Will create 1 activation switch with the same name as the
* Occupancy Sensor by default. Change this if you need more than 1 switch
* to control the sensor.
* delay: If set to less than 1 there will be no delay when all switches are
* turned to off. Specify a number in seconds and the sensor will wait
* that long after all switches have been turned off to become
* "Un-occupied". If any activation switch is turned on the counter will clear
* and start over once all switches are off again.
*
*
* What can I do with this plugin?
* @todo: Addd use case and instructions here.
*/
class DelayedOccupancy {
constructor(log, config) {
this.log = log;
this.name = config.name || "Delayed Occupancy Sensor";
this.switches = config.switches || [];
this.delay = Math.min(3600, Math.max(0, parseInt(config.delay, 10) || 0));
this._timer = null;
this._timer_started = null;
this._timer_delay = 0;
this._interval = null;
this._interval_last_value = 0;
this._last_occupied_state = false;
this.switchServices = [];
this.occupancyService = new Service.OccupancySensor(this.name);
this.occupancyService.addCharacteristic(Characteristic.DelayTime);
this.occupancyService.setCharacteristic(Characteristic.DelayTime, this.delay);
this.occupancyService.getCharacteristic(Characteristic.DelayTime).on('change', (event) => {
this.log('Setting delay to:', event.newValue);
this.delay = event.newValue;
});
this.occupancyService.addCharacteristic(Characteristic.DelayTimeRemaining);
this.occupancyService.setCharacteristic(Characteristic.DelayTimeRemaining, 0);
if (this.switches.length === 0) {
this.switchCount = 1;
this.switchServices.push(this._createSwitch('Occupancy Delay Activation Switch'));
} else {
this.switchCount = this.switches.length;
for (const activationSwitch of this.switches) {
this.switchServices.push(this._createSwitch(activationSwitch));
}
}
}
/**
* Starts the countdown timer.
*/
start() {
this.stop();
this._timer_started = (new Date()).getTime();
if (this.delay) {
this._timer = setTimeout(this.setOccupancyNotDetected.bind(this), (this.delay * 1000));
this._timer_delay = this.delay;
this._interval = setInterval(() => {
var elapsed = ((new Date()).getTime() - this._timer_started) / 1000,
newValue = Math.round(this._timer_delay - elapsed);
if (newValue !== this._interval_last_value) {
this.occupancyService.setCharacteristic(Characteristic.DelayTimeRemaining, newValue);
this._interval_last_value = newValue;
}
}, 250);
} else {
/* occupancy no longer detected */
this.setOccupancyNotDetected();
}
};
/**
* Stops the delay countdown timer
*/
stop() {
if (this._timer) {
clearTimeout(this._timer);
clearInterval(this._interval);
this._timer = null;
this._timer_started = null;
this._timer_delay = null;
this._interval = null;
}
};
setOccupancyDetected() {
this._last_occupied_state = true;
this.log('Sensor is now detecting occupancy.');
this.occupancyService.setCharacteristic(Characteristic.OccupancyDetected, Characteristic.OccupancyDetected.OCCUPANCY_DETECTED);
if (this.delay) {
this.occupancyService.setCharacteristic(Characteristic.DelayTimeRemaining, this.delay);
}
}
setOccupancyNotDetected() {
this._last_occupied_state = false;
this.stop();
this.log('Sensor is no longer detecting occupancy.');
this.occupancyService.setCharacteristic(Characteristic.OccupancyDetected, Characteristic.OccupancyDetected.OCCUPANCY_NOT_DETECTED);
if (this.delay) {
this.occupancyService.setCharacteristic(Characteristic.DelayTimeRemaining, 0);
}
}
/**
* Checks all the activation switches to see if any of them are on. If so this
* Occupancy Sensor will remain "Occupied". This is used as a callback when
* the "On" state changes on any of the activation switches.
*/
checkOccupancy(switchName, data) {
if (data.oldValue !== data.newValue) {
this.log('%s has turned: %s', switchName, data.newValue ? 'ON' : 'OFF');
}
var occupied = 0,
remaining = this.switchCount,
/* callback for when all the switches values have been returend */
return_occupancy = (occupied) => {
if (occupied) {
if (this._last_occupied_state === !!occupied) {
this.stop();
} else {
this.setOccupancyDetected();
}
} else if (null === this._timer) {
this.start();
}
},
/*
callback when we check a switches value. keeps track of the switches
returned value and decides when to finish the function
*/
set_value = (value) => {
remaining -= 1;
if (value) {
occupied += 1;
}
if (!remaining) {
return_occupancy(occupied);
}
};
/* look at all the activation switches "on" characteristic and return to callback */
for (let i = 0; i < this.switchCount; i += 1) {
this.switchServices[i]
.getCharacteristic(Characteristic.On)
.getValue(function(err, value) {
if (!err) {
set_value(value);
}
});
}
}
/**
* Homebridge function to return all the Services associated with this
* Accessory.
*
* @returns {*[]}
*/
getServices() {
const sensorUUID = UUID.generate(this.name);
const accessoryUUID = UUID.toShortForm(sensorUUID, sensorUUID.substr(8));
var informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'Djelibeybi')
.setCharacteristic(Characteristic.Model, 'Delayed Occupancy Sensor')
.setCharacteristic(Characteristic.FirmwareRevision, '2.2.0')
.setCharacteristic(Characteristic.SerialNumber, accessoryUUID);
return [this.occupancyService, informationService, ...this.switchServices]
}
/**
* Internal helper function to create a virtual switch that is tied to the
* status of this Occupancy Sensor.
*
* @param name
* @returns {Service.Switch|*}
* @private
*/
_createSwitch(switchName) {
var activationSwitch = new Service.Switch(switchName, switchName);
activationSwitch.setCharacteristic(Characteristic.On, false);
activationSwitch.getCharacteristic(Characteristic.On).on('change', this.checkOccupancy.bind(this, switchName));
this.log('Created occupancy activation switch: ' + switchName);
return activationSwitch;
}
}