-
Notifications
You must be signed in to change notification settings - Fork 75
/
ZWN-SC7.SmartApp.groovy
366 lines (314 loc) · 11 KB
/
ZWN-SC7.SmartApp.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Button Controller App for ZWN-SC7
*
* Author: Matt Frank based on VRCS Button Controller by Brian Dahlem, based on SmartThings Button Controller
* Date Created: 2014-12-18
* Last Updated: 2015-11-14
*
* Contributions from erocm1231 @ SmartThings Community
*
*/
definition(
name: "ZWN-SC7 Button Controller",
namespace: "mattjfrank",
author: "Matt Frank, using code from Brian Dahlem",
description: "ZWN-SC7 7-Button Scene Controller Button Assignment App",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/MyApps/Cat-MyApps.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/MyApps/[email protected]"
)
preferences {
page(name: "selectButton")
page(name: "configureButton1")
page(name: "configureButton2")
page(name: "configureButton3")
page(name: "configureButton4")
page(name: "configureButton5")
page(name: "configureButton6")
page(name: "configureButton7")
}
def selectButton() {
dynamicPage(name: "selectButton", title: "First, select which ZWN-SC7", nextPage: "configureButton1", uninstall: configured()) {
section {
input "buttonDevice", "capability.button", title: "Controller", multiple: false, required: true
}
section(title: "Advanced", hideable: true, hidden: true) {
input "debounce", "number", title: "Debounce time in milliseconds", required: true, value: 3000
}
}
}
def configureButton1() {
dynamicPage(name: "configureButton1", title: "1st button, what do you want it to do?",
nextPage: "configureButton2", uninstall: configured(), getButtonSections(1))
}
def configureButton2() {
dynamicPage(name: "configureButton2", title: "2nd button, what do you want it to do?",
nextPage: "configureButton3", uninstall: configured(), getButtonSections(2))
}
def configureButton3() {
dynamicPage(name: "configureButton3", title: "3rd button, what do you want it to do?",
nextPage: "configureButton4", uninstall: configured(), getButtonSections(3))
}
def configureButton4() {
dynamicPage(name: "configureButton4", title: "4th button, what do you want it to do?",
nextPage: "configureButton5", uninstall: configured(), getButtonSections(4))
}
def configureButton5() {
dynamicPage(name: "configureButton5", title: "5th button, what do you want it to do?",
nextPage: "configureButton6", uninstall: configured(), getButtonSections(5))
}
def configureButton6() {
dynamicPage(name: "configureButton6", title: "6th button, what do you want it to do?",
nextPage: "configureButton7", uninstall: configured(), getButtonSections(6))
}
def configureButton7() {
dynamicPage(name: "configureButton7", title: "7th button, what do you want it to do?",
install: true, uninstall: true, getButtonSections(7))
}
def getButtonSections(buttonNumber) {
return {
section(title: "Toggle these...", hidden: hideSection(buttonNumber, "toggle"), hideable: true) {
input "lights_${buttonNumber}_toggle", "capability.switch", title: "switches:", multiple: true, required: false
input "locks_${buttonNumber}_toggle", "capability.lock", title: "locks:", multiple: true, required: false
input "sonos_${buttonNumber}_toggle", "capability.musicPlayer", title: "music players:", multiple: true, required: false
}
section(title: "Turn on these...", hidden: hideSection(buttonNumber, "on"), hideable: true) {
input "lights_${buttonNumber}_on", "capability.switch", title: "switches:", multiple: true, required: false
input "sonos_${buttonNumber}_on", "capability.musicPlayer", title: "music players:", multiple: true, required: false
}
section(title: "Turn off these...", hidden: hideSection(buttonNumber, "off"), hideable: true) {
input "lights_${buttonNumber}_off", "capability.switch", title: "switches:", multiple: true, required: false
input "sonos_${buttonNumber}_off", "capability.musicPlayer", title: "music players:", multiple: true, required: false
}
section(title: "Locks:", hidden: hideLocksSection(buttonNumber), hideable: true) {
input "locks_${buttonNumber}_unlock", "capability.lock", title: "Unlock these locks:", multiple: true, required: false
input "locks_${buttonNumber}_lock", "capability.lock", title: "Lock these locks:", multiple: true, required: false
}
section("Modes") {
input "mode_${buttonNumber}_on", "mode", title: "Activate these modes:", required: false
}
def phrases = location.helloHome?.getPhrases()*.label
if (phrases) {
section("Hello Home Actions") {
log.trace phrases
input "phrase_${buttonNumber}_on", "enum", title: "Activate these phrases:", required: false, options: phrases
}
}
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
subscribe(buttonDevice, "button", buttonEvent)
if (relayDevice) {
log.debug "Associating ${relayDevice.deviceNetworkId}"
if (relayAssociate == true) {
buttonDevice.associateLoad(relayDevice.deviceNetworkId)
}
else {
buttonDevice.associateLoad(0)
}
}
}
def configured() {
return buttonDevice || buttonConfigured(1) || buttonConfigured(2) || buttonConfigured(3) || buttonConfigured(4) || buttonConfigured(5) || buttonConfigured(6) || buttonConfigured(7)
}
def buttonConfigured(idx) {
return settings["lights_$idx_toggle"] ||
settings["locks_$idx_toggle"] ||
settings["sonos_$idx_toggle"] ||
settings["mode_$idx_on"] ||
settings["lights_$idx_on"] ||
settings["locks_$idx_on"] ||
settings["sonos_$idx_on"] ||
settings["lights_$idx_off"] ||
settings["locks_$idx_off"] ||
settings["sonos_$idx_off"]
}
def buttonEvent(evt){
log.debug "buttonEvent"
if(allOk) {
def buttonNumber = evt.jsonData.buttonNumber
def firstEventId = 0
def value = evt.value
//log.debug "buttonEvent: $evt.name = $evt.value ($evt.data)"
log.debug "button: $buttonNumber, value: $value"
def recentEvents = buttonDevice.eventsSince(new Date(now() - debounce)).findAll{it.value == evt.value && it.data == evt.data}
log.debug "Found ${recentEvents.size()?:0} events in past ${debounce/1000} seconds"
if (recentEvents.size() != 0){
log.debug "First Event ID: ${recentEvents[0].id}"
firstEventId = recentEvents[0].id
}
else {
firstEventId = 0
}
log.debug "This Event ID: ${evt.id}"
if(firstEventId == evt.id){
switch(buttonNumber) {
case ~/.*1.*/:
executeHandlers(1)
break
case ~/.*2.*/:
executeHandlers(2)
break
case ~/.*3.*/:
executeHandlers(3)
break
case ~/.*4.*/:
executeHandlers(4)
break
case ~/.*5.*/:
executeHandlers(5)
break
case ~/.*6.*/:
executeHandlers(6)
break
case ~/.*7.*/:
executeHandlers(7)
break
}
} else if (firstEventId == 0) {
log.debug "No events found. Possible SmartThings latency"
} else {
log.debug "Duplicate button press found. Not executing handlers"
}
}
else {
log.debug "NotOK"
}
}
def executeHandlers(buttonNumber) {
log.debug "executeHandlers: $buttonNumber"
def lights = find('lights', buttonNumber, "toggle")
if (lights != null) toggle(lights)
def locks = find('locks', buttonNumber, "toggle")
if (locks != null) toggle(locks)
def sonos = find('sonos', buttonNumber, "toggle")
if (sonos != null) toggle(sonos)
lights = find('lights', buttonNumber, "on")
if (lights != null) flip(lights, "on")
locks = find('locks', buttonNumber, "unlock")
if (locks != null) flip(locks, "unlock")
sonos = find('sonos', buttonNumber, "on")
if (sonos != null) flip(sonos, "on")
lights = find('lights', buttonNumber, "off")
if (lights != null) flip(lights, "off")
locks = find('locks', buttonNumber, "lock")
if (locks != null) flip(locks, "lock")
sonos = find('sonos', buttonNumber, "off")
if (sonos != null) flip(sonos, "off")
def mode = find('mode', buttonNumber, "on")
if (mode != null) changeMode(mode)
def phrase = find('phrase', buttonNumber, "on")
if (phrase != null) location.helloHome.execute(phrase)
}
def find(type, buttonNumber, value) {
def preferenceName = type + "_" + buttonNumber + "_" + value
def pref = settings[preferenceName]
if(pref != null) {
log.debug "Found: $pref for $preferenceName"
}
return pref
}
def flip(devices, newState) {
log.debug "flip: $devices = ${devices*.currentValue('switch')}"
if (newState == "off") {
devices.off()
}
else if (newState == "on") {
devices.on()
}
else if (newState == "unlock") {
devices.unlock()
}
else if (newState == "lock") {
devices.lock()
}
}
def toggle(devices) {
log.debug "toggle: $devices = ${devices*.currentValue('switch')}"
if (devices*.currentValue('switch').contains('on')) {
devices.off()
}
else if (devices*.currentValue('switch').contains('off')) {
devices.on()
}
else if (devices*.currentValue('lock').contains('locked')) {
devices.unlock()
}
else if (devices*.currentValue('lock').contains('unlocked')) {
devices.lock()
}
else {
devices.on()
}
}
def changeMode(mode) {
log.debug "changeMode: $mode, location.mode = $location.mode, location.modes = $location.modes"
if (location.mode != mode && location.modes?.find { it.name == mode }) {
setLocationMode(mode)
}
}
// execution filter methods
private getAllOk() {
modeOk && daysOk && timeOk
}
private getModeOk() {
def result = !modes || modes.contains(location.mode)
log.trace "modeOk = $result"
result
}
private getDaysOk() {
def result = true
if (days) {
def df = new java.text.SimpleDateFormat("EEEE")
if (location.timeZone) {
df.setTimeZone(location.timeZone)
}
else {
df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
}
def day = df.format(new Date())
result = days.contains(day)
}
log.trace "daysOk = $result"
result
}
private getTimeOk() {
def result = true
if (starting && ending) {
def currTime = now()
def start = timeToday(starting).time
def stop = timeToday(ending).time
result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
}
log.trace "timeOk = $result"
result
}
private hhmm(time, fmt = "h:mm a")
{
def t = timeToday(time, location.timeZone)
def f = new java.text.SimpleDateFormat(fmt)
f.setTimeZone(location.timeZone ?: timeZone(time))
f.format(t)
}
private hideOptionsSection() {
(starting || ending || days || modes) ? false : true
}
private hideSection(buttonNumber, action) {
(find("lights", buttonNumber, action) || find("locks", buttonNumber, action) || find("sonos", buttonNumber, action)) ? false : true
}
private hideLocksSection(buttonNumber) {
(find("lights", buttonNumber, "lock") || find("locks", buttonNumber, "unlock")) ? false : true
}
private timeIntervalLabel() {
(starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""
}
private integer(String s) {
return Integer.parseInt(s)
}