forked from shelly-tools/shelly-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelly-plus-2pm_switch_controller_hue-relay.js
131 lines (120 loc) · 3.32 KB
/
shelly-plus-2pm_switch_controller_hue-relay.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
let CONFIG = {
ip: '10.0.0.1', //Hue Bridge IP
user: 'abcdef', //Hue Bridge API user
cmd: {
"single_push": {
"0": {
"relay" : 0 // Shelly Relay Channel 0
},
"1": {
"relay" : 1 // Shelly Relay Channel 1
}
},
"double_push": {
"0": {
"hue": {
"type" : "groups",
"id": "2" // Esstisch
}
},
"1": {
"hue": {
"type" : "groups",
"id": "1" // Wohnzimmer
}
}
}
}
};
// Set Switch detached 0
Shelly.call("Input.SetConfig", {
id: 0,
config: {
type: "button",
},
});
Shelly.call("Switch.SetConfig", {
id: 0,
config: {
in_mode: "detached",
initial_state: "restore_last"
},
});
// Set Switch detached 1
Shelly.call("Input.SetConfig", {
id: 1,
config: {
type: "button",
},
});
Shelly.call("Switch.SetConfig", {
id: 1,
config: {
in_mode: "detached",
initial_state: "restore_last"
},
});
// add an evenHandler
Shelly.addEventHandler(
function (event, user_data) {
let cmds = null;
print(JSON.stringify(event));
if (typeof event.info.event !== 'undefined' && typeof(CONFIG.cmd[event.info.event]) !== "undefined") {
cmds = CONFIG.cmd[event.info.event];
if (typeof cmds[event.info.id] !== "undefined" ) {
if (typeof cmds[event.info.id]["relay"] === "number") {
// switch relay
Shelly.call(
"Switch.Toggle",
{
id: cmds[event.info.id]["relay"]
},
function (result, code, msg, ud) {},
null
);
}
else if (typeof cmds[event.info.id]["hue"] === "object") {
toggleHue(
cmds[event.info.id]["hue"]["type"],
cmds[event.info.id]["hue"]["id"]
);
}
else {
return true;
}
} else {
return true;
}
} else {
return true;
}
}
);
function toggleHue(_type, _id) {
Shelly.call(
"http.request", {
method: "GET",
url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/' + _type + '/' + _id,
},
function (res, error_code, error_message, ud) {
let st = JSON.parse(res.body);
let onOff = (st.state.any_on === true || st.state.on === true) ? "false" : "true";
let b = '{"on": ' + onOff + '}';
let action = ud.type === "groups" ? "action" : "state";
Shelly.call(
"http.request", {
method: "PUT",
url: 'http://' + CONFIG.ip + '/api/' + CONFIG.user + '/' + ud.type + '/' + ud.id + '/' + action,
body: b
},
function (r, e, m) {
},
null
);
},
{
type: _type,
id: _id
}
);
}