forked from lgkahn/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomeygas.groovy
350 lines (276 loc) · 10.7 KB
/
homeygas.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
/**
* WZB Natural Gas Sensor
*
* 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.
*
* lgk fix issue in porting where configure caused error, also add checkin clear and last status 12/21
* also add gas as well as smoke attribute, and attributes for zonetype enrollment model etc.
* also add multiple fingerprints.
*
* lgk v 3
* add user defined checkin time. in seconds 120-86400 (1 day) cannot go less than 2 minutes/120 or it doesnt work.
* also add debuging
*
* ;gk b4 remove gas and change to naturalGas add capability gas detector.
* lgk 9/22 as istatechange = true to force a clear status to always go in the log.. otherwise when hit refresh nothing
* but battery events go in.
*
*/
metadata {
definition (name: "Homi/Heiman Gas Detector", namespace: "lgkahn", author: "jrhbcn/lgkahn") {
capability "Configuration"
capability "Smoke Detector"
capability "Sensor"
capability "Refresh"
capability "Gas Detector"
//command "enrollResponse"
attribute "zoneType", "string"
attribute "enrollment", "string"
attribute "manufacture", "string"
attribute "model", "string"
attribute "lastUpdate", "string"
//attribute "gas", "string"
fingerprint profileID: "0104", deviceID: "0402", inClusters: "0000,0003,0500,0009", outClusters: "0019"
fingerprint profileID: "0104", deviceID: "0402", inClusters: "0000,0003,0500,0B05", outClusters: "0019"
fingerprint profileID: "0104", deviceID: "12", inClusters: "0000,0003,0500,0009", outClusters: "0003,0019"
fingerprint profileID: "0104", deviceID: "176", inClusters: "0000,0003,0500,0B05", outClusters: "0019"
}
preferences {
input(name: "checkInTime", type: "Integer", title: "Check in Time?", description: "Check in Every how many Seconds (range 120-86400 (1 day)) ?", range: "60-86400", defaultValue: 1800, required: true);
input(name: "debug", type: "bool", title: "Debugging ?", defaultValue: false, required: true)
}
}
def parse(String description) {
if (settings.debug) log.debug "description: $description"
def now = new Date().format('MM/dd/yyyy h:mm a',location.timeZone)
sendEvent(name: "lastUpdate", value: now)
Map map = [:]
if (description?.startsWith('catchall:')) {
map = parseCatchAllMessage(description)
}
else if (description?.startsWith('read attr -')) {
map = parseReportAttributeMessage(description)
}
else if (description?.startsWith('zone status')) {
map = parseIasMessage(description)
}
//log.debug "Parse returned $map"
def result = map ? createEvent(map) : null
//log.debug "result = $result"
if (description?.startsWith('enroll request')) {
List cmds = enrollResponse()
log.debug "enroll response: ${cmds}"
result = cmds?.collect { new hubitat.device.HubAction(it) }
}
return result
}
private Map parseCatchAllMessage(String description) {
Map resultMap = [:]
def cluster = zigbee.parse(description)
if (shouldProcessMessage(cluster)) {
log.debug "Parse $cluster"
}
return resultMap
}
private boolean shouldProcessMessage(cluster) {
// 0x0B is default response indicating message got through
// 0x07 is bind message
boolean ignoredMessage = cluster.profileId != 0x0104 ||
cluster.command == 0x0B ||
cluster.command == 0x07 ||
(cluster.data.size() > 0 && cluster.data.first() == 0x3e)
return !ignoredMessage
}
private Map parseReportAttributeMessage(String description) {
// lgk new
descMap = zigbee.parseDescriptionAsMap(description)
if ((descMap?.cluster == "0500" && descMap.attrInt == 0x0001) && (descMap.value == '002B'))
{ //Zone Type
log.debug "Zone Type is Gas Sensor"
sendEvent(name: "zoneType", value: "Gas Sensor")
}
else if ((descMap?.cluster == "0500" && descMap.attrInt == 0x0000) && (descMap.value == '01'))
{ //Zone State
log.debug "Zone State is enrolled"
sendEvent(name: "enrollment", value: "Enrolled")
}
else if ((descMap?.cluster == "0500" && descMap.attrInt == 0x0002) && ((descMap.value == '0030') || (descMap.value == '0020')))
{ //Zone Status
log.debug "${device.displayName} is clear"
sendEvent(name: "smoke", value: "clear", isStateChange: true)
sendEvent(name: "naturalGas", value: "clear", isStateChange: true)
}
else if (descMap?.cluster == "0000" && descMap.attrInt == 0x0004)
{ //Manufacture
log.debug "Manuf: ${descMap.value}"
sendEvent(name: "manufacture", value: descMap.value)
}
else if (descMap?.cluster == "0000" && descMap.attrInt == 0x0005)
{ //Model
log.debug "Model: ${descMap.value}"
sendEvent(name: "model", value: descMap.value)
}
else if ((descMap?.cluster == "0502" && descMap.attrInt == 0x0000))
{ //Alarm Max Duration
def int alarmMinutes = Integer.parseInt(descMap.value,16)
log.debug "Max Alarm Duration is ${alarmMinutes} seconds"
}
}
def refresh() {
if (settings.debug) log.debug "Refreshing..."
def refreshCmds = []
refreshCmds +=
zigbee.readAttribute(0x0500, 1) + // IAS ZoneType
zigbee.readAttribute(0x0500, 0) + // IAS ZoneState
zigbee.readAttribute(0x0500, 2) + // IAS ZoneStatus
// zigbee.readAttribute(0x0502, 0x0000) + // Alarm Max Duration // this doesnt work for this driver
// zigbee.readAttribute(0x0000, 7) + // power source power is irrelevant always ac
// zigbee.readAttribute(0x0009, 7) + // not sure what this cluster do yet
zigbee.readAttribute(0x0000, 4) + // manufacture
zigbee.readAttribute(0x0000, 5) // // model indentification
return refreshCmds
}
private Map parseIasMessage(String description) {
List parsedMsg = description.split(' ')
String msgCode = parsedMsg[2]
Map resultMap = [:]
switch(msgCode) {
case '0x0020': // Clear
log.debug "Alert: Gas Cleared!"
resultMap = getSmokeResult('clear')
break
case '0x0030': // Clear // 30 is checkin
log.info "Checkin"
resultMap = getCheckInResult('clear')
break
case '0x0021': // ??
break
case '0x0022': // Smoke
log.debug "Alert: Gas Detected!"
resultMap = getSmokeResult('detected')
break
case '0x0023': // ??
break
case '0x0024': // ??
break
case '0x0025': // ??
break
case '0x0026': // Trouble/Failure
break
case '0x0028': // Test Mode
break
}
return resultMap
}
private Map getCheckInResult(value) {
if (settings.debug) log.debug 'Checkin/Status'
def linkText = getLinkText(device)
def descriptionText = "${linkText} has checked In: $value"
def gasmap = [
name : 'naturalGas',
value : value,
descriptionText : descriptionText
]
sendEvent gasmap
return [
name: 'smoke',
value: value,
descriptionText: descriptionText
]
}
private Map getSmokeResult(value) {
if (settings.debug) log.debug 'Gas Status'
def linkText = getLinkText(device)
def descriptionText = "${linkText} is $value"
def gasmap = [
name : 'naturalGas',
value : value,
descriptionText : descriptionText
]
sendEvent gasmap
return [
name: 'smoke',
value: value,
descriptionText: descriptionText
]
}
def updated()
{
configure()
}
def configure() {
unschedule()
def String ctime = settings.checkInTime
def Integer ctimeint = ctime.toInteger()
if (ctimeint < 120)
{
log.debug "Reset checkIn/Reporting Time to 120"
device.updateSetting("checkInTime", [type: "integer", value: 120]);
ctimeint = 120
ctime = ctimeint.toString()
}
log.info "Check in time: $ctime secs."
def debug = settings.debug
if (debug)
log.debug "In Configure"
if (debug)
{
log.info "Debugging is on , will automatically turn off in 30 minutes."
runIn(1800, logDebugOff);
}
else log.info "Debugging is off."
String zigbeeId = swapEndianHex(device.zigbeeId)
if (debug) log.debug "Confuguring Reporting, IAS CIE, and Bindings."
def configCmds = [
"zcl global write 0x500 0x10 0xf0 {${zigbeeId}}", "delay 200",
//"send 0x${device.deviceNetworkId} 1 ${endpointId}", "delay 1500", endpoint is only 1 so this is null and already set below
"raw 0x500 {01 23 00 00 00}", "delay 200",
"send 0x${device.deviceNetworkId} 1 1", "delay 1500",
]
configCmds += zigbee.configureReporting(0x0500, 0x0002, 0x19, 0, ctimeint, 0x00) // try 2 min
if (debug) log.debug "configure: Write IAS CIE -- $configCmds"
// schedule refresh
runIn(2,"refresh")
return configCmds // send refresh cmds as part of config
}
def enrollResponse() {
if (settings.debug) log.debug "Sending enroll response"
[
"raw 0x500 {01 23 00 00 00}", "delay 200",
"send 0x${device.deviceNetworkId} 1 ${endpointId}"
]
}
private hex(value) {
new BigInteger(Math.round(value).toString()).toString(16)
}
private String swapEndianHex(String hex) {
reverseArray(hex.decodeHex()).encodeHex()
}
private byte[] reverseArray(byte[] array) {
int i = 0;
int j = array.length - 1;
byte tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
return array
}
void logDebugOff() {
//
// runIn() callback to disable "Debug" logging after 30 minutes
// Cannot be private
//
log.info "Turning off debugging."
device.updateSetting("debug", [type: "bool", value: false]);
}