forked from muxa/hubitat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
konke-zigbee-motion-sensor.groovy
249 lines (218 loc) · 8.79 KB
/
konke-zigbee-motion-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
/**
* Konke ZigBee Motion Sensor
* Device Driver for Hubitat Elevation hub
* Version 1.0.2
*
* Based on code from Robert Morris and ssalahi.
*
* 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.
*
*/
// https://community.hubitat.com/t/konke-motion-sensor/21240/3
import hubitat.zigbee.clusters.iaszone.ZoneStatus
import com.hubitat.zigbee.DataType
metadata {
definition (name: "Konke ZigBee Motion Sensor", namespace: "muxa", author: "Mikhail Diatchenko") {
capability "MotionSensor"
capability "Battery"
capability "PushableButton"
capability "Configuration"
capability "Refresh"
fingerprint endpointId: "01", profileId: "0104", inClusters: "0000,0001,0003,0500,FCC0", outClusters: "0003,FCC0", manufacturer: "Konke", model: "3AFE27010402000D"
}
preferences {
input "motionreset", "number", title: "After motion is detected, wait ___ second(s) until resetting to inactive state. Default = 16 seconds (Hardware resets at 15 seconds)", description: "", range: "1..7200", defaultValue: 16
input "batteryReportingHours", "number", title: "Report battery every ___ hours. Default = 12h (Minimum 2 h)", description: "", range: "2..12", defaultValue: 12
input name: "infoLogging", type: "bool", title: "Enable info message logging", description: ""
input name: "debugLogging", type: "bool", title: "Enable debug message logging", description: ""
}
}
// Parse incoming device messages to generate events
def parse(String description) {
Map map = [:]
// logDebug("Parsing: $description")
if (description?.startsWith('zone status')) {
// logDebug("Zone status: $description")
def zs = zigbee.parseZoneStatus(description)
map = parseIasMessage(zs)
}
else if (description?.startsWith("catchall") || description?.startsWith("read attr"))
{
Map descMap = zigbee.parseDescriptionAsMap(description)
if (descMap.clusterInt == zigbee.POWER_CONFIGURATION_CLUSTER && descMap.attrInt == 0x0020) {
map = parseBattery(descMap.value)
} else if (descMap.command == "07") {
// Process "Configure Reporting" response
if (descMap.data[0] == "00") {
switch (descMap.clusterInt) {
case zigbee.POWER_CONFIGURATION_CLUSTER:
logInfo("Battery reporting configured");
break
default:
log.warn("Unknown reporting configured: ${descMap}");
break
}
} else {
log.warn "Reporting configuration failed: ${descMap}"
}
} else if (descMap.clusterInt == 0x0500 && descMap.attrInt == 0x0002) {
logDebug("Zone status repoted: $descMap")
def zs = new ZoneStatus(Integer.parseInt(descMap.value, 16))
map = parseIasMessage(zs)
} else if (descMap.clusterInt == 0x0500 && descMap.attrInt == 0x0011) {
logInfo("IAS Zone ID: ${descMap.value}")
} else if (descMap.profileId == "0000") {
// ignore routing table messages
} else {
log.warn ("Description map not parsed: $descMap")
}
}
//------IAS Zone Enroll request------//
else if (description?.startsWith('enroll request')) {
logInfo "Sending IAS enroll response..."
return zigbee.enrollResponse()
}
else {
log.warn "Description not parsed: $description"
}
if (map != [:]) {
logInfo(map.descriptionText)
return createEvent(map)
} else
return [:]
}
// helpers -------------------
def parseIasMessage(ZoneStatus zs) {
if ((zs.alarm1 || zs.alarm2) && zs.battery == 0 && zs.trouble == 0) {
// Motion detected
return handleMotion(true)
}
else if (zs.tamper == 1 && zs.battery == 1 && zs.trouble == 1 && zs.ac == 1) {
logDebug "Device button pressed"
map = [
name: 'pushed',
value: 1,
isStateChange: true,
descriptionText: "Device button pushed"
]
}
else {
log.warn "Zone status message not parsed"
if (debugLogging) {
logDebug "zs.alarm1 = $zs.alarm1"
logDebug "zs.alarm2 = $zs.alarm2"
logDebug "zs.tamper = $zs.tamper"
logDebug "zs.battery = $zs.battery"
logDebug "zs.supervisionReports = $zs.supervisionReports"
logDebug "zs.restoreReports = $zs.restoreReports"
logDebug "zs.trouble = $zs.trouble"
logDebug "zs.ac = $zs.ac"
logDebug "zs.test = $zs.test"
logDebug "zs.batteryDefect = $zs.batteryDefect"
}
}
}
private handleMotion(motionActive) {
if (motionActive) {
def timeout = motionreset ?: 16
// The sensor only sends a motion detected message so reset to motion inactive is performed in code
runIn(timeout, resetToMotionInactive)
if (device.currentState('motion')?.value != "active") {
state.motionStarted = now()
}
}
return getMotionResult(motionActive)
}
def getMotionResult(motionActive) {
def descriptionText = "Detected motion"
if (!motionActive) {
descriptionText = "Motion reset to inactive after ${getSecondsInactive()}s"
}
return [
name : 'motion',
value : motionActive ? 'active' : 'inactive',
descriptionText : descriptionText
]
}
def resetToMotionInactive() {
if (device.currentState('motion')?.value == "active") {
def descText = "Motion reset to inactive after ${getSecondsInactive()}s"
sendEvent(
name:'motion',
value:'inactive',
// isStateChange: true,
descriptionText: descText
)
logInfo(descText)
}
}
def getSecondsInactive() {
if (state.motionStarted) {
return Math.round((now() - state.motionStarted)/1000)
} else {
return motionreset ?: 16
}
}
// Convert 2-byte hex string to voltage
// 0x0020 BatteryVoltage - The BatteryVoltage attribute is 8 bits in length and specifies the current actual (measured) battery voltage, in units of 100mV.
private parseBattery(valueHex) {
//logDebug("Battery parse string = ${valueHex}")
def rawVolts = Integer.parseInt(valueHex, 16) / 10 // hexStrToSignedInt(valueHex)/10
def minVolts = voltsmin ? voltsmin : 2.5
def maxVolts = voltsmax ? voltsmax : 3.0
def pct = (rawVolts - minVolts) / (maxVolts - minVolts)
def roundedPct = Math.min(100, Math.round(pct * 100))
def descText = "Battery level is ${roundedPct}% (${rawVolts} Volts)"
//logInfo(descText)
// sendEvent(name: "batteryLevelLastReceived", value: new Date())
def result = [
name: 'battery',
value: roundedPct,
unit: "%",
//isStateChange: true,
descriptionText: descText
]
return result
}
// lifecycle methods -------------
// installed() runs just after a sensor is paired
def installed() {
logInfo("Installing")
sendEvent(name: "numberOfButtons", value: 1, descriptionText: "Device installed")
return refresh()
}
// configure() runs after installed() when a sensor is paired or reconnected
def configure() {
logInfo("Configuring")
return configureReporting()
}
def refresh() {
logInfo("Refreshing")
return zigbee.readAttribute(zigbee.POWER_CONFIGURATION_CLUSTER, 0x0020) // battery voltage
}
// updated() runs every time user saves preferences
def updated() {
logInfo("Updating preference settings")
return configureReporting()
}
// helpers -------------
private def configureReporting() {
def seconds = Math.round((batteryReportingHours ?: 12)*3600)
logInfo("Battery reporting frequency: ${seconds/3600}h")
return zigbee.configureReporting(zigbee.POWER_CONFIGURATION_CLUSTER, 0x0020, DataType.UINT8, seconds, seconds, 0x01)
+ zigbee.readAttribute(zigbee.POWER_CONFIGURATION_CLUSTER, 0x20)
}
private def logDebug(message) {
if (debugLogging) log.debug "${device.displayName}: ${message}"
}
private def logInfo(message) {
if (infoLogging)
log.info "${device.displayName}: ${message}"
}