Update double-tap.groovy
[smartapps.git] / official / the-big-switch.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  The Big Switch
14  *
15  *  Author: SmartThings
16  *
17  *  Date: 2013-05-01
18  */
19 definition(
20         name: "The Big Switch",
21         namespace: "smartthings",
22         author: "SmartThings",
23         description: "Turns on, off and dim a collection of lights based on the state of a specific switch.",
24         category: "Convenience",
25         iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
26         iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
27 )
28
29 preferences {
30         section("When this switch is turned on, off or dimmed") {
31                 input "master", "capability.switch", title: "Where?"
32         }
33         section("Turn on or off all of these switches as well") {
34                 input "switches", "capability.switch", multiple: true, required: false
35         }
36         section("And turn off but not on all of these switches") {
37                 input "offSwitches", "capability.switch", multiple: true, required: false
38         }
39         section("And turn on but not off all of these switches") {
40                 input "onSwitches", "capability.switch", multiple: true, required: false
41         }
42         section("And Dim these switches") {
43                 input "dimSwitches", "capability.switchLevel", multiple: true, required: false
44         }    
45 }
46
47 def installed()
48 {   
49         subscribe(master, "switch.on", onHandler)
50         subscribe(master, "switch.off", offHandler)
51         subscribe(master, "level", dimHandler)   
52 }
53
54 def updated()
55 {
56         unsubscribe()
57         subscribe(master, "switch.on", onHandler)
58         subscribe(master, "switch.off", offHandler)
59         subscribe(master, "level", dimHandler)   
60 }
61
62 def logHandler(evt) {
63         log.debug evt.value
64 }
65
66 def onHandler(evt) {
67         log.debug evt.value
68         log.debug onSwitches()
69         onSwitches()?.on()
70 }
71
72 def offHandler(evt) {
73         log.debug evt.value
74         log.debug offSwitches()
75         offSwitches()?.off()
76 }
77
78 def dimHandler(evt) {
79         log.debug "Dim level: $evt.value"
80         dimSwitches?.setLevel(evt.value)
81 }
82
83 private onSwitches() {
84         if(switches && onSwitches) { switches + onSwitches }
85         else if(switches) { switches }
86         else { onSwitches }
87 }
88
89 private offSwitches() {
90         if(switches && offSwitches) { switches + offSwitches }
91         else if(switches) { switches }
92         else { offSwitches }
93 }