forked from codahq/opengarage.io-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenGarage.io-handler.groovy
320 lines (282 loc) · 11.9 KB
/
OpenGarage.io-handler.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
/**
* Copyright 2018 Ben Rimmasch
*
* Based on a device handler by Ian Lindsay which is originally forked from here:
* https://github.com/littlegumSmartHome/opengarage.io-handler
*
* This code can be found at:
* https://github.com/codahq/opengarage.io-handler
*
* 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.
*
*/
preferences {
input("devicekey", "text", title: "Device Key", description: "Your OpenGarage.io device key")
input("ipadd", "text", title: "IP address", description: "The IP address of your OpenGarage.io unit")
input("port", "text", title: "Port", description: "The port of your OpenGarage.io unit")
}
metadata {
definition (name: "OpenGarage.io Handler", namespace: "me.bendy.opengarage", author: "Ben Rimmasch") {
capability "Door Control"
capability "Garage Door Control"
capability "Refresh"
}
tiles (scale: 2) {
standardTile("garagedoor", "device.garagedoor", width: 6, height: 4) {
state "open", label: '${name}', action: "close", icon: "st.doors.garage.garage-open", backgroundColor: "#e54444", nextState: "closed"
state "closed", label: '${name}', action: "open", icon: "st.doors.garage.garage-closed", backgroundColor: "#79b821", nextState: "open"
}
standardTile("vehicle", "device.vehicle", width: 3, height: 2) {
state "absent", label: "Absent", backgroundColor: "#e54444"
state "present", label: "Present", icon: "st.Transportation.transportation10", backgroundColor: "#79b821"
state "na", label: "Open", icon: "st.Transportation.transportation13", backgroundColor: "#f0f066"
}
standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 3, height: 2) {
state "default", action: "refresh.refresh", icon: "st.secondary.refresh"
}
main("garagedoor")
details(["garagedoor", "vehicle", "refresh"])
}
simulator {
// simulator metadata
}
}
def isDebug() {
return false
}
def installed() {
initialize()
}
def updated() {
if (!state.initialized) {
initialize()
}
refresh()
}
def initialize() {
log.info "Initialize triggered"
// initialize state
state.pollingInterval = state.pollingInterval != null ? state.pollingInterval : 5 //time in minutes
state.garageMotionTime = state.garageMotionTime != null ? state.garageMotionTime : 25 //time in seconds
state.doorStatus = state.doorStatus != null ? state.doorStatus : 1 // 1 means open, 0 means closed
state.vehicleStatus = state.vehicleStatus != null ? state.vehicleStatus : 2 // 0 means absent, 1 means present, 2 means na because door is open
state.opening = state.opening != null ? state.opening : 0
state.closing = state.closing != null ? state.closing : 0
state.initialized = 1
refresh()
}
def open() {
log.info "Executing 'open'"
state.opening = 1
api("getstatus", [])
}
def close() {
log.info "Executing 'close'"
state.closing = 1
api("getstatus", [])
}
def refresh() {
log.info "Executing 'refresh'"
unschedule()
state.updatedDate = now();
api("getstatus", [])
customPolling()
}
def customPolling() {
if (!isConfigured()) {
log.info "Polling canceled. Please configure the device!"
return
}
double timesSinceContact = (now() - state.updatedDate).abs() / 60000 //time since last update in minutes
if (isDebug()) log.debug "Polling started. timesSinceContact: ${timesSinceContact}"
if (timesSinceContact > state.pollingInterval) {
if (isDebug()) log.debug "Polling interval exceeded"
refresh()
}
runIn(state.pollingInterval * 60, customPolling) //time in seconds
}
def api(method, args = [], success = {}) {
def methods = [
"getstatus": [gdipadd: "${ipadd}", gdport:"${port}", gdpath:"/jc", gdtype: "GET"],
"openclose": [gdipadd: "${ipadd}", gdport:"${port}", gdpath:"/cc?dkey=${devicekey}&click=1", gdtype: "GET"]
]
def request = methods.getAt(method)
if (isDebug()) log.debug "About to do doRequest with values $request.gdipadd, $request.gdport, $request.gdpath, $request.gdtype, $success"
return doRequest(request.gdipadd, request.gdport, request.gdpath, request.gdtype, success)
}
private doRequest(gdipadd, gdport, gdpath, gdtype, success) {
if (!isConfigured()) {
log.info "Request canceled. Please configure the device!"
return
}
def host = gdipadd
def hosthex = convertIPToHex(host)
def porthex = convertPortToHex(gdport)
if (porthex.length() < 4) {
porthex = "00" + porthex
}
if (isDebug()) log.debug "Port in Hex is $porthex"
if (isDebug()) log.debug "Hosthex is : $hosthex"
if (isDebug()) log.debug "DNI is ${device.deviceNetworkId}"
device.deviceNetworkId = "$hosthex:$porthex"
if (isDebug()) log.debug "And just for good measture: ${getHostAddress()}"
if (isDebug()) log.debug "Path is: $gdpath"
def headers = [:] //"HOST:" + getHostAddress() + ""
headers.put("HOST", "$host:$gdport")
try {
if (isDebug()) log.debug "About to create HubAction"
def hubAction = new physicalgraph.device.HubAction(
[
method: gdtype,
path: gdpath,
headers: headers
]
,device.deviceNetworkId
)
if (isDebug()) log.debug "After HubAction"
return sendHubCommand(hubAction)
}
catch (Exception e)
{
log.debug "Hit exception in doRequest: ${hubAction}"
log.debug e
}
}
def parse(description) {
def events = []
try {
def msg = parseLanMessage(description)
if (isDebug()) log.debug "Start of parse: $msg"
def headersAsString = msg.header // => headers as a string
def headerMap = msg.headers // => headers as a Map
def body = msg.body // => request body as a string
def status = msg.status // => http status code of the response
//def json = msg.json // => any JSON included in response body, as a data structure of lists and maps
//def xml = msg.xml // => any XML included in response body, as a document tree structure
//def data = msg.data // => either JSON or XML in response body (whichever is specified by content-type header in response)
def slurper = new groovy.json.JsonSlurper()
def json = slurper.parseText(msg.body)
if (isDebug()) log.debug json
if (isDebug()) log.debug "Before state.doorStatus: $state.doorStatus"
// open / close event
if(json.result){
if(state.doorStatus){
log.info "Door open so closing"
state.doorStatus = 0
events << createEvent(name: "garagedoor", value: "closed", descriptionText: "${device.name} closed")
events << createEvent(name: "vehicle", value: "na", descriptionText: "${device.name}'s vehicle updated to 'na' while door is moving", displayed: false)
runIn(state.garageMotionTime, refresh)
} else {
log.info "Door closed so opening"
state.doorStatus = 1
events << createEvent(name: "garagedoor", value: "open", descriptionText: "${device.name} opened")
events << createEvent(name: "vehicle", value: "na", descriptionText: "${device.name}'s vehicle updated to 'na' while door is moving", displayed: false)
}
}
//status update request
if (json.mac) {
def action = json.door ? "open" : "closed"
if (state.doorStatus != json.door) {
state.doorStatus = json.door
log.info "Door is ${action}. Refreshing state"
events << createEvent(name: "garagedoor", value: action, descriptionText: "${device.name} updated to ${action}")
}
else {
if (isDebug()) log.debug "Door state already in sync. No change necessary"
events << createEvent(name: "garagedoor", value: action, descriptionText: "${device.name} already synchronized", displayed: false, isStateChange: false)
}
if (isDebug()) log.debug "and closing/opening is ${state.closing}/${state.opening}"
if (json.door) {
if (state.closing == 1) {
state.closing = 0
log.info "Door can close"
sendHubCommand(api("openclose", []))
}
if (state.opening == 1) {
state.opening = 0
log.info "Door is already opened"
}
}
else {
if (state.opening == 1) {
state.opening = 0
log.info "Door can open"
sendHubCommand(api("openclose", []))
}
if (state.closing == 1) {
state.closing = 0
log.info "Door is already closed"
}
}
def present
switch (json.vehicle) {
case 0:
present = "absent"
break
case 1:
present = "present"
break
default:
present = "na"
break
}
if (state.vehicleStatus != json.vehicle) {
state.vehicleStatus = json.vehicle
log.info "Vehicle is ${present}. Refreshing state"
events << createEvent(name: "vehicle", value: present, descriptionText: "${device.name}'s vehicle updated to ${present}")
}
else {
if (isDebug()) log.debug "Vehicle state already in sync. No change necessary"
events << createEvent(name: "vehicle", value: present, descriptionText: "${device.name}'s vehicle already synchronized", displayed: false, isStateChange: false)
}
}
if (isDebug()) log.debug "after state.doorStatus: $state.doorStatus"
}
catch (Exception e)
{
log.debug "Hit exception in parse"
log.debug e
}
return events
}
/*General Helper Methods*/
private isConfigured() {
if (!state.pollingInterval || !state.garageMotionTime) {
initialize()
}
return ipadd && port && devicekey
}
/*To Hex Helper Methods*/
private String convertIPToHex(ipAddress) {
String hex = ipAddress.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join()
if (isDebug()) log.debug "IP address entered is $ipAddress and the converted hex code is $hex"
return hex.toUpperCase()
}
private String convertPortToHex(port) {
String hexport = port.toString().format( '%04x', port.toInteger() )
if (isDebug()) log.debug "Port entered is ${port} and the converted hex port is ${hexport}"
return hexport.toUpperCase()
}
/*Out of Hex Help Methods*/
private Integer convertHexToInt(hex) {
if (isDebug()) log.debug "Convert hex to int: ${hex}"
return Integer.parseInt(hex,16)
}
private String convertHexToIP(hex) {
if (isDebug()) log.debug("Convert hex to ip: $hex") // a0 00 01 6
[convertHexToInt(hex[0..1]),convertHexToInt(hex[2..3]),convertHexToInt(hex[4..5]),convertHexToInt(hex[6..7])].join(".")
}
private getHostAddress() {
def parts = device.deviceNetworkId.split(":")
if (isDebug()) log.debug "Device Network ID: $device.deviceNetworkId"
def ip = convertHexToIP(parts[0])
def port = convertHexToInt(parts[1])
return ip + ":" + port
}