Update double-tap.groovy
[smartapps.git] / third-party / DoubleTapTimedLight.groovy
1 /**
2  *  Double Tap Mode Switch
3  *
4  *  Copyright 2014 George Sudarkoff
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  a      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: "Double Tap Timed Light Switch",
19     namespace: "com.sudarkoff",
20     author: "George Sudarkoff",
21     description: "Turn a light for a period of time when an existing switch is tapped OFF twice in a row.",
22     category: "Convenience",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png"
25 )
26
27 preferences {
28     page (name: "configPage", install: true, uninstall: true) {
29         section ("When this switch is double-tapped OFF...") {
30             input "master", "capability.switch", required: true
31         }
32
33         section ("Turn it on for this many minutes...") {
34             input "duration", "number", required: true
35         }
36
37         section ("Notification method") {
38             input "sendPushMessage", "bool", title: "Send a push notification?"
39         }
40
41         section (title: "More Options", hidden: hideOptionsSection(), hideable: true) {
42             input "phone", "phone", title: "Additionally, also send a text message to:", required: false
43
44             input "customName", "text", title: "Assign a name", required: false
45
46             def timeLabel = timeIntervalLabel()
47             href "timeIntervalInput", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null
48
49             input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
50                 options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
51             input "modes", "mode", title: "Only when mode is", multiple: true, required: false
52         }
53     }
54
55     page (name: "timeIntervalInput", title: "Only during a certain time") {
56         section {
57             input "starting", "time", title: "Starting", required: false
58             input "ending", "time", title: "Ending", required: false
59         }
60     }
61 }
62
63 def installed()
64 {
65     initialize()
66 }
67
68 def updated()
69 {
70     unsubscribe()
71     unschedule()
72     initialize()
73 }
74
75 def initialize()
76 {
77     if (customName) {
78         state.currentAppLabel = customName
79     }
80     subscribe(master, "switch", switchHandler, [filterEvents: false])
81 }
82
83 def switchHandler(evt) {
84     log.info evt.value
85
86     if (allOk) {
87         // use Event rather than DeviceState because we may be changing DeviceState to only store changed values
88         def recentStates = master.eventsSince(new Date(now() - 4000), [all:true, max: 10]).findAll{it.name == "switch"}
89         log.debug "${recentStates?.size()} STATES FOUND, LAST AT ${recentStates ? recentStates[0].dateCreated : ''}"
90
91         if (evt.isPhysical()) {
92             if (evt.value == "on") {
93                 unschedule();
94             }
95             else if (evt.value == "off" && lastTwoStatesWere("off", recentStates, evt)) {
96                 log.debug "detected two OFF taps, turning the light ON for ${duration} minutes"
97                 master.on()
98                 runIn(duration * 60, switchOff)
99                 def message = "${master.label} turned on for ${duration} minutes"
100                 send(message)
101             }
102         }
103         else {
104             log.trace "Skipping digital on/off event"
105         }
106     }
107 }
108
109 def switchOff() {
110     master.off()
111 }
112
113 private lastTwoStatesWere(value, states, evt) {
114     def result = false
115     if (states) {
116         log.trace "unfiltered: [${states.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
117         def onOff = states.findAll { it.isPhysical() || !it.type }
118         log.trace "filtered:   [${onOff.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
119
120         // This test was needed before the change to use Event rather than DeviceState. It should never pass now.
121         if (onOff[0].date.before(evt.date)) {
122             log.warn "Last state does not reflect current event, evt.date: ${evt.dateCreated}, state.date: ${onOff[0].dateCreated}"
123             result = evt.value == value && onOff[0].value == value
124         }
125         else {
126             result = onOff.size() > 1 && onOff[0].value == value && onOff[1].value == value
127         }
128     }
129     result
130 }
131
132 private send(msg) {
133     if (sendPushMessage != "No") {
134         sendPush(msg)
135     }
136
137     if (phone) {
138         sendSms(phone, msg)
139     }
140
141     log.debug msg
142 }
143
144 // execution filter methods
145 private getAllOk() {
146     modeOk && daysOk && timeOk
147 }
148
149 private getModeOk() {
150     def result = !modes || modes.contains(location.mode)
151     result
152 }
153
154 private getDaysOk() {
155     def result = true
156     if (days) {
157         def df = new java.text.SimpleDateFormat("EEEE")
158         if (location.timeZone) {
159             df.setTimeZone(location.timeZone)
160         }
161         else {
162             df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"))
163         }
164         def day = df.format(new Date())
165         result = days.contains(day)
166     }
167     result
168 }
169
170 private getTimeOk() {
171     def result = true
172     if (starting && ending) {
173         def currTime = now()
174         def start = timeToday(starting).time
175         def stop = timeToday(ending).time
176         result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
177     }
178     log.trace "timeOk = $result"
179     result
180 }
181
182 private hideOptionsSection() {
183     (phone || starting || ending || customName || days || modes) ? false : true
184 }
185
186 private hhmm(time, fmt = "h:mm a")
187 {
188     def t = timeToday(time, location.timeZone)
189     def f = new java.text.SimpleDateFormat(fmt)
190     f.setTimeZone(location.timeZone ?: timeZone(time))
191     f.format(t)
192 }
193
194 private timeIntervalLabel() {
195     (starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""
196 }
197