forked from Nakmans/smartthings-zigbee-device-handlers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVesternet VES-ZB-REM-013 4 Channel Remote Control.groovy
277 lines (261 loc) · 11.9 KB
/
Vesternet VES-ZB-REM-013 4 Channel Remote Control.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
/**
* Vesternet VES-ZB-REM-013 4 Channel Remote Control
*
*/
import groovy.json.JsonOutput
metadata {
definition (name: "Vesternet VES-ZB-REM-013 4 Channel Remote Control", namespace: "Vesternet", author: "Vesternet", mcdSync:true, ocfDeviceType: "x.com.st.d.remotecontroller", mnmn: "Sunricher", vid: "generic-12-button") {
capability "Button"
capability "Sensor"
capability "Battery"
capability "Configuration"
fingerprint profileId: "0104", endpointId: "01", inClusters: "0000,0001,0003,0B05", outClusters: "0003,0004,0005,0006,0008,0019,0300,1000", manufacturer: "Sunricher", model: "ZGRC-KEY-013", deviceJoinName: "Vesternet VES-ZB-REM-013 4 Channel Remote Control"
}
preferences {
input name: "logEnable", type: "bool", title: "Debug"
}
}
def installed() {
device.updateSetting("logEnable", [value: "true", type: "bool"])
logDebug("installed called")
def numberOfButtons = modelNumberOfButtons[device.getDataValue("model")]
sendEvent(getEvent(name: "numberOfButtons", value: numberOfButtons, displayed: false))
sendEvent(getEvent(name: "supportedButtonValues", value: supportedButtonValues.encodeAsJSON(), displayed: false))
for(def buttonNumber : 1..numberOfButtons) {
sendEvent(getEvent(name: "button", value: "pushed", data: [buttonNumber: buttonNumber], isStateChange: true, displayed: false))
}
setupChildDevices()
runIn(1800,logsOff)
}
def updated() {
logDebug("updated called")
log.warn("debug logging is: ${logEnable != false}")
state.clear()
unschedule()
if (logEnable) runIn(1800,logsOff)
}
def configure() {
logDebug("configure called")
def cmds = [ "zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0006 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0008 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0005 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x02 0x01 0x0006 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x02 0x01 0x0008 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x03 0x01 0x0006 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x03 0x01 0x0008 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x04 0x01 0x0006 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x04 0x01 0x0008 {${device.zigbeeId}} {}", "delay 200",
"zdo bind 0x${device.deviceNetworkId} 0x01 0x01 0x0001 {${device.zigbeeId}} {}", "delay 200",
"st cr 0x${device.deviceNetworkId} 0x01 0x0001 0x21 0x20 7200 7200 {0x01}" ]
logDebug("sending ${cmds}")
return cmds
}
void parse(String description) {
logDebug("parse called")
logDebug("got description: ${description}")
def descriptionMap = zigbee.parseDescriptionAsMap(description)
def events = getEvents(descriptionMap)
if (events) {
events.each {
sendEvent(getEvent(it))
}
}
else {
log.warn("Unhandled command: ${descriptionMap}")
}
}
def getEvents(descriptionMap) {
logDebug("getEvents called")
logDebug("got descriptionMap: ${descriptionMap}")
def events = []
def endpoint = descriptionMap.sourceEndpoint ? zigbee.convertHexToInt(descriptionMap.sourceEndpoint) : descriptionMap.endpoint ? zigbee.convertHexToInt(descriptionMap.endpoint) : device.endpointId.toInteger()
if (descriptionMap.cluster == "0006" || descriptionMap.clusterId == "0006" || descriptionMap.clusterInt == 6) {
def switchCommand = "${descriptionMap.command} unknown"
switch (descriptionMap.command) {
case "00":
switchCommand = "off"
break
case "01":
switchCommand = "on"
break
}
logDebug("switch (0006) command: ${switchCommand}")
if (descriptionMap.command == "00" || descriptionMap.command == "01") {
switchCommand = zigbee.convertHexToInt(descriptionMap.command)
def buttonNumber = switchCommand == 1 ? (endpoint * 2) - 1 : (endpoint * 2)
logDebug("button number is ${buttonNumber}")
events.add([name: "button", value: "pushed", data: [buttonNumber: buttonNumber], descriptionText: "Button Number ${buttonNumber} was pushed", isStateChange: true])
sendEventToChild(buttonNumber, "pushed")
}
else {
logDebug("switch (0006) command skipped")
}
}
else if (descriptionMap.cluster == "0008" || descriptionMap.clusterId == "0008" || descriptionMap.clusterInt == 8) {
def levelCommand = "${descriptionMap.command} unknwon"
switch (descriptionMap.command) {
case "05":
levelCommand = "move with on/off"
break
case "07":
levelCommand = "stop"
break
}
logDebug("level (0008) command: ${levelCommand}")
if (descriptionMap.command == "05") {
def levelDirectionData = descriptionMap.data[0];
if (levelDirectionData == "00" || levelDirectionData == "01") {
def levelDirection = "${levelDirectionData} unknown"
switch (levelDirectionData) {
case "00":
levelDirection = "up"
break
case "01":
levelDirection = "down"
break
}
logDebug("level (0008) direction: ${levelDirection}")
levelDirection = zigbee.convertHexToInt(levelDirectionData)
def buttonNumber = levelDirection == 0 ? (endpoint * 2) - 1 : (endpoint * 2)
logDebug("button number is ${buttonNumber}")
logDebug("button event is held")
events.add([name: "button", value: "held", data: [buttonNumber: buttonNumber], descriptionText: "Button Number ${buttonNumber} was held", isStateChange: true])
events.add([name: "lastheld", value: buttonNumber, isStateChange: true, displayed: false])
sendEventToChild(buttonNumber, "held")
}
else {
logDebug("level (0008) direction: ${levelDirectionData} unknown")
}
}
else if (descriptionMap.command == "07") {
def buttonNumber = device.currentValue("lastheld")
if (buttonNumber) {
logDebug("button number was ${buttonNumber}")
logDebug("button event is released")
events.add([name: "button", value: "double", data: [buttonNumber: buttonNumber], descriptionText: "Button Number ${buttonNumber} was released", isStateChange: true])
sendEventToChild(buttonNumber, "double")
}
else {
logDebug("could not determine buttonNumber")
}
}
else {
logDebug("level (0008) command skipped")
}
}
else if (descriptionMap.cluster == "0005" || descriptionMap.clusterId == "0005" || descriptionMap.clusterInt == 5) {
if (endpoint == 1) {
def sceneCommand = "${descriptionMap.command} unknwon"
switch (descriptionMap.command) {
case "05":
sceneCommand = "recall scene"
break
}
if (descriptionMap.command == "05") {
def sceneData = descriptionMap.data[2];
if (sceneData == "01" || sceneData == "02") {
logDebug("scene (0005) command: ${sceneCommand} scene: ${sceneData}")
sceneData = zigbee.convertHexToInt(sceneData)
def buttonNumber = sceneData == 1 ? 9 : 10
logDebug("button number is ${buttonNumber}")
logDebug("button event is pushed")
events.add([name: "button", value: "pushed", data: [buttonNumber: buttonNumber], descriptionText: "Button Number ${buttonNumber} was pushed", isStateChange: true])
sendEventToChild(buttonNumber, "pushed")
}
else {
logDebug("scene (0005) command: ${sceneCommand} scene: ${sceneData} unknown")
}
}
else {
logDebug("scene (0005) command skipped")
}
}
else {
logDebug("got unexpected endpoint in scene command")
}
}
else if (descriptionMap.cluster == "001" || descriptionMap.clusterId == "001" || descriptionMap.clusterInt == 1) {
if (descriptionMap.attrId == "0021" || descriptionMap.attrInt == 33) {
logDebug("power configuration (0001) battery report")
def batteryValue = zigbee.convertHexToInt(descriptionMap.value)
logDebug("battery percentage report is ${batteryValue}")
events.add([name: "battery", value: batteryValue, unit: "%", descriptionText: "${device.displayName} is ${batteryValue}%"])
}
else {
logDebug("power configuration (0001) command skipped")
}
}
else if (descriptionMap.cluster == "8021" || descriptionMap.clusterId == "8021" || descriptionMap.clusterInt == 32801) {
logDebug("zdo bind reponse (0x8021) ${descriptionMap.data}")
}
else {
logDebug("skipped")
}
if (descriptionMap.additionalAttrs) {
logDebug("got additionalAttrs: ${descriptionMap.additionalAttrs}")
descriptionMap.additionalAttrs.each {
it.clusterInt = descriptionMap.clusterInt
it.cluster = descriptionMap.cluster
it.clusterId = descriptionMap.clusterId
it.command = descriptionMap.command
events.add(getEvents(it))
}
}
return events
}
def setupChildDevices(){
logDebug("setupChildDevices called")
if(!childDevices) {
addChildButtons(modelNumberOfButtons[device.getDataValue("model")])
}
}
def addChildButtons(numberOfButtons) {
logDebug("addChildButtons called buttons: ${numberOfButtons}")
for (def endpoint : 1..numberOfButtons) {
try {
String childDni = "${device.deviceNetworkId}:$endpoint"
def componentLabel = (device.displayName.endsWith(' 1') ? device.displayName[0..-2] : (device.displayName + " Button ")) + "${endpoint}"
def child = addChildDevice("Vesternet", "Vesternet VES-ZB-REM-013 4 Channel Remote Control Child Button", childDni, device.getHub().getId(), [
completedSetup: true,
label : componentLabel,
isComponent : true,
componentName : "button$endpoint",
componentLabel: "Button $endpoint"
])
}
catch(Exception e) {
logDebug "Exception: ${e}"
}
}
}
def sendEventToChild(buttonNumber, value) {
logDebug("sendEventToChild called buttonNumber: ${buttonNumber} value: ${value}")
String childDni = "${device.deviceNetworkId}:$buttonNumber"
def child = childDevices.find { it.deviceNetworkId == childDni }
child?.sendButtonEvent(buttonNumber, value)
}
def getModelNumberOfButtons() {
logDebug("getModelNumberOfButtons called")
["ZGRC-KEY-013" : 10]
}
def getSupportedButtonValues() {
logDebug("getSupportedButtonValues called")
def values = ["pushed", "held", "double"] //no released value supported by smartthings
return values
}
def getEvent(event) {
logDebug("getEvent called data: ${event}")
return createEvent(event)
}
def logDebug(msg) {
if (settings.logEnable != false) {
log.debug("${msg}")
}
}
def getLogging() {
return logEnable != false
}
def logsOff() {
log.warn("debug logging disabled")
device.updateSetting("logEnable", [value: "false", type: "bool"])
}