/
VRCZ4-MRZ-DE.groovy
375 lines (295 loc) · 12.6 KB
/
VRCZ4-MRZ-DE.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* ZWave Scene Control to button converter
*
* 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.
*
*/
import groovy.transform.Field
import groovy.json.JsonOutput
metadata {
// Automatically generated. Make future change here.
definition (name: "Z-Wave Scene To Button", namespace: "jarcher", author: "Jim Archer") {
capability "Actuator"
capability "Button"
capability "Configuration"
capability "Sensor"
attribute "currentButton", "STRING"
attribute "numButtons", "STRING"
fingerprint deviceId: "0x0100", inClusters:"0x85, 0x2D, 0x7C, 0x77, 0x82, 0x73, 0x86, 0x72, 0x91, 0xEF, 0x2B, 0x2C"
}
simulator {
status "button 1 pushed": "command: 2B01, payload: 01 FF"
status "button 2 pushed": "command: 2B01, payload: 02 FF"
status "button 3 pushed": "command: 2B01, payload: 03 FF"
status "button 4 pushed": "command: 2B01, payload: 04 FF"
status "button released": "command: 2C02, payload: 00"
}
preferences {
input "forceupdate", "bool", title: "Force Settings Update/Refresh?", description: "Toggle to force settings update", required: false
}
tiles {
standardTile("button", "device.button", width: 2, height: 2) {
state "default", label: " ", icon: "st.unknown.zwave.remote-controller", backgroundColor: "#ffffff"
state "button 1", label: "1", icon: "st.Weather.weather14", backgroundColor: "#79b821"
state "button 2", label: "2", icon: "st.Weather.weather14", backgroundColor: "#79b821"
state "button 3", label: "3", icon: "st.Weather.weather14", backgroundColor: "#79b821"
state "button 4", label: "4", icon: "st.Weather.weather14", backgroundColor: "#79b821"
state "dim up", label: " ", icon: "st.thermostat.thermostat-up", backgroundColor: "#79b821"
state "dim down", label: " ", icon: "st.thermostat.thermostat-down", backgroundColor: "#79b821"
}
// Configure button. Syncronize the device capabilities that the UI provides
standardTile("configure", "device.configure", inactiveLabel: false, decoration: "flat") {
state "configure", label:'', action:"configuration.configure", icon:"st.secondary.configure"
}
main "button"
details (["button", "configure"])
}
}
// parse events into attributes
def parse(String description)
{
log.debug "Parsing '${description}'"
def result = null
def cmd = zwave.parse(description)
if (cmd) {
result = zwaveEvent(cmd)
}
return result
}
// Handle a button being pressed
def buttonEvent(button)
{
log.debug("Entered buttonEvent() method");
button = button as Integer
def result = []
log.debug("Button number: ${button}");
updateState("currentButton", "$button")
if (button > 0)
{
// update the device state, recording the button press
log.debug("Creating button push event for button: ${button}");
result << createEvent(name: "button", value: "pushed", data: [buttonNumber: button],
descriptionText: "$device.displayName button $button was pushed", isStateChange: true)
// turn off the button LED
result << response(zwave.sceneActuatorConfV1.sceneActuatorConfReport(dimmingDuration: 255, level: 255, sceneId: 0))
}
else {
// update the device state, recording the button press
// result = createEvent(name: "button", value: "default", descriptionText: "$device.displayName button was released", isStateChange: true)
// Let's ignore the button release for now
log.debug("Ignoreing button push release event for button: ${button}");
createEvent([:])
//result << response(zwave.sceneActuatorConfV1.sceneActuatorConfReport(dimmingDuration: 255, level: 255, sceneId: 0))
}
result
//delayBetween(result, 200)
}
// A zwave command for a button press was received
def zwaveEvent(physicalgraph.zwave.commands.sceneactivationv1.SceneActivationSet cmd)
{
log.debug("Entered: zwaveEvent(physicalgraph.zwave.commands.sceneactivationv1.SceneActivationSet cmd)");
// The controller likes to repeat the command... ignore repeats
if (state.lastScene == cmd.sceneId && (state.repeatCount < 4) && (now() - state.repeatStart < 500))
{
log.debug "Button ${cmd.sceneId} repeat ${state.repeatCount}x ${now()}"
state.repeatCount = state.repeatCount + 1
createEvent([:])
}
else
{
// If the button was really pressed, store the new scene and handle the button press
log.debug("This appears to be a valid press of button: $cmd.sceneId");
state.lastScene = cmd.sceneId
state.lastLevel = 0
state.repeatCount = 0
state.repeatStart = now()
buttonEvent(cmd.sceneId)
}
}
// A scene command was received -- it's probably scene 0, so treat it like a button release
def zwaveEvent(physicalgraph.zwave.commands.sceneactuatorconfv1.SceneActuatorConfGet cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.commands.sceneactuatorconfv1.SceneActuatorConfGet() method");
buttonEvent(cmd.sceneId)
}
// The controller sent a scene activation report. Log it, but it really shouldn't happen.
def zwaveEvent(physicalgraph.zwave.commands.sceneactuatorconfv1.SceneActuatorConfReport cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.commands.sceneactuatorconfv1.SceneActuatorConfReport method");
log.debug "Scene activation report"
log.debug "Scene ${cmd.sceneId} set to ${cmd.level}"
createEvent([:])
}
// Configuration Reports are replys to configuration value requests... If we knew what configuration parameters
// to request this could be very helpful.
def zwaveEvent(physicalgraph.zwave.commands.configurationv1.ConfigurationReport cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.commands.configurationv1.ConfigurationReport() method");
createEvent([:])
}
// The VRC supports hail commands, but I haven't seen them.
def zwaveEvent(physicalgraph.zwave.commands.hailv1.Hail cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.commands.hailv1.Hail() method");
createEvent([name: "hail", value: "hail", descriptionText: "Switch button was pressed", displayed: false])
}
// Update manufacturer information when it is reported
def zwaveEvent(physicalgraph.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport() method");
if (state.manufacturer != cmd.manufacturerName) {
updateDataValue("manufacturer", cmd.manufacturerName)
}
createEvent([:])
}
// Association Groupings Reports tell us how many groupings the device supports. This equates to the number of
// buttons/scenes in the VRCS
def zwaveEvent(physicalgraph.zwave.commands.associationv2.AssociationGroupingsReport cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.commands.associationv2.AssociationGroupingsReport() method");
def response = []
log.debug "${getDataByName("numButtons")} buttons stored"
if (getDataByName("numButtons") != "$cmd.supportedGroupings") {
updateState("numButtons", "$cmd.supportedGroupings")
log.debug "${cmd.supportedGroupings} groups available"
response << createEvent(name: "numButtons", value: cmd.supportedGroupings, displayed: false)
response << associateHub()
}
else {
response << createEvent(name: "numButtons", value: cmd.supportedGroupings, displayed: false)
}
return response
}
// Handles all Z-Wave commands we don't know we are interested in
def zwaveEvent(physicalgraph.zwave.Command cmd)
{
log.debug("Entered zwaveEvent(physicalgraph.zwave.Command() method");
createEvent([:])
}
// Create a list of the configuration commands to send to the device
def configurationCmds()
{
log.debug("Entered configurationCmds() method");
// Always check the manufacturer and the number of groupings allowed
def commands = [
zwave.manufacturerSpecificV1.manufacturerSpecificGet().format(),
zwave.associationV1.associationGroupingsGet().format()
]
commands << associateHub()
// Reset to sceneId 0 (no scene) initially to turn off all LEDs.
commands << zwave.sceneActuatorConfV1.sceneActuatorConfReport(dimmingDuration: 255, level: 255, sceneId: 0).format()
delayBetween(commands)
}
// Configure the device
def configure()
{
log.debug("Entered configure() method");
def cmd = configurationCmds()
log.debug("Sending configuration: ${cmd}")
return cmd
}
def initialize() {
sendEvent(name: "numberOfButtons", value: 4, displayed: false)
sendEvent(name: "button", value: "pushed", data: [buttonNumber: 1], displayed: false)
sendEvent(name: "supportedButtonValues", value:JsonOutput.toJson(["pushed","held"]), displayed:false)
}
def updated() {
log.debug "$device.displayName updated()"
sendEvent(name: "supportedButtonValues", value:JsonOutput.toJson(["pushed","held"]), displayed:false)
sendEvent(name: "numberOfButtons", value: 4, displayed: false)
configure()
}
// Associate a load with the button, or clear the association if node id = 0
//
// nodeId: a hex string, ie 4E for the Z-Wave node number
def associateLoad(String nodeId)
{
/* */
log.debug("Entered associateLoad() method");
//def node = integerhex(nodeId)
//if (node != 0) {
// updateState("associatedLoad", "1")
// updateState("associatedLoadId", nodeId)
// log.debug "Node $nodeId associated with button 1"
//}
//else {
updateState("associatedLoad", "0")
// log.debug "No nodes associated with button 1"
//}
configure()
/* */
}
// Associate the hub with the buttons on the device, so we will get status updates
def associateHub()
{
log.debug("Entered associateHub() method");
def commands = []
// Loop through all the buttons on the controller
for (def buttonNum = 1; buttonNum <= integer(getDataByName("numButtons")); buttonNum++) {
// Make sure the scene controller is configured with one scene per group, with the same number.
commands << zwave.sceneControllerConfV1.sceneControllerConfSet(groupId:buttonNum, sceneId:buttonNum).format()
// For the first button:
if (buttonNum == 1) {
// If there is an associated load
if (getDataByName("associatedLoad") != "0" && getDataByName("associatedLoad") != null) {
// Unassociate the hub from button 0 and associate the load. That way we won't get button presses for the switch
commands << zwave.associationV1.associationRemove(groupingIdentifier: buttonNum, nodeId: zwaveHubNodeId).format()
commands << zwave.associationV1.associationSet(groupingIdentifier: buttonNum, nodeId: integerhex(getDataByName("associatedLoadId"))).format()
}
else {
// If there is no associated load, associate the hub with button 1 so we receive button presses
commands << zwave.associationV1.associationSet(groupingIdentifier: buttonNum, nodeId: zwaveHubNodeId).format()
// If there is an associatedLoad stored in the device, disassociate it from the button to avoid weirdness.
if (getDataByName("associatedLoadId")) {
commands << zwave.associationV1.associationRemove(groupingIdentifier: buttonNum, nodeId: integerhex(getDataByName("associatedLoadId"))).format()
}
}
}
// For the other buttons:
else {
// Associate the hub with the button so we will get status updates
commands << zwave.associationV1.associationSet(groupingIdentifier: buttonNum, nodeId: zwaveHubNodeId).format()
}
}
return commands
}
// Update State
// Store mode and settings
def updateState(String name, String value)
{
log.debug("Entered updateState() method");
state[name] = value
device.updateDataValue(name, value)
}
// Get Data By Name
// Given the name of a setting/attribute, lookup the setting's value
def getDataByName(String name)
{
state[name] ?: device.getDataValue(name)
}
//Stupid conversions
// convert a double to an integer
def integer(double v) {
return v.toInteger()
}
// convert a hex string to integer
def integerhex(String v) {
if (v == null) {
return 0
}
return Integer.parseInt(v, 16)
}
// convert a hex string to integer
def integer(String v) {
if (v == null) {
return 0
}
return Integer.parseInt(v)
}