-
Notifications
You must be signed in to change notification settings - Fork 0
/
master_child_switch.app.groovy
66 lines (56 loc) · 1.72 KB
/
master_child_switch.app.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
/**
* Master and Child Switches
*/
definition(
name: "Master and Child Switches",
namespace: "jscgs350",
author: "SmartThings",
description: "Pick a master switch to control other switches. Kind of like a traditional switched outlet.",
category: "My Apps",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png")
preferences {
section("When this master switch is turned on or off...") {
input name: "master", title: "Which Switch?", type: "capability.switch", required: true
}
section("Turn on or off all of these switches as well") {
input "switches", "capability.switch", multiple: true, required: false
}
section("Turn off but not on all of these switches") {
input "offSwitches", "capability.switch", multiple: true, required: false
}
section("And turn on but not off all of these switches") {
input "onSwitches", "capability.switch", multiple: true, required: false
}
}
def installed() {
subscribeToEvents()
}
def updated() {
unsubscribe()
subscribeToEvents()
}
def subscribeToEvents() {
subscribe(master, "switch.on", onHandler, [filterEvents: false])
subscribe(master, "switch.off", offHandler, [filterEvents: false])
}
def onHandler(evt) {
log.debug evt.value
log.debug onSwitches()
onSwitches()?.on()
}
def offHandler(evt) {
log.debug evt.value
log.debug offSwitches()
offSwitches()?.off()
}
private onSwitches() {
if(switches && onSwitches) { switches + onSwitches }
else if(switches) { switches }
else { onSwitches }
}
private offSwitches() {
if(switches && offSwitches) { switches + offSwitches }
else if(switches) { switches }
else { offSwitches }
}