-
Notifications
You must be signed in to change notification settings - Fork 59
/
cycle-switch.js
57 lines (50 loc) · 1.25 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
let CONFIG = {
/**
* Pick your desired Input to be used for triggering the cycling (note: this input would be changed
* to detached!)
*/
INPUT_ID: 0,
/**
* List (in the expected order) the operations that you want this script to cycle through.
* E.g. [switchId, "on" or "off"]
*/
CYCLES: [
[0, "on"],
[1, "on"],
[0, "off"],
[1, "off"],
],
};
let currentCycle = 0;
let runCycle = function () {
let currentOperation = CONFIG.CYCLES[currentCycle];
if (!currentOperation) {
currentCycle = 0;
currentOperation = CONFIG.CYCLES[currentCycle];
}
currentCycle++;
Shelly.call("switch.set", {
id: JSON.stringify(currentOperation[0]),
on: currentOperation[1] === "on",
});
};
let setup = function () {
Shelly.call(
"switch.setconfig",
{ id: JSON.stringify(CONFIG.INPUT_ID), config: { in_mode: "detached" } },
function () {
Shelly.addEventHandler(function (event) {
if (event.component === "input:" + JSON.stringify(CONFIG.INPUT_ID)) {
if (
event.info.state !== false &&
event.info.event !== "btn_up" &&
event.info.event !== "btn_down"
) {
runCycle();
}
}
}, null);
}
);
};
setup();