homebridge-dummy 0.4.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -63,3 +63,40 @@ You may also want to create a timed switch that turns itself off after being on
63
63
  ]
64
64
 
65
65
  ```
66
+
67
+ ## Resettable Timed Switches
68
+
69
+ You may also want to create a timed switch that is reset each time it is activated. This way, the original timer is reset instead of creating a new timer.
70
+ This can be done by passing the 'resettable' argument in your config.json:
71
+
72
+ ```
73
+ "accessories": [
74
+ {
75
+ "accessory": "DummySwitch",
76
+ "name": "My Stateful Switch 1",
77
+ "time": 5000,
78
+ "resettable": true
79
+ }
80
+ ]
81
+
82
+ ```
83
+
84
+ ## Random Timed Switches
85
+
86
+ You might want to create a switch that given a random time turns itself off.
87
+ This can be achieved by enabling 'random'.
88
+ Each time a non-stateful, random timed switch is triggered, the time is set to a random value between 0 and 'time' milliseconds.
89
+ A random period within one hour is defined as follows in your config.json:
90
+
91
+ ```
92
+ "accessories": [
93
+ {
94
+ "accessory": "DummySwitch",
95
+ "name": "My Stateful Random Switch 1",
96
+ "time": 3600000,
97
+ "random": true
98
+ }
99
+ ]
100
+
101
+ ```
102
+
@@ -27,6 +27,18 @@
27
27
  "type": "number",
28
28
  "default": 1000,
29
29
  "description": "The switch will turn off after this number of milliseconds. Not used if the switch is stateful."
30
+ },
31
+ "random": {
32
+ "title": "Random",
33
+ "type": "boolean",
34
+ "default": false,
35
+ "description": "Randomize the time until a switch turns off. Not used if the switch is stateful."
36
+ },
37
+ "resettable": {
38
+ "title": "Resettable",
39
+ "type": "boolean",
40
+ "default": false,
41
+ "description": "The timer will reset each time the switch is turned on. "
30
42
  }
31
43
  }
32
44
  }
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  var Service, Characteristic, HomebridgeAPI;
4
+ const { HomebridgeDummyVersion } = require('./package.json');
4
5
 
5
6
  module.exports = function(homebridge) {
6
7
 
@@ -10,14 +11,25 @@ module.exports = function(homebridge) {
10
11
  homebridge.registerAccessory("homebridge-dummy", "DummySwitch", DummySwitch);
11
12
  }
12
13
 
14
+
13
15
  function DummySwitch(log, config) {
14
16
  this.log = log;
15
17
  this.name = config.name;
16
18
  this.stateful = config.stateful;
17
19
  this.reverse = config.reverse;
18
20
  this.time = config.time ? config.time : 1000;
21
+ this.resettable = config.resettable;
22
+ this.timer = null;
23
+ this.random = config.random;
19
24
  this._service = new Service.Switch(this.name);
20
25
 
26
+ this.informationService = new Service.AccessoryInformation();
27
+ this.informationService
28
+ .setCharacteristic(Characteristic.Manufacturer, 'Homebridge')
29
+ .setCharacteristic(Characteristic.Model, 'Dummy Switch')
30
+ .setCharacteristic(Characteristic.FirmwareRevision, HomebridgeDummyVersion)
31
+ .setCharacteristic(Characteristic.SerialNumber, 'Dummy-' + this.name.replace(/\s/g, '-'));
32
+
21
33
  this.cacheDirectory = HomebridgeAPI.user.persistPath();
22
34
  this.storage = require('node-persist');
23
35
  this.storage.initSync({dir:this.cacheDirectory, forgiveParseErrors: true});
@@ -38,21 +50,38 @@ function DummySwitch(log, config) {
38
50
  }
39
51
 
40
52
  DummySwitch.prototype.getServices = function() {
41
- return [this._service];
53
+ return [this.informationService, this._service];
54
+ }
55
+
56
+ function randomize(time) {
57
+ return Math.floor(Math.random() * (time + 1));
42
58
  }
43
59
 
44
60
  DummySwitch.prototype._setOn = function(on, callback) {
45
61
 
46
- this.log("Setting switch to " + on);
62
+ var delay = this.random ? randomize(this.time) : this.time;
63
+ var msg = "Setting switch to " + on
64
+ if (this.random && !this.stateful) {
65
+ if (on && !this.reverse || !on && this.reverse) {
66
+ msg = msg + " (random delay " + delay + "ms)"
67
+ }
68
+ }
69
+ this.log(msg);
47
70
 
48
71
  if (on && !this.reverse && !this.stateful) {
49
- setTimeout(function() {
72
+ if (this.resettable) {
73
+ clearTimeout(this.timer);
74
+ }
75
+ this.timer = setTimeout(function() {
50
76
  this._service.setCharacteristic(Characteristic.On, false);
51
- }.bind(this), this.time);
77
+ }.bind(this), delay);
52
78
  } else if (!on && this.reverse && !this.stateful) {
53
- setTimeout(function() {
79
+ if (this.resettable) {
80
+ clearTimeout(this.timer);
81
+ }
82
+ this.timer = setTimeout(function() {
54
83
  this._service.setCharacteristic(Characteristic.On, true);
55
- }.bind(this), this.time);
84
+ }.bind(this), delay);
56
85
  }
57
86
 
58
87
  if (this.stateful) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-dummy",
3
- "version": "0.4.1",
3
+ "version": "0.7.0",
4
4
  "description": "Dummy switches for Homebridge: https://github.com/nfarina/homebridge",
5
5
  "license": "ISC",
6
6
  "keywords": [
@@ -1,556 +0,0 @@
1
- [
2
- {
3
- "id": "33300ac17-on-0",
4
- "name": "Feet up on 0",
5
- "lights": [
6
- "1",
7
- "2",
8
- "3"
9
- ],
10
- "active": true
11
- },
12
- {
13
- "id": "0f2c67d80-on-0",
14
- "name": "Blue rain on 0",
15
- "lights": [
16
- "1",
17
- "2",
18
- "3"
19
- ],
20
- "active": true
21
- },
22
- {
23
- "id": "fd499c3e2-on-0",
24
- "name": "Taj on 0",
25
- "lights": [
26
- "1",
27
- "2",
28
- "3"
29
- ],
30
- "active": true
31
- },
32
- {
33
- "id": "3896c310d-on-0",
34
- "name": "Jump! on 0",
35
- "lights": [
36
- "1",
37
- "2",
38
- "3"
39
- ],
40
- "active": true
41
- },
42
- {
43
- "id": "797106fb9-on-0",
44
- "name": "Hammock on 0",
45
- "lights": [
46
- "1",
47
- "2",
48
- "3"
49
- ],
50
- "active": true
51
- },
52
- {
53
- "id": "64b4aa87e-on-0",
54
- "name": "Laila on 0",
55
- "lights": [
56
- "1",
57
- "2",
58
- "3"
59
- ],
60
- "active": true
61
- },
62
- {
63
- "id": "e07a35d7e-on-0",
64
- "name": "Greece on 0",
65
- "lights": [
66
- "1",
67
- "2",
68
- "3"
69
- ],
70
- "active": true
71
- },
72
- {
73
- "id": "e915785b2-on-0",
74
- "name": "Reading on 0",
75
- "lights": [
76
- "1",
77
- "2",
78
- "3"
79
- ],
80
- "active": true
81
- },
82
- {
83
- "id": "84fa88aa5-on-0",
84
- "name": "Energize on 0",
85
- "lights": [
86
- "1",
87
- "2",
88
- "3"
89
- ],
90
- "active": true
91
- },
92
- {
93
- "id": "ac637e2f0-on-0",
94
- "name": "Relax on 0",
95
- "lights": [
96
- "1",
97
- "2",
98
- "3"
99
- ],
100
- "active": true
101
- },
102
- {
103
- "id": "f5d73dbdd-on-0",
104
- "name": "Concentrate on 0",
105
- "lights": [
106
- "1",
107
- "2",
108
- "3"
109
- ],
110
- "active": true
111
- },
112
- {
113
- "id": "b0cae7c0f-on-0",
114
- "name": "Family Room on 0",
115
- "lights": [
116
- "1",
117
- "2",
118
- "3",
119
- "4",
120
- "5",
121
- "6",
122
- "7",
123
- "9",
124
- "10",
125
- "11",
126
- "12",
127
- "13",
128
- "14",
129
- "15"
130
- ],
131
- "active": true
132
- },
133
- {
134
- "id": "54ae5d997-on-0",
135
- "name": "Ski on 0",
136
- "lights": [
137
- "1",
138
- "2",
139
- "3"
140
- ],
141
- "active": true
142
- },
143
- {
144
- "id": "6b6ca6a0d-on-0",
145
- "name": "Beach on 0",
146
- "lights": [
147
- "1",
148
- "2",
149
- "3"
150
- ],
151
- "active": true
152
- },
153
- {
154
- "id": "087f88f52-on-0",
155
- "name": "Sunset on 0",
156
- "lights": [
157
- "1",
158
- "2",
159
- "3"
160
- ],
161
- "active": true
162
- },
163
- {
164
- "id": "2e1dbbb4a-on-0",
165
- "name": "Pencils on 0",
166
- "lights": [
167
- "1",
168
- "2",
169
- "3"
170
- ],
171
- "active": true
172
- },
173
- {
174
- "id": "2141cb8bd-on-0",
175
- "name": "Deep sea on 0",
176
- "lights": [
177
- "1",
178
- "2",
179
- "3"
180
- ],
181
- "active": true
182
- },
183
- {
184
- "id": "f2b600f90-on-0",
185
- "name": "Kathy on 0",
186
- "lights": [
187
- "1",
188
- "2",
189
- "3"
190
- ],
191
- "active": true
192
- },
193
- {
194
- "id": "358853161-on-0",
195
- "name": "SATCO on 0",
196
- "lights": [
197
- "1",
198
- "2",
199
- "3"
200
- ],
201
- "active": true
202
- },
203
- {
204
- "id": "0df0d8410-on-0",
205
- "name": "TV on 0",
206
- "lights": [
207
- "1",
208
- "2",
209
- "3",
210
- "4",
211
- "5",
212
- "6"
213
- ],
214
- "active": true
215
- },
216
- {
217
- "id": "b1ff92641-on-0",
218
- "name": "TV on 0",
219
- "lights": [
220
- "1",
221
- "2",
222
- "3",
223
- "4",
224
- "5",
225
- "6",
226
- "7",
227
- "9",
228
- "10",
229
- "11",
230
- "12",
231
- "13",
232
- "14",
233
- "15"
234
- ],
235
- "active": true
236
- },
237
- {
238
- "id": "7e21bd918-on-0",
239
- "name": "TV on 0",
240
- "lights": [
241
- "1",
242
- "2",
243
- "3",
244
- "4",
245
- "5",
246
- "6",
247
- "7",
248
- "9",
249
- "10",
250
- "11",
251
- "12",
252
- "13",
253
- "14",
254
- "15"
255
- ],
256
- "active": true
257
- },
258
- {
259
- "id": "672422585-on-0",
260
- "name": "Hangout on 0",
261
- "lights": [
262
- "1",
263
- "2",
264
- "3"
265
- ],
266
- "active": true
267
- },
268
- {
269
- "id": "2ef4ff39f-on-0",
270
- "name": "Relaxing on 0",
271
- "lights": [
272
- "1",
273
- "2",
274
- "3"
275
- ],
276
- "active": true
277
- },
278
- {
279
- "id": "dd3046b79-on-0",
280
- "name": "Bedroom on 0",
281
- "lights": [
282
- "1",
283
- "2",
284
- "3"
285
- ],
286
- "active": true
287
- },
288
- {
289
- "id": "02b12e930-off-0",
290
- "name": "3 lights off",
291
- "lights": [
292
- "1",
293
- "2",
294
- "3"
295
- ],
296
- "active": true
297
- },
298
- {
299
- "id": "beaaf9826-on-0",
300
- "name": "Relaxing on 0",
301
- "lights": [
302
- "1",
303
- "2",
304
- "3"
305
- ],
306
- "active": true
307
- },
308
- {
309
- "id": "f1c0e1d4b-on-0",
310
- "name": "Hangout on 0",
311
- "lights": [
312
- "1",
313
- "2",
314
- "3"
315
- ],
316
- "active": true
317
- },
318
- {
319
- "id": "OFF-TAP-1",
320
- "name": "Tap scene 1",
321
- "lights": [
322
- "1",
323
- "2",
324
- "3"
325
- ],
326
- "active": true
327
- },
328
- {
329
- "id": "TAP-2",
330
- "name": "Tap scene 2",
331
- "lights": [
332
- "1",
333
- "2",
334
- "3"
335
- ],
336
- "active": true
337
- },
338
- {
339
- "id": "TAP-3",
340
- "name": "Tap scene 3",
341
- "lights": [
342
- "1",
343
- "2",
344
- "3"
345
- ],
346
- "active": true
347
- },
348
- {
349
- "id": "TAP-4",
350
- "name": "Tap scene 4",
351
- "lights": [
352
- "1",
353
- "2",
354
- "3"
355
- ],
356
- "active": true
357
- },
358
- {
359
- "id": "5707a1666-on-0",
360
- "name": "TV on 0",
361
- "lights": [
362
- "1",
363
- "2",
364
- "3"
365
- ],
366
- "active": true
367
- },
368
- {
369
- "id": "f84ddb8fe-on-0",
370
- "name": "Daylight on 0",
371
- "lights": [
372
- "1",
373
- "2",
374
- "3"
375
- ],
376
- "active": true
377
- },
378
- {
379
- "id": "4eaf358d8-on-0",
380
- "name": "Relax on 0",
381
- "lights": [
382
- "1",
383
- "2",
384
- "3",
385
- "4",
386
- "5",
387
- "6"
388
- ],
389
- "active": true
390
- },
391
- {
392
- "id": "1df07f572-off-0",
393
- "name": "6 lights off",
394
- "lights": [
395
- "1",
396
- "2",
397
- "3",
398
- "4",
399
- "5",
400
- "6"
401
- ],
402
- "active": true
403
- },
404
- {
405
- "id": "442c3441f-on-0",
406
- "name": "Dimmed on 0",
407
- "lights": [
408
- "1",
409
- "2",
410
- "3",
411
- "4",
412
- "5",
413
- "6"
414
- ],
415
- "active": true
416
- },
417
- {
418
- "id": "ON-DIMMER",
419
- "name": "Dimmer scene",
420
- "lights": [
421
- "1",
422
- "2",
423
- "3",
424
- "4",
425
- "5",
426
- "6",
427
- "7",
428
- "8",
429
- "9"
430
- ],
431
- "active": true
432
- },
433
- {
434
- "id": "fc08438b0-on-0",
435
- "name": "Relax on 0",
436
- "lights": [
437
- "1",
438
- "2",
439
- "3",
440
- "4",
441
- "5",
442
- "6",
443
- "9",
444
- "10",
445
- "11",
446
- "12",
447
- "13"
448
- ],
449
- "active": true
450
- },
451
- {
452
- "id": "0f58637a8-on-0",
453
- "name": "Relax on 0",
454
- "lights": [
455
- "1",
456
- "2",
457
- "3",
458
- "4",
459
- "5",
460
- "6",
461
- "7",
462
- "9",
463
- "10",
464
- "11",
465
- "12",
466
- "13",
467
- "14",
468
- "15"
469
- ],
470
- "active": true
471
- },
472
- {
473
- "id": "0205ab766-off-0",
474
- "name": "14 lights off",
475
- "lights": [
476
- "1",
477
- "2",
478
- "3",
479
- "4",
480
- "5",
481
- "6",
482
- "7",
483
- "9",
484
- "10",
485
- "11",
486
- "12",
487
- "13",
488
- "14",
489
- "15"
490
- ],
491
- "active": true
492
- },
493
- {
494
- "id": "7e1165625-on-0",
495
- "name": "Family Room on 0",
496
- "lights": [
497
- "1",
498
- "2",
499
- "3",
500
- "4",
501
- "5",
502
- "6",
503
- "7",
504
- "9",
505
- "10",
506
- "11",
507
- "12",
508
- "13",
509
- "14",
510
- "15"
511
- ],
512
- "active": true
513
- },
514
- {
515
- "id": "faa918d0f-on-0",
516
- "name": "Living Room on 0",
517
- "lights": [
518
- "1",
519
- "2",
520
- "3",
521
- "4",
522
- "5",
523
- "6",
524
- "7",
525
- "9",
526
- "10",
527
- "11",
528
- "12",
529
- "13",
530
- "14",
531
- "15"
532
- ],
533
- "active": true
534
- },
535
- {
536
- "id": "426146c8d-on-0",
537
- "name": "Family Room on 0",
538
- "lights": [
539
- "1",
540
- "2",
541
- "3",
542
- "4",
543
- "5",
544
- "6",
545
- "7",
546
- "9",
547
- "10",
548
- "11",
549
- "12",
550
- "13",
551
- "14",
552
- "15"
553
- ],
554
- "active": true
555
- }
556
- ]