-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgarage-door-helper.groovy
157 lines (143 loc) · 3.65 KB
/
garage-door-helper.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
/**
* Garage Door Helper.
*
* Copyright 2018 Yuxuan "fishy" Wang
*
* 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.
*
*/
definition(
name: "Garage Door Helper",
namespace: "fishy",
author: "Yuxuan Wang",
description: "Use contact sensor to better refresh garage door state",
category: "Safety & Security",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact@2x.png",
)
preferences {
section("Garage Door") {
input(
"door",
"capability.refresh",
title: "Pick a garage door",
multiple: false,
)
}
section("Contact Sensor") {
input(
"contact",
"capability.contactSensor",
title: "Pick a contact sensor",
multiple: false,
required: false,
)
input(
"refresh_rate",
"number",
title: "refresh for every N seconds (default 5)",
required: false,
)
input(
"max_refresh",
"number",
title: "stop refreshing after N minutes (default 2)",
required: false,
)
}
section("Notification") {
input(
"push_notif",
"bool",
title: "Send push notification when Garage Door device handler failed",
)
input(
"phone",
"text",
title: "Phone number to send SMS notification",
required: false,
)
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
if (contact) {
subscribe(contact, "contact", contactHandler)
}
if (push_notif || phone) {
subscribe(door, "lastHttpStatus", httpHandler)
}
}
def getRefreshRate() {
if (refresh_rate) {
return refresh_rate
}
return 5
}
def getMaxRefresh() {
if (max_refresh) {
return max_refresh
}
return 2
}
def forceRefreshGarageState(data) {
def timestamp = now()
log.debug "forceRefreshGarageState: ${new Date()}, timestamp: $timestamp, stops at ${data.stopAt}"
try {
door.refresh()
} catch (Exception e) {
// Sometimes SmartThings platform will throw
// groovy.lang.MissingMethodException: No signature of method: ...
// here, but the door actually got the refresh command.
// If we don't catch the exception here we will stop after the first
// refresh, which is usually not enough.
// As a result, log and ignore.
log.debug "refresh failed with: $e"
}
if (timestamp >= data.stopAt) {
log.debug "stopping refreshing..."
unschedule(forceRefreshGarageState)
} else {
def options = [
overwrite: true,
data: data,
]
runIn(getRefreshRate(), forceRefreshGarageState, options)
}
}
def contactHandler(evt) {
log.debug "contactHandler: $evt.value: $evt, $settings"
if (evt.isStateChange()) {
def timestamp = now() + 60 * 1000 * getMaxRefresh()
def data = [stopAt: timestamp]
forceRefreshGarageState(data)
}
}
def httpHandler(evt) {
log.debug "httpHandler: $evt.value: $evt, $settings"
if (evt.isStateChange() && evt.value == "failed") {
def msg = "${door.displayName} http request failed, please check your settings."
if (push_notif) {
sendPush(msg)
}
if (phone) {
sendSms(phone, msg)
}
}
}