-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcycle-switch.js
94 lines (82 loc) · 2.44 KB
/
cycle-switch.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
const CONFIG = {
/**
* Pick your desired Input to be used for triggering the cycling (note: this input would be changed
* to detached and to type button!)
*/
INPUT_ID: 0,
/**
* List (in the expected order) the operations that you want this script to cycle through.
* E.g. for 3 outputs - alternating on/off it could be [true, false, false] on the first cycle,
* and [false, true, false] for the second cycle and so on ...
* Value true means that output is enabled.
* Value false means that output is disabled.
* When cycle gets to the end of CYCLES array - on the next iteration operations start from beggining.
* With every single item in the inner arrays with values true/false output state can be managed - on/off.
* CYCLES array could be fit to output count of the shelly device.
* E.g. device with 3 outputs
* CYCLES: [
[true, false, false],
[false, true, false],
[false, false, true],
[false, false, false]
]
* E.g. device with 4 outputs
* CYCLES: [
[true, false, false, false],
[false, true, false, false],
[false, false, true, false],
[false, false, false, true],
[false, false, false, false]
],
...
*/
CYCLES: [
[true, false, false],
[false, true, false],
[false, false, true],
[false, false, false],
],
/**
* Define component type
*/
COMPONENT_TYPE: 'input'
}
let currentCycle = 0;
function runCycle () {
if (currentCycle >= CONFIG.CYCLES.length) {
currentCycle = 0;
}
let currentOperation = CONFIG.CYCLES[currentCycle];
currentCycle++;
for (let inputId = 0; inputId < currentOperation.length; inputId++) {
Shelly.call(
"Switch.Set",
{
id: inputId,
on: currentOperation[inputId]
}
);
}
}
function setup() {
Shelly.call(
"Switch.SetConfig",
{
id: JSON.stringify(CONFIG.INPUT_ID),
config: {in_mode: "detached" }
}
);
Shelly.call(
"Input.SetConfig",
{
id: JSON.stringify(CONFIG.INPUT_ID),
config: {type: "button"}
}
);
Shelly.addEventHandler(function (event) {
if (event.info.event === "single_push" && event.id === CONFIG.INPUT_ID && event.info.component === CONFIG.COMPONENT_TYPE + ":" + JSON.stringify(CONFIG.INPUT_ID)) {
runCycle();
}
});
}
setup();