forked from thebearmay/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenGarage.groovy
223 lines (197 loc) · 6.02 KB
/
openGarage.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
/*
* Open Garage
*
* Licensed Virtual 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.
*
* Change History:
*
* Date Who What
* ---- --- ----
* 2023-01--4 thebearmay Initial release
* 2023-01-16 ucdscott Added txtEnable logging, changed debugEnable preference to Hubitat de facto standard logEnable, updated Change History
*
*/
static String version() { return '0.0.2' }
metadata {
definition (
name: "OpenGarage",
namespace: "thebearmay",
author: "Jean P. May, Jr.",
importUrl:"https://raw.githubusercontent.com/thebearmay/hubitat/main/openGarage.groovy"
) {
capability "Actuator"
capability "Polling"
capability "GarageDoorControl"
attribute "door", "string"
attribute "distance", "number"
attribute "vehStatus", "string"
attribute "rssi","number"
//command "close"
command "toggleDoor"
command "rebootDevice"
}
}
preferences {
input("devIP", "string", title: "IP of the Open Garage Device", width:4)
input("devPwd", "password", title: "Device Password", width:4)
input("pollRate","number", title: "Polling interval in seconds (0 to disable)", width:4)
input (name: "txtEnable", type: "bool", title: "Enable descriptionText logging", required: false, defaultValue: false)
input("logEnable", "bool", title: "Enable debug logging", defaultValue: false)
}
def logInfo(msg) {
if (txtEnable) {
log.info msg
}
}
def installed() {
log.trace "Open Garage v${version()} installed()"
device.updateSetting("devIP",[value:"192.168.4.1",type:"string"])
device.updateSetting("devPwd",[value:"opendoor",type:"password"])
device.updateSetting("pollRate",[value:0,type:"number"])
}
def updated(){
log.trace "updated()"
if(logEnable)
runIn(1800,"logsOff")
else
unschedule("logEnable")
if(pollRate > 0)
runIn(pollRate, "poll")
else
unschedule("poll")
}
// HTTP GET to open relay
void open() {
if(txtEnable) log.info "opening relay..."
httpGet(
[
uri: "http://$devIP",
path: "/cc?dkey=$devPwd&open=1",
headers: [
Accept: "application/json"
]
]
) { resp -> }
runIn(10,"poll")
}
// HTTP GET to close relay. Ignored is door is already closed.
void close() {
if(txtEnable) log.info "closing relay..."
httpGet(
[
uri: "http://$devIP",
path: "/cc?dkey=$devPwd&close=1",
headers: [
Accept: "application/json"
]
]
) { resp -> }
runIn(10,"poll")
}
// HTTP GET to toggle relay
void toggleDoor() {
if(txtEnable) log.debug "toggling relay..."
httpGet(
[
uri: "http://$devIP",
path: "/cc?dkey=$devPwd&click=1",
headers: [
Accept: "application/json"
]
]
) { resp -> }
runIn(10,"poll")
}
void rebootDevice() {
if(logEnable) log.debug "rebooting.."
httpGet(
[
uri: "http://$devIP",
path: "/cc?dkey=$devPwd&reboot=1",
headers: [
Accept: "application/json"
]
]
) { resp -> }
runIn(30,"poll")
}
void updateAttr(String aKey, aValue, String aUnit = ""){
aValue = aValue.toString()
sendEvent(name:aKey, value:aValue, unit:aUnit)
}
void poll(){
httpGet(
[
uri: "http://$devIP",
path: "/jc",
headers: [
Accept: "application/json"
]
]
) { resp ->
try{
if (resp.getStatus() == 200){
if (logEnable)
log.debug resp.data
try {
processJc((Map)resp.data)
} catch (e) {
processJc(respToMap(resp.data.toString()))
}
}
}catch (ex) {
log.error "$ex"
}
}
if(pollRate > 0)
runIn(pollRate, "poll")
else
unschedule("poll")
}
HashMap respToMap(String rData){
if(logEnable) log.debug rData
rList = rData.substring(1,rData.size()-1).split(",")
if(logEnable) log.debug rList
rMap = [:]
rList.each{
rMap["${it.substring(0,it.indexOf(":")).trim()}"] = it.substring(it.indexOf(":")+1)
}
if(logEnable) log.debug rMap
return rMap
}
void processJc(dMap){
updateAttr("distance", dMap.dist, "cm")
updateAttr("rssi", dMap.rssi, "dBm")
if(dMap.door.toInteger() == 1 && device.currentValue("door") != "open") {
descriptionText = "${device.displayName} = open"
logInfo descriptionText
updateAttr("door","open")
} else{
if(dMap.door.toInteger() == 0 && device.currentValue("door") != "closed") {
descriptionText = "${device.displayName} = closed"
logInfo descriptionText
updateAttr("door", "closed")
}
}
if(dMap.vehicle.toInteger() == 1 && device.currentValue("vehStatus") != "present"){
descriptionText = "${device.displayName} vehStatus = present"
logInfo descriptionText
updateAttr("vehStatus", "present")
} else {
if(dMap.vehicle.toInteger() == 0 && device.currentValue("vehStatus") != "not present"){
descriptionText = "${device.displayName} vehStatus = not present"
logInfo descriptionText
updateAttr("vehStatus", "not present")
}
}
}
void logsOff(){
device.updateSetting("logEnable",[value:"false",type:"bool"])
}