4 * Copyright 2014 SmartThings
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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")
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"
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
40 page(name: "pageTwo", title: "Master devices", install: true, uninstall: true)
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.") {
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
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
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
64 section([mobileOnly:true]) {
65 label title: "Assign a name", required: false
66 mode title: "Set for specific mode(s)", required: false
72 log.debug "Installed with settings: ${settings}"
78 log.debug "Updated with settings: ${settings}"
85 subscribe tag, "nfcTouch", touchHandler
86 subscribe app, touchHandler
89 private currentStatus(devices, master, attribute) {
90 log.trace "currentStatus($devices, $master, $attribute)"
93 //result = devices.find{it.id == master}?.currentValue(attribute)
94 result = devices.find{"[" + it.id + "]" == master}?.currentValue(attribute)
99 def value = it.currentValue(attribute)
100 map[value] = (map[value] ?: 0) + 1
101 log.trace "$it.displayName: $value"
104 result = map.collect{it}.sort{it.value}[-1].key
106 log.debug "$attribute = $result"
110 def touchHandler(evt) {
111 log.trace "touchHandler($evt.descriptionText)"
113 def status = currentStatus(switch1, masterSwitch, "switch")
115 if (status == "on") {
125 def status = currentStatus(lock, masterLock, "lock")
127 if (status == "locked") {
137 def status = currentStatus(garageDoor, masterDoor, "status")
139 if (status == "open") {