Update thermostat-mode-director.groovy
[smartapps.git] / official / nfc-tag-toggle.groovy
1 /**
2  *  NFC Tag Toggle
3  *
4  *  Copyright 2014 SmartThings
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16  
17 definition(
18     name: "NFC Tag Toggle",
19     namespace: "smartthings",
20     author: "SmartThings",
21     description: "Allows toggling of a switch, lock, or garage door based on an NFC Tag touch event",
22     category: "SmartThings Internal",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor@2x.png",
25     iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Developers/nfc-tag-executor@2x.png")
26
27
28 preferences {
29     page(name: "pageOne", title: "Device selection", uninstall: true, nextPage: "pageTwo") {
30         section("Select an NFC tag") {
31             input "tag", "capability.touchSensor", title: "NFC Tag"
32         }
33         section("Select devices to control") {
34             input "switch1", "capability.switch", title: "Light or switch", required: false, multiple: true
35             input "lock", "capability.lock", title: "Lock", required: false, multiple: true
36             input "garageDoor", "capability.doorControl", title: "Garage door controller", required: false, multiple: true
37         }
38     }
39     
40     page(name: "pageTwo", title: "Master devices", install: true, uninstall: true)
41 }
42
43 def pageTwo() {
44         dynamicPage(name: "pageTwo") {
45         section("If set, the state of these devices will be toggled each time the tag is touched, " + 
46                 "e.g. a light that's on will be turned off and one that's off will be turned on, " +
47                 "other devices of the same type will be set to the same state as their master device. " +
48                 "If no master is designated then the majority of devices of the same type will be used " +
49                 "to determine whether to turn on or off the devices.") {
50             
51             if (switch1 || masterSwitch) {
52                 //input "masterSwitch", "enum", title: "Master switch", options: switch1.collect{[(it.id): it.displayName]}, required: false
53                 input "masterSwitch", "enum", title: "Master switch", options: switch1.collect{[(it.id)]}, required: false
54             }
55             if (lock || masterLock) {
56                 //input "masterLock", "enum", title: "Master lock", options: lock.collect{[(it.id): it.displayName]}, required: false
57                 input "masterLock", "enum", title: "Master lock", options: lock.collect{[(it.id)]}, required: false
58             }
59             if (garageDoor || masterDoor) {
60                 //input "masterDoor", "enum", title: "Master door", options: garageDoor.collect{[(it.id): it.displayName]}, required: false
61                 input "masterDoor", "enum", title: "Master door", options: garageDoor.collect{[(it.id)]}, required: false
62             }            
63                 }
64                 section([mobileOnly:true]) {
65                         label title: "Assign a name", required: false
66                         mode title: "Set for specific mode(s)", required: false
67                 }        
68     }
69 }
70
71 def installed() {
72         log.debug "Installed with settings: ${settings}"
73
74         initialize()
75 }
76
77 def updated() {
78         log.debug "Updated with settings: ${settings}"
79
80         unsubscribe()
81         initialize()
82 }
83
84 def initialize() {
85         subscribe tag, "nfcTouch", touchHandler
86     subscribe app, touchHandler
87 }
88
89 private currentStatus(devices, master, attribute) {
90         log.trace "currentStatus($devices, $master, $attribute)"
91         def result = null
92         if (master) {
93         //result = devices.find{it.id == master}?.currentValue(attribute)
94         result = devices.find{"[" + it.id + "]" == master}?.currentValue(attribute)
95     }
96     else {
97         def map = [:]
98         devices.each {
99                 def value = it.currentValue(attribute)
100             map[value] = (map[value] ?: 0) + 1
101             log.trace "$it.displayName: $value"
102         }
103         log.trace map
104         result = map.collect{it}.sort{it.value}[-1].key
105     }
106     log.debug "$attribute = $result"
107     result
108 }
109
110 def touchHandler(evt) {
111         log.trace "touchHandler($evt.descriptionText)"
112     if (switch1) {
113         def status = currentStatus(switch1, masterSwitch, "switch")
114         switch1.each {
115             if (status == "on") {
116                 it.off()
117             }
118             else {
119                 it.on()
120             }
121         }
122     }
123     
124     if (lock) {
125         def status = currentStatus(lock, masterLock, "lock")
126         lock.each {
127             if (status == "locked") {
128                 lock.unlock()
129             }
130             else {
131                 lock.lock()
132             }
133         }
134     }
135     
136     if (garageDoor) {
137         def status = currentStatus(garageDoor, masterDoor, "status")
138         garageDoor.each {
139                 if (status == "open") {
140                 it.close()
141             }
142             else {
143                 it.open()
144             }
145         }
146     }
147 }