Update double-tap.groovy
[smartapps.git] / official / double-tap.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  *  Double Tap
14  *
15  *  Author: SmartThings
16  */
17 definition(
18     name: "Double Tap",
19     namespace: "smartthings",
20     author: "SmartThings",
21     description: "Turn on or off any number of switches when an existing switch is tapped twice in a row.",
22     category: "Convenience",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
25 )
26
27 preferences {
28         section("When this switch is double-tapped...") {
29                 input "master", "capability.switch", title: "Where?"
30         }
31         section("Turn on or off all of these switches as well") {
32                 input "switches", "capability.switch", multiple: true, required: false
33         }
34         section("And turn off but not on all of these switches") {
35                 input "offSwitches", "capability.switch", multiple: true, required: false
36         }
37         section("And turn on but not off all of these switches") {
38                 input "onSwitches", "capability.switch", multiple: true, required: false
39         }
40 }
41
42 def installed()
43 {
44         subscribe(master, "switch", switchHandler, [filterEvents: false])
45 }
46
47 def updated()
48 {
49         unsubscribe()
50         subscribe(master, "switch", switchHandler, [filterEvents: false])
51 }
52
53 def switchHandler(evt) {
54         log.info evt.value
55
56         // use Event rather than DeviceState because we may be changing DeviceState to only store changed values
57         def recentStates = master.eventsSince(new Date(now() - 4000), [all:true, max: 10]).findAll{it.name == "switch"}
58         log.debug "${recentStates?.size()} STATES FOUND, LAST AT ${recentStates ? recentStates[0].dateCreated : ''}"
59
60         if (evt.physical) {
61                 if (evt.value == "on" && lastTwoStatesWere("on", recentStates, evt)) {
62                         log.debug "detected two taps, turn on other light(s)"
63                         onSwitches1()*.on()
64                 } else if (evt.value == "off" && lastTwoStatesWere("off", recentStates, evt)) {
65                         log.debug "detected two taps, turn off other light(s)"
66                         offSwitches1()*.off()
67                 }
68         }
69         else {
70                 log.trace "Skipping digital on/off event"
71         }
72 }
73
74 private onSwitches1() {
75         (switches + onSwitches).findAll{it}
76 }
77
78 private offSwitches1() {
79         (switches + offSwitches).findAll{it}
80 }
81
82 private lastTwoStatesWere(value, states, evt) {
83         def result = true
84         /*if (states) {
85
86                 log.trace "unfiltered: [${states.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
87                 def onOff = states.findAll { it.physical || !it.type }
88                 log.trace "filtered:   [${onOff.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
89
90                 // This test was needed before the change to use Event rather than DeviceState. It should never pass now.
91                 if (onOff[0].date.before(evt.date)) {
92                         log.warn "Last state does not reflect current event, evt.date: ${evt.dateCreated}, state.date: ${onOff[0].dateCreated}"
93                         result = evt.value == value && onOff[0].value == value
94                 }
95                 else {
96                         result = onOff.size() > 1 && onOff[0].value == value && onOff[1].value == value
97                 }
98         }*/
99         result
100 }