-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVirtualSwitchParent.groovy
404 lines (354 loc) · 9.24 KB
/
VirtualSwitchParent.groovy
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* VirtualSwitchParent
*
* Author: [email protected]
* Date: 2014-03-26
*/
preferences {
section("Connect these virtual switches to the Arduino's relays") {
input "switch1", title: "Switch for relay 1", "capability.switch"
input "switch2", title: "Switch for relay 2", "capability.switch", required: false
input "switch3", title: "Switch for relay 3", "capability.switch", required: false
input "switch4", title: "Switch for relay 4", "capability.switch", required: false
input "switch5", title: "Switch for relay 5", "capability.switch", required: false
input "switch6", title: "Switch for relay 6", "capability.switch", required: false
input "switch7", title: "Switch for relay 7", "capability.switch", required: false
input "switch8", title: "Switch for relay 8", "capability.switch", required: false
}
section("Which Arduino relay board to control?") {
input "arduino", "device.arduinoRelayBoard"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
subscribe()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribe()
}
def subscribe() {
// Listen to the virtual switches
subscribe(switch1, "switch.on", switchOn1)
subscribe(switch1, "switch.off", switchOff1)
subscribe(switch2, "switch.on", switchOn2)
subscribe(switch2, "switch.off", switchOff2)
subscribe(switch3, "switch.on", switchOn3)
subscribe(switch3, "switch.off", switchOff3)
subscribe(switch4, "switch.on", switchOn4)
subscribe(switch4, "switch.off", switchOff4)
subscribe(switch5, "switch.on", switchOn5)
subscribe(switch5, "switch.off", switchOff5)
subscribe(switch6, "switch.on", switchOn6)
subscribe(switch6, "switch.off", switchOff6)
subscribe(switch7, "switch.on", switchOn7)
subscribe(switch7, "switch.off", switchOff7)
subscribe(switch8, "switch.on", switchOn8)
subscribe(switch8, "switch.off", switchOff8)
// Listen to anything which happens on the device
subscribe(arduino, "relay1.on", relayTurnOn1)
subscribe(arduino, "relay1.off", relayTurnOff1)
subscribe(arduino, "relay2.on", relayTurnOn2)
subscribe(arduino, "relay2.off", relayTurnOff2)
subscribe(arduino, "relay3.on", relayTurnOn3)
subscribe(arduino, "relay3.off", relayTurnOff3)
subscribe(arduino, "relay4.on", relayTurnOn4)
subscribe(arduino, "relay4.off", relayTurnOff4)
subscribe(arduino, "relay5.on", relayTurnOn5)
subscribe(arduino, "relay5.off", relayTurnOff5)
subscribe(arduino, "relay6.on", relayTurnOn6)
subscribe(arduino, "relay6.off", relayTurnOff6)
subscribe(arduino, "relay7.on", relayTurnOn7)
subscribe(arduino, "relay7.off", relayTurnOff7)
subscribe(arduino, "relay8.on", relayTurnOn8)
subscribe(arduino, "relay8.off", relayTurnOff8)
}
def switchOn1(evt)
{
log.debug "switchOn1($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay1") != "on")
{
log.debug "Sending RelayOn1 event to Arduino"
arduino.RelayOn1()
}
}
def switchOff1(evt)
{
log.debug "switchOff1($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay1") != "off")
{
log.debug "Sending RelayOff1 event to Arduino"
arduino.RelayOff1()
}
}
def switchOn2(evt)
{
log.debug "switchOn2($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay2") != "on")
{
log.debug "Sending RelayOn2 event to Arduino"
arduino.RelayOn2()
}
}
def switchOff2(evt)
{
log.debug "switchOff2($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay2") != "off")
{
log.debug "Sending RelayOff2 event to Arduino"
arduino.RelayOff2()
}
}
def switchOn3(evt)
{
log.debug "switchOn3($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay3") != "on")
{
log.debug "Sending RelayOn3 event to Arduino"
arduino.RelayOn3()
}
}
def switchOff3(evt)
{
log.debug "switchOff3($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay3") != "off")
{
log.debug "Sending RelayOff3 event to Arduino"
arduino.RelayOff3()
}
}
def switchOn4(evt)
{
log.debug "switchOn4($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay4") != "on")
{
log.debug "Sending RelayOn4 event to Arduino"
arduino.RelayOn4()
}
}
def switchOff4(evt)
{
log.debug "switchOff4($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay4") != "off")
{
log.debug "Sending RelayOff4 event to Arduino"
arduino.RelayOff4()
}
}
def switchOn5(evt)
{
log.debug "switchO5($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay5") != "on")
{
log.debug "Sending RelayOn5 event to Arduino"
arduino.RelayOn5()
}
}
def switchOff5(evt)
{
log.debug "switchOff5($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay5") != "off")
{
log.debug "Sending RelayOff5 event to Arduino"
arduino.RelayOff5()
}
}
def switchOn6(evt)
{
log.debug "switchOn6($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay6") != "on")
{
log.debug "Sending RelayOn6 event to Arduino"
arduino.RelayOn6()
}
}
def switchOff6(evt)
{
log.debug "switchOff6($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay6") != "off")
{
log.debug "Sending RelayOff6 event to Arduino"
arduino.RelayOff6()
}
}
def switchOn7(evt)
{
log.debug "switchOn7($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay7") != "on")
{
log.debug "Sending RelayOn7 event to Arduino"
arduino.RelayOn7()
}
}
def switchOff7(evt)
{
log.debug "switchOff7($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay7") != "off")
{
log.debug "Sending RelayOff7 event to Arduino"
arduino.RelayOff7()
}
}
def switchOn8(evt)
{
log.debug "switchOn8($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay8") != "on")
{
log.debug "Sending RelayOn8 event to Arduino"
arduino.RelayOn8()
}
}
def switchOff8(evt)
{
log.debug "switchOff8($evt.name: $evt.value: $evt.deviceId)"
if (arduino.currentValue("relay8") != "off")
{
log.debug "Sending RelayOff8 event to Arduino"
arduino.RelayOff8()
}
}
def relayTurnOn1(evt)
{
log.debug "Relay 1 was turned on"
if (switch1.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 1 on"
switch1.on()
}
}
def relayTurnOff1(evt)
{
log.debug "Relay 1 was turned off"
if (switch1.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 1 off"
switch1.off()
}
}
def relayTurnOn2(evt)
{
log.debug "Relay 2 was turned on"
if (switch2.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 2 on"
switch2.on()
}
}
def relayTurnOff2(evt)
{
log.debug "Relay 2 was turned off"
if (switch2.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 2 off"
switch2.off()
}
}
def relayTurnOn3(evt)
{
log.debug "Relay 3 was turned on"
if (switch3.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 3 on"
switch3.on()
}
}
def relayTurnOff3(evt)
{
log.debug "Relay 3 was turned off"
if (switch3.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 3 off"
switch3.off()
}
}
def relayTurnOn4(evt)
{
log.debug "Relay 4 was turned on"
if (switch4.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 4 on"
switch4.on()
}
}
def relayTurnOff4(evt)
{
log.debug "Relay 4 was turned off"
if (switch4.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 4 off"
switch4.off()
}
}
def relayTurnOn5(evt)
{
log.debug "Relay 5 was turned on"
if (switch5.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 5 on"
switch5.on()
}
}
def relayTurnOff5(evt)
{
log.debug "Relay 5 was turned off"
if (switch5.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 5 off"
switch5.off()
}
}
def relayTurnOn6(evt)
{
log.debug "Relay 6 was turned on"
if (switch6.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 6 on"
switch6.on()
}
}
def relayTurnOff6(evt)
{
log.debug "Relay 6 was turned off"
if (switch6.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 6 off"
switch6.off()
}
}
def relayTurnOn7(evt)
{
log.debug "Relay 7 was turned on"
if (switch7.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 7 on"
switch7.on()
}
}
def relayTurnOff7(evt)
{
log.debug "Relay 7 was turned off"
if (switch7.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 7 off"
switch7.off()
}
}
def relayTurnOn8(evt)
{
log.debug "Relay 8 was turned on"
if (switch8.currentValue("switch") != "on")
{
log.debug "Flipping virtual switch 8 on"
switch8.on()
}
}
def relayTurnOff8(evt)
{
log.debug "Relay 8 was turned off"
if (switch8.currentValue("switch") != "off")
{
log.debug "Flipping virtual switch 8 off"
switch8.off()
}
}