/
gocontrol-contact-sensor.groovy
675 lines (585 loc) · 19.5 KB
/
gocontrol-contact-sensor.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/**
* GoControl Contact Sensor v1.10.3
* (WADWAZ-1)
*
* Author:
* Kevin LaFramboise (krlaframboise)
*
* URL to documentation:
* https://community.smartthings.com/t/release-gocontrol-door-window-sensor-motion-sensor-and-siren-dth/50728?u=krlaframboise
*
* Changelog:
*
* 1.10.3 (10/15/2018)
* - Added support for new mobile app.
*
* 1.10.2 (09/10/2017)
* - Removed old style fingerprint to eliminate conflicts with other generic sensors.
*
* 1.10.1 (07/01/2017)
* - WARNING: This version may temporarily cause the main tile to display the wrong status, but it will correct itself the next time the device wakes up. It doesn't effect the actual values being reported and you can immediately correct the primary tile by opening the settings and tapping Done.
* - Added setting for displaying garage icons.
* - Modified Health Check feature so that it doesn't set the checkin interval until it confirms that the wakeup interval has been changed.
* - Updated colors to match SmartThing's new color theme.
* - Added settings that allows you to specify the number of checkins that have to be missed before reporting the device as offline. Setting it to 10 will practically disable the Health Check feature.
*
* 1.9.2 (04/23/2017)
* - SmartThings broke parse method response handling so switched to sendhubaction.
*
* 1.9.1 (04/20/2017)
* - Added workaround for ST Health Check bug.
*
* 1.9 (04/08/2017)
* - Added child device functionality for external contact.
*
* 1.8.3 (03/12/2017)
* - Adjusted health check to allow it to skip a checkin before going offline.
*
* 1.8.1 (02/21/2017)
* - Added Health Check.
*
* 1.7.1 (01/10/2017)
* - Stopped displaying the internal/external events when the Main Contact Behavior is set to one of the "Only" options.
*
* 1.7 (01/07/2017)
* - Added Configuration command class to initialize the battery and contact values during installation.
* - Added setting that controls the behavior of the main contact based on the state of the internal and/or external contacts.
* - Removed lastPoll attribute and added lastCheckin since the device doesn't support polling.
*
* 1.6.1 (08/02/2016)
* - Fixed iOS UI issue caused by using multiple states with a value tile.
*
* 1.6 (06/22/2016)
* - Added support for the external contact.
* - Added attributes for internal and external contact so you can use them independently, but the main contact reflects the last state of either contact.
*
* 1.5 (06/19/2016)
* - Bug with initial battery reporting.
*
* 1.4.3 (06/17/2016)
* - Fixed issue with battery level being debug logged.
*
* 1.4.2 (05/21/2016)
* - Fixing polling so that it doesn't require forcing state changes or always displaying events.
*
* 1.4.1 (05/5/2016)
* - UI Enhancements
* - Added Debug Logging
* - Fixed default tamper state
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
metadata {
definition (
name: "GoControl Contact Sensor",
namespace: "krlaframboise",
author: "Kevin LaFramboise",
vid:"generic-contact"
) {
capability "Sensor"
capability "Configuration"
capability "Contact Sensor"
capability "Battery"
capability "Tamper Alert"
capability "Refresh"
capability "Health Check"
attribute "internalContact", "enum", ["open", "closed"]
attribute "externalContact", "enum", ["open", "closed"]
attribute "lastCheckin", "string"
attribute "lastOpen", "string"
attribute "lastClosed", "string"
attribute "primaryStatus", "string"
// fingerprint deviceId: "0x2001", inClusters: "0x71,0x85,0x80,0x72,0x30,0x86,0x84"
fingerprint mfr:"014F", prod:"2001", model:"0102"
}
// simulator metadata
simulator {
status "open": "command: 2001, payload: FF"
status "closed": "command: 2001, payload: 00"
}
preferences {
input "mainContactBehavior", "enum",
title: "Main Contact Behavior:",
defaultValue: "Last Changed",
required: false,
options: ["Last Changed Contact (Default)", "Internal Contact Only", "External Contact Only", "Both Contacts Closed", "Both Contacts Open"]
input "checkinInterval", "enum",
title: "Checkin Interval:",
defaultValue: checkinIntervalSetting,
required: false,
displayDuringSetup: true,
options: checkinIntervalOptions.collect { it.name }
input "missedCheckins", "enum",
title: "How many checkins does the device need to miss before it's reported as offline?",
defaultValue: missedCheckinsSetting,
required: false,
displayDuringSetup: true,
options: missedCheckinsOptions.collect { it.name }
input "batteryReportingInterval", "enum",
title: "Battery Reporting Interval:",
defaultValue: batteryReportingIntervalSetting,
required: false,
displayDuringSetup: true,
options: checkinIntervalOptions.collect { it.name }
input "useExternalDevice", "bool",
title: "Create Device for External Sensor?",
defaultValue: false,
displayDuringSetup: true,
required: false
input "useGarageIcons", "bool",
title: "Use Garage Icons?",
defaultValue: false,
displayDuringSetup: true,
required: false
input "debugOutput", "bool",
title: "Enable debug logging?",
defaultValue: false,
displayDuringSetup: true,
required: false
}
// UI tile definitions
tiles(scale: 2) {
multiAttributeTile(name:"contact", type: "generic", width: 6, height: 4){
tileAttribute ("device.primaryStatus", key: "PRIMARY_CONTROL") {
attributeState "closed",
label:'closed',
icon:"st.contact.contact.closed",
backgroundColor:"#00a0dc"
attributeState "open",
label:'open',
icon:"st.contact.contact.open",
backgroundColor:"#e86d13"
attributeState "garage-closed",
label:'Closed',
icon:"st.doors.garage.garage-closed",
backgroundColor:"#00a0dc"
attributeState "garage-open",
label:'Open',
icon:"st.doors.garage.garage-open",
backgroundColor:"#e86d13"
}
}
valueTile("battery", "device.battery", decoration: "flat", width: 2, height: 2){
state "battery", label:'${currentValue}% \nBattery', unit:""
}
standardTile("tampering", "device.tamper", width: 2, height: 2) {
state "detected", label:"Tamper", backgroundColor: "#e86d13"
state "clear", label:"No \nTamper", backgroundColor: "#cccccc"
}
standardTile("refresh", "device.refresh", width: 2, height: 2) {
state "default", label: "Refresh", action: "refresh", icon:"st.secondary.refresh-icon"
}
valueTile("lastCheckin", "device.lastCheckin", decoration: "flat", width: 2, height: 2){
state "lastCheckin", label:'Checked In \n\n${currentValue}'
}
valueTile("lastOpen", "device.lastOpen", decoration: "flat", width: 2, height: 2){
state "lastOpen", label:'Opened \n\n${currentValue}'
}
valueTile("lastClosed", "device.lastClosed", decoration: "flat", width: 2, height: 2){
state "lastClosed", label:'Closed \n\n${currentValue}'
}
valueTile("battery", "device.battery", decoration: "flat", width: 2, height: 2){
state "battery", label:'${currentValue}% \nBattery', unit:""
}
main("contact")
details(["contact", "battery", "tampering", "refresh", "lastOpen", "lastClosed", "lastCheckin"])
}
}
def updated() {
if (!isDuplicateCommand(state.lastUpdated, 3000)) {
state.lastUpdated = new Date().time
logTrace "updated()"
if (useExternalDeviceSetting && !getChildDevices()) {
createChildDevice()
def child = getChildDevice()
if (child) {
log.debug "Updating"
child.update()
}
}
if (!device.currentValue("primaryStatus") || useGarageIconsSetting != state.useGarageIcons) {
state.useGarageIcons = useGarageIconsSetting
sendEvent(createPrimaryStatusEventMap(device.currentValue("contact")))
}
}
}
private void createChildDevice() {
try {
logDebug "Creating Child Device"
def options = [
completedSetup: true,
label: "${device.label} - External",
isComponent: false
]
addChildDevice("krlaframboise", "GoControl External Contact Sensor", childDeviceNetworkId, null, options)
}
catch (e) {
log.warn("You need to install the GoControl External Contact Sensor DTH in order to use this feature.\n$e")
}
}
def uninstalled() {
logTrace "Executing uninstalled()"
devices?.each {
logDebug "Removing ${it.displayName}"
deleteChildDevice(it.deviceNetworkId)
}
}
def childUninstalled() {
// Required to prevent warning on uninstall.
}
private getChildDeviceNetworkId() {
return "${device.deviceNetworkId}-ext"
}
def configure() {
logTrace "configure()"
def cmds = []
if (!device.currentValue("contact")) {
sendEvent(name: "contact", value: "open", isStateChange: true, displayed: false)
sendEvent(createPrimaryStatusEventMap("open"))
}
else if (!device.currentValue("primaryStatus")) {
sendEvent(createPrimaryStatusEventMap(device.currentValue("contact")))
}
if (!state.isConfigured) {
state.isConfigured = true
logTrace "Waiting 1 second because this is the first time being configured"
// Give inclusion time to finish.
cmds << "delay 1000"
}
if (state.checkinIntervalSeconds != (checkinIntervalSettingSeconds)) {
logTrace "Updating wakeup interval"
cmds << wakeUpIntervalSetCmd(checkinIntervalSettingSeconds)
cmds << wakeUpIntervalGetCmd()
}
if (canReportBattery()) {
cmds << batteryGetCmd()
}
return cmds ? delayBetween(cmds, 500) : []
}
// Required for HealthCheck Capability, but doesn't actually do anything because this device sleeps.
def ping() {
logDebug "ping()"
}
// Resets the tamper attribute to clear and requests the device to be refreshed.
def refresh() {
if (device.currentValue("tamper") != "clear") {
sendEvent(getEventMap("tamper", "clear"))
}
else {
logDebug "The battery will be refresh the next time the device wakes up. If you want the battery to update immediately, open the back cover of the device, wait until the red light turns solid, and then put the cover back on."
state.lastBatteryReport = null
}
}
def parse(String description) {
def result = []
if (!isDuplicateCommand(state.lastCheckin, 60000)) {
state.lastCheckin = new Date().time
sendEvent(name: "lastCheckin", value: convertToLocalTimeString(new Date()), displayed: false, isStateChange: true)
}
if (description.startsWith("Err")) {
log.warn "Parse Error: $description"
result << createEvent(descriptionText: "$device.displayName $description", isStateChange: true)
}
else {
def cmd = zwave.parse(description, getCommandClassVersions())
if (cmd) {
result += zwaveEvent(cmd)
}
else {
logDebug "Unable to parse description: $description"
}
}
return result
}
private getCommandClassVersions() {
[
0x20: 1, // Basic
0x30: 2, // Sensor Binary
0x71: 3, // Alarm v1 or Notification v4
0x72: 2, // ManufacturerSpecific
0x80: 1, // Battery
0x84: 2, // WakeUp
0x85: 2, // Association
0x86: 1, // Version (2)
]
}
def zwaveEvent(physicalgraph.zwave.commands.wakeupv2.WakeUpNotification cmd)
{
logTrace "WakeUpNotification: $cmd"
def cmds = []
cmds += configure()
if (cmds) {
cmds << "delay 1000"
}
cmds << wakeUpNoMoreInfoCmd()
return sendResponse(cmds)
}
def zwaveEvent(physicalgraph.zwave.commands.wakeupv2.WakeUpIntervalReport cmd) {
logTrace "WakeUpIntervalReport: $cmd"
def result = []
state.checkinIntervalSeconds = cmd.seconds
// Set the Health Check interval so that it reports offline 5 minutes after it missed the # of checkins specified in the settings.
def threshold = convertOptionSettingToInt(missedCheckinsOptions, missedCheckinsSetting)
def checkInterval = ((cmd.seconds * threshold) + (5 * 60))
result << createEvent(name: "checkInterval", value: checkInterval, displayed: false, data: [protocol: "zwave", hubHardwareId: device.hub.hardwareID])
return result
}
private sendResponse(cmds) {
def actions = []
cmds?.each { cmd ->
actions << new physicalgraph.device.HubAction(cmd)
}
sendHubCommand(actions)
return []
}
private canReportBattery() {
def reportEveryMS = (batteryReportingIntervalSettingMinutes * 60 * 1000)
return (!state.lastBatteryReport || ((new Date().time) - state.lastBatteryReport > reportEveryMS))
}
def zwaveEvent(physicalgraph.zwave.commands.batteryv1.BatteryReport cmd) {
logTrace "BatteryReport: $cmd"
def val = (cmd.batteryLevel == 0xFF ? 1 : cmd.batteryLevel)
if (val > 100) {
val = 100
}
state.lastBatteryReport = new Date().time
def isNew = (device.currentValue("battery") != val)
def result = []
result << createEvent(name: "battery", value: val, unit: "%", display: isNew, isStateChange: isNew)
return result
}
def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd) {
logTrace "BasicReport: $cmd"
def result = []
if (device.currentValue("internalContact")) {
result += handleContactEvent("internalContact", cmd.value)
}
if (device.currentValue("externalContact")) {
result += handleContactEvent("externalContact", cmd.value)
}
return result
}
def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicSet cmd) {
// logTrace "Basic Set: $cmd"
return []
}
def zwaveEvent(physicalgraph.zwave.commands.notificationv3.NotificationReport cmd) {
def result = []
logTrace "NotificationReport: $cmd"
if (cmd.notificationType == 7) {
switch (cmd.event) {
case 0x02:
result += handleContactEvent(cmd.v1AlarmLevel, "internalContact")
break
case 0x03:
result += handleTamperEvent(cmd.v1AlarmLevel)
break
case 0xFE:
result += handleContactEvent(cmd.v1AlarmLevel, "externalContact")
break
}
}
return result
}
private handleTamperEvent(alarmLevel) {
def result = []
if (alarmLevel == 0xFF) {
logDebug "Tamper Detected"
result << createEvent(getEventMap("tamper", "detected"))
}
return result
}
private handleContactEvent(alarmLevel, attr) {
def result = []
def val = (alarmLevel == 0xFF) ? "open" : "closed"
def otherVal = device.currentValue((attr == "internalContact") ? "externalContact" : "internalContact")
def displayed = null
if (settings?.mainContactBehavior?.contains("Only")) {
displayed = false
}
if (attr == "externalContact") {
handleChildContactEvent(val)
}
result << createEvent(getEventMap("$attr", val, displayed))
def mainVal = getMainContactVal(attr, val, otherVal)
if (mainVal) {
result << createEvent(getEventMap("contact", mainVal))
result << createEvent(createPrimaryStatusEventMap(mainVal))
}
sendEvent(name: "last${mainVal?.capitalize()}", value: convertToLocalTimeString(new Date()), displayed: false, isStateChange: true)
return result
}
private createPrimaryStatusEventMap(val) {
if (useGarageIconsSetting) {
val = "garage-${val}"
}
return getEventMap("primaryStatus", val, false)
}
private void handleChildContactEvent(val) {
def child = getChildDevice()
if (child ) {
logDebug "Executing Child ${val.capitalize()}()"
if (val == "open") {
child?.open()
}
else {
child?.close()
}
}
return null
}
private getChildDevice() {
return getChildDevices()?.find { it && "${it}" != "null" }
}
private getMainContactVal(activeAttr, activeVal, otherVal) {
def mainVal
switch (settings?.mainContactBehavior) {
case "Last Changed Contact (Default)":
mainVal = activeVal
break
case "Internal Contact Only":
if (activeAttr == "internalContact") {
mainVal = activeVal
}
break
case "External Contact Only":
if (activeAttr == "externalContact") {
mainVal = activeVal
}
break
case "Both Contacts Closed":
mainVal = (activeVal == "closed" && otherVal == "closed") ? "closed" : "open"
break
case "Both Contacts Open":
mainVal = (activeVal == "open" && otherVal == "open") ? "open" : "closed"
break
default:
mainVal = activeVal
}
return mainVal
}
private getEventMap(eventName, newVal, displayed=null) {
def isNew = device.currentValue(eventName) != newVal
def desc = "${device.displayName} is ${newVal}"
displayed = (displayed != null) ? displayed : isNew
if (displayed) {
logDebug "${desc}"
}
[
name: eventName,
value: newVal,
displayed: displayed,
isStateChange: true,
descriptionText: desc
]
}
def zwaveEvent(physicalgraph.zwave.Command cmd) {
logDebug "Unhandled Command: $cmd"
}
private basicGetCmd() {
return zwave.basicV1.basicGet().format()
}
private batteryGetCmd() {
logTrace "Requesting battery report"
return zwave.batteryV1.batteryGet().format()
}
private wakeUpIntervalSetCmd(val) {
return zwave.wakeUpV2.wakeUpIntervalSet(seconds:val, nodeid:zwaveHubNodeId).format()
}
private wakeUpIntervalGetCmd() {
return zwave.wakeUpV2.wakeUpIntervalGet().format()
}
private wakeUpNoMoreInfoCmd() {
return zwave.wakeUpV2.wakeUpNoMoreInformation().format()
}
// Settings
private getUseExternalDeviceSetting() {
return settings?.useExternalDevice ?: false
}
private getUseGarageIconsSetting() {
return settings?.useGarageIcons ?: false
}
private getCheckinIntervalSettingSeconds() {
return (convertOptionSettingToInt(checkinIntervalOptions, checkinIntervalSetting) * 60) ?: (6 * 60 * 60)
}
private getCheckinIntervalSetting() {
return settings?.checkinInterval ?: findDefaultOptionName(checkinIntervalOptions)
}
private getMissedCheckinsSetting() {
return settings?.missedCheckins ?: findDefaultOptionName(missedCheckinsOptions)
}
private getBatteryReportingIntervalSettingMinutes() {
return convertOptionSettingToInt(checkinIntervalOptions, batteryReportingIntervalSetting) ?: 360
}
private getBatteryReportingIntervalSetting() {
return settings?.batteryReportingInterval ?: findDefaultOptionName(checkinIntervalOptions)
}
private getCheckinIntervalOptions() {
[
[name: "10 Minutes", value: 10],
[name: "15 Minutes", value: 15],
[name: "30 Minutes", value: 30],
[name: "1 Hour", value: 60],
[name: "2 Hours", value: 120],
[name: "3 Hours", value: 180],
[name: formatDefaultOptionName("6 Hours"), value: 360],
[name: "9 Hours", value: 540],
[name: "12 Hours", value: 720],
[name: "18 Hours", value: 1080],
[name: "24 Hours", value: 1440]
]
}
private getMissedCheckinsOptions() {
def items = []
(1..10).each {
items << [
name: (it == 3) ? formatDefaultOptionName("$it") : "$it",
value: it
]
}
return items
}
private convertOptionSettingToInt(options, settingVal) {
return safeToInt(options?.find { "${settingVal}" == it.name }?.value, 0)
}
private formatDefaultOptionName(val) {
return "${val}${defaultOptionSuffix}"
}
private findDefaultOptionName(options) {
def option = options?.find { it.name?.contains("${defaultOptionSuffix}") }
return option?.name ?: ""
}
private getDefaultOptionSuffix() {
return " (Default)"
}
private safeToInt(val, defaultVal=-1) {
return "${val}"?.isInteger() ? "${val}".toInteger() : defaultVal
}
private convertToLocalTimeString(dt) {
def timeZoneId = location?.timeZone?.ID
if (timeZoneId) {
return dt.format("MM/dd/yyyy hh:mm:ss a", TimeZone.getTimeZone(timeZoneId))
}
else {
return "$dt"
}
}
private isDuplicateCommand(lastExecuted, allowedMil) {
!lastExecuted ? false : (lastExecuted + allowedMil > new Date().time)
}
private logDebug(msg) {
if (settings?.debugOutput || settings?.debugOutput == null) {
log.debug "$msg"
}
}
private logTrace(msg) {
// log.trace "$msg"
}