homebridge-dummy 0.6.0 → 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 +20 -0
- package/config.schema.json +6 -0
- package/index.js +15 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,3 +80,23 @@ This can be done by passing the 'resettable' argument in your config.json:
|
|
|
80
80
|
]
|
|
81
81
|
|
|
82
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
|
+
|
package/config.schema.json
CHANGED
|
@@ -28,6 +28,12 @@
|
|
|
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
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
|
+
},
|
|
31
37
|
"resettable": {
|
|
32
38
|
"title": "Resettable",
|
|
33
39
|
"type": "boolean",
|
package/index.js
CHANGED
|
@@ -20,6 +20,7 @@ function DummySwitch(log, config) {
|
|
|
20
20
|
this.time = config.time ? config.time : 1000;
|
|
21
21
|
this.resettable = config.resettable;
|
|
22
22
|
this.timer = null;
|
|
23
|
+
this.random = config.random;
|
|
23
24
|
this._service = new Service.Switch(this.name);
|
|
24
25
|
|
|
25
26
|
this.informationService = new Service.AccessoryInformation();
|
|
@@ -52,9 +53,20 @@ DummySwitch.prototype.getServices = function() {
|
|
|
52
53
|
return [this.informationService, this._service];
|
|
53
54
|
}
|
|
54
55
|
|
|
56
|
+
function randomize(time) {
|
|
57
|
+
return Math.floor(Math.random() * (time + 1));
|
|
58
|
+
}
|
|
59
|
+
|
|
55
60
|
DummySwitch.prototype._setOn = function(on, callback) {
|
|
56
61
|
|
|
57
|
-
this.
|
|
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);
|
|
58
70
|
|
|
59
71
|
if (on && !this.reverse && !this.stateful) {
|
|
60
72
|
if (this.resettable) {
|
|
@@ -62,14 +74,14 @@ DummySwitch.prototype._setOn = function(on, callback) {
|
|
|
62
74
|
}
|
|
63
75
|
this.timer = setTimeout(function() {
|
|
64
76
|
this._service.setCharacteristic(Characteristic.On, false);
|
|
65
|
-
}.bind(this),
|
|
77
|
+
}.bind(this), delay);
|
|
66
78
|
} else if (!on && this.reverse && !this.stateful) {
|
|
67
79
|
if (this.resettable) {
|
|
68
80
|
clearTimeout(this.timer);
|
|
69
81
|
}
|
|
70
82
|
this.timer = setTimeout(function() {
|
|
71
83
|
this._service.setCharacteristic(Characteristic.On, true);
|
|
72
|
-
}.bind(this),
|
|
84
|
+
}.bind(this), delay);
|
|
73
85
|
}
|
|
74
86
|
|
|
75
87
|
if (this.stateful) {
|