-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZ-Way Dimmer.groovy
639 lines (543 loc) · 19 KB
/
Z-Way Dimmer.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* ----------------------------------------------------------------------------------
*
* `_` `_,_` _' `,`
* -#@@- >O#@@@@u B@@> 8@E
* :)ilc}` `=|}uccccVu}r" VQz `@@#Mhzk= |8M `=v}ucccccuY), `~v}uVVcccccV#@$
* ^Q@#EMqK.I#@QRdMqqMdRQ@@Q, Q@B `@@BqqqW^ W@@` e@@QRdMMMMbEQ@@8: i#@BOMqqqqqqqM#@$
* D@@` )@@x <@@T Q@B `@@q W@@`>@@l :@@z`#@d Q@$
* D@# ?@@@##########@@@} Q@B `@@q W@@`^@@@##########@@@y`#@W Q@$
* 0@# )@@d!::::::::::::` Q@B `@@M W@@`<@@E!::::::::::::``#@b `B@$
* D@# `m@@#bGPP} Q@B `@@q W@@` 3@@BbPPPV y@@QZPPPPPGME#@8=
* *yx .*icywwv )yv }y> ~yT .^icywyL .*]uywwwwycL^-
*
* (c) 2021 Reified Ltd. W: www.reified.co.uk E: [email protected]
*
* ----------------------------------------------------------------------------------
*
* Z-way Dimmer Driver for Hubitat C-7
*
* Calls Z-way REST API to perform switch on/off and dimming on a class 38 device.
*
* For technical details of Z-Way REST API, see https://zwave.me/z-way
*
* Notes: (1) Z-Way seems to require admin privileges (Z-Way 2.2.5) for control.
*
* (2) The API URIs can be found using by clicking the device's cog in Zway,
* then scrolling down to "API commands for developers"
*
* (3) Logging is enabled via UI, but auto-disables after a while (to avoid
* the logs getting accidentally overwhelmed).
*
* (4) Polling is made more complicated in that it *seems* that z-way may
* itself also be polling internally, which can create weird twitchy
* effects if trying to update the Hubitat slider with the latest
* "actual" value too frequently. Also, hubitat slider tracks your
* finger (of course) so reflects thet *requested* value, so if you
* update its state to the "actual" value, it may be a stale "actual"
* value for a short while.
*
* Author: S.J.Morley Date: 2021-02-12 E: [email protected] W: www.reified.co.uk
*
* ----------------------------------------------------------------------------------
*/
import groovy.json.JsonSlurper
/*
* Define the device driver capabilities.
*
*/
metadata
{
definition(name: "Z-way Dimmer", namespace: "Reified", author: "Steve Morley") {
capability "Initialize"
capability "Light"
capability "Switch"
capability "Switch Level"
}
}
/*
* Define the user-specifiable device configuration parameters.
*
*/
preferences
{
section("Device Identity") {
input name: "ipAddr", type: "text", title: "Z-Way IP Address", required: true
input name: "ipPort", type: "number", title: "Z-Way Port", defaultValue: 8083, required: true
input name: "username", type: "text", title: "Z-Way Username", required: false
input name: "password", type: "password", title: "Z-Way Password", required: false
input name: "deviceID", type: "text", title: "Z-Way Switch Device ID", required: true
input name: "instanceID", type: "text", title: "Z-Way Switch Instance ID", defaultValue: 0, required: true
}
section("Device Behaviour") {
input name: "immediateFeedback", type: "bool", title: "Immediate feedback", defaultValue: true
input name: "refreshInterval", type: "number", title: "Actual state polling (seconds, 0=none)", defaultValue: 5, required: true
}
section("Diagnostics") {
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
}
}
/*
* ----------------------------------------------------------------------------------
*
* zWaySendCommand(username, password, ipAddr, ipPort, commandPath)
*
* Send a Z-Way specific HTTP GET to a Z-Way device.
* username - username for BasicAuth login (optional)
* password - password for basicAuth login (optional)
* ipAddr - IP address of the Z-Way device
* ipPort - Port no. for the Z-way device
* commandPath - Device-specific path-part representing the command.
*
* ----------------------------------------------------------------------------------
*/
Object zwaySendCommand(username, password, ipAddr, ipPort, commandPath)
{
def response = null;
String host = "${ipAddr}:${ipPort}";
password = !password ? "" : password; // render null as ""
headerItems = [:];
if (username) { // neither null nor empty
String credentials = "${username}:${password}"
headerItems.put("Authorization", "Basic " + credentials.bytes.encodeBase64().toString());
}
if (logEnable) {
log.debug "${device.label?device.label:device.name}: Zway - " + host + " GET path " + commandPath;
}
URI fullUri = new URI("http://" + host + commandPath);
httpGet(
uri: fullUri,
headers: headerItems
) { resp -> response = resp; }
if (!response.success) {
throw new Exception("Unsuccessful response to HTTP GET of ${commandPath}");
}
if (logEnable) {
log.debug "${device.label?device.label:device.name}: response data: ${response.data}";
}
return response;
}
/*
* ----------------------------------------------------------------------------------
*
* zWaySendCommand(username, password, ipAddr, ipPort, commandPath)
*
* Send a Z-Way specific HTTP GET to the current Z-Way device.
* commandPath - Device-specific path-part representing the command.
*
* ----------------------------------------------------------------------------------
*/
Object zwaySendCommand(commandPath) {
return zwaySendCommand(
settings.username, settings.password,
settings.ipAddr, settings.ipPort,
commandPath
);
}
/*
* ----------------------------------------------------------------------------------
*
* zWayRead(commandPath)
*
* Send a Z-Way specific command to return some data.
* commandPath - Device-specific path-part representing the item to return.
* Returns: Json object for the requested data.
*
* ----------------------------------------------------------------------------------
*/
Object zwayRead(commandPath) {
def response = zwaySendCommand(commandPath);
return response.data;
}
/*
* ----------------------------------------------------------------------------------
*
* zWayWrite(commandPath)
*
* Send a Z-Way specific command to write some data.
* commandPath - Device-specific path-part representing the item write command.
* Returns: Json object for the requested data.
*
* ----------------------------------------------------------------------------------
*/
def void zwayWrite(commandPath) {
zwaySendCommand(commandPath);
}
/*
* ----------------------------------------------------------------------------------
*
* formDevicePathBase(deviceID, instanceID, classID)
*
* Form the path base for the given deviceID/instanceID/classID.
* deviceID - the deviceID as shown in z-way
* instanceID - the device instance ID (usually zero) as shown in z-way.
* classID - the zwave class of device.
* Returns: The path to the device.
*
* ----------------------------------------------------------------------------------
*/
def String zwayFormDevicePathBase(deviceID, instanceID, classID) {
return "/ZAutomation/api/v1/devices/ZWayVDev_zway_${deviceID}-${instanceID}-${classID}";
}
/*
* ----------------------------------------------------------------------------------
*
* zwayReadDeviceState()
*
* Send a Z-Way specific command to read an attribute of the device.
* Returns: The JSON string representing the device's state.
*
* ----------------------------------------------------------------------------------
*/
def Object zwayReadDeviceState()
{
def zwayCommandClass = 38; // zway class MultilevelSwitch
String commandPath = zwayFormDevicePathBase(settings.deviceID, settings.instanceID, zwayCommandClass);
log.debug "${device.label?device.label:device.name}: reading state for device ${settings.deviceID} instance ${settings.instanceID}.";
Object responseDataJson = zwayRead(commandPath);
return responseDataJson;
}
/*
* ----------------------------------------------------------------------------------
*
* int zwayGetLevel()
*
* Reads the device's dimming level.
* For a dimmer this will be 1..100 if on, or 0 for off.
* For a switch this will 255 if on, or 0 for off.
*
* ----------------------------------------------------------------------------------
*/
def Integer zwayGetLevel() {
Object json = zwayReadDeviceState();
return json.data.metrics.level;
}
/*
* ----------------------------------------------------------------------------------
*
* zwaySetLevel(level)
* Writes the devices dimming level.
* (0 means off, 255 means on, 1..100 mean specific dimming level (if a dimmer)
*
* ----------------------------------------------------------------------------------
*/
def zwaySetLevel(level) {
def zwayCommandClass = 38; // ...means a dimmer (MultilevelSwitch)
String commandPath = zwayFormDevicePathBase(settings.deviceID, settings.instanceID, zwayCommandClass);
commandPath += "/command/exact?level=${level}";
zwayWrite(commandPath);
}
/*
* ----------------------------------------------------------------------------------
*
* zwaySetOnOff(on)
* Sets the device to either on or off.
*
* ----------------------------------------------------------------------------------
*/
def zwaySetOnOff(on) {
String command = on ? "on" : "off";
def zwayCommandClass = 38; // ...means a dimmer (MultilevelSwitch)
String commandPath = zwayFormDevicePathBase(settings.deviceID, settings.instanceID, zwayCommandClass);
commandPath += "/command/${command}";
zwayWrite(commandPath);
}
/*
* ----------------------------------------------------------------------------------
*
* zwayGetOnOff(on)
* Gets the device on/off state.
* Returns true if on, otherwise false.
*
* ----------------------------------------------------------------------------------
*/
def zwayGetOnOff() {
Object json = zwayReadDeviceState();
return json.data.metrics.level > 0;
}
/*
* ----------------------------------------------------------------------------------
*
* on()
*
* Framework event: respond to an ON request.
*
* ----------------------------------------------------------------------------------
*/
def on() {
log.debug "### ${device.label?device.label:device.name}: REQUEST FOR ON.";
doOn();
}
/*
* ----------------------------------------------------------------------------------
*
* doOn()
*
* Tell zwave about the off request.
*
* ----------------------------------------------------------------------------------
*/
def doOn() {
try {
zwaySetOnOff(true);
if (settings.immediateFeedback) {
sendEvent(name: "switch", value: "on", isStateChange: true);
}
} catch (Exception e) {
log.warn "${device.label?device.label:device.name}: failed to switch on: ${e.message})";
}
}
/*
* ----------------------------------------------------------------------------------
*
* off()
*
* Framework event: respond to an OFF request.
*
* ----------------------------------------------------------------------------------
*/
def off() {
log.debug "### ${device.label?device.label:device.name}: REQUEST FOR OFF.";
doOff();
}
/*
* ----------------------------------------------------------------------------------
*
* doOff()
*
* Tell zwave about the off request.
*
* ----------------------------------------------------------------------------------
*/
def doOff() {
try {
zwaySetOnOff(false);
if (settings.immediateFeedback) {
sendEvent(name: "switch", value: "off", isStateChange: true);
}
} catch (Exception e) {
log.warn "${device.label?device.label:device.name}: failed to switch off: ${e.message})";
}
}
/*
* ----------------------------------------------------------------------------------
*
* setLevel(selectedLevel)
*
* Tell zwave about the newly requested level.
* selectedLevel - percentage dimmed (0-100).
*
* ----------------------------------------------------------------------------------
*/
def setLevel(level) {
log.debug "### ${device.label?device.label:device.name}: REQUEST TO SET LEVEL ${level}."
doSetLevel(level);
}
/*
* ----------------------------------------------------------------------------------
*
* setLevel(level)
*
* Framework event: request to set a specific dimming level.
* level - percentage dimmed (0-100).
*
* ----------------------------------------------------------------------------------
*/
def doSetLevel(level) {
try {
zwaySetLevel(level);
if (settings.immediateFeedback) {
sendEvent(name: "switch", value: isOn(level) ? "on" : "off", isStateChange: true);
rescheduleStateRefresh(); // to avoid any imminent (stale) update to the UI.
}
} catch (Exception e) {
log.warn "${device.label?device.label:device.name}: failed to set dimming level to ${level}: ${e.message})";
}
}
/*
* ----------------------------------------------------------------------------------
*
* isOn(level)
*
* Determine if the given level means on.
* Returns: true if on, false if off.
*
* ----------------------------------------------------------------------------------
*/
def isOn(level) {
return level > 0;
}
/*
* ----------------------------------------------------------------------------------
*
* writeState(attributeName value, hasChange)
*
* ----------------------------------------------------------------------------------
*/
def writeState(attributeName, value, hasChanged) {
sendEvent(name: attributeName, value: value.toString(), isStateChange: hasChanged);
}
/*
* ----------------------------------------------------------------------------------
*
* showStaticAttributes()
*
* Retrieve some unchanging attributes from the device and show them.
*
* ----------------------------------------------------------------------------------
*/
def showStaticAttributes()
{
// Retrieve non-changing attributes here.
// These are potentially useful, showing aspects of the dimmer,
// so let's retrieve them and show them on the device config page.
// Object json = zwaySendCommand("/ZWaveAPI/Data/*");
// log.debug "Initialisation: retrieved attributes ${json}";
// writeState("zwayDeviceType", json.data.deviceType, true);
// writeState("zwaveVersion", state.data., true);
// writeState("zwaveSecurity", zwayReadAttribute("security"), true);
}
/*
* ----------------------------------------------------------------------------------
*
* initialize()
*
* Framework event: request to initialize the dimmer.
*
* ----------------------------------------------------------------------------------
*/
def initialize() {
scheduleStateRefresh();
showStaticAttributes();
}
/*
*
*/
def refreshStateLevel(level) {
log.debug "Refreshing state level as ${level}"
sendEvent(name: "level", value: level, unit:"%", isStateChange: true);
sendEvent(name: "switch", value: isOn(level) ? "on" : "off", isStateChange: true);
}
/*
*
*/
def scheduleStateRefresh() {
if (settings.refreshInterval > 0) {
runIn(settings.refreshInterval, doStateRefresh);
}
}
/*
*
*/
def unscheduleStateRefresh() {
try {
unschedule(doStateRefresh);
} catch (Exception ex) {
log.warn "${device.label?device.label:device.name}: on trying to unschedule state refresh: ${ex.message}"
}
}
/*
*
*/
def rescheduleStateRefresh() {
unscheduleStateRefresh();
scheduleStateRefresh();
}
/*
*
*/
def stateRefreshLevel()
{
// We only update our own state from what zway tell us. We never do so
// at the point of user interaction as that request might fail. We
// of course want our own state (and UI) to reflect the reality.
synchronized(this)
{
try {
level = zwayGetLevel();
log.debug "State refresh: read level of ${level}";
refreshStateLevel(level);
} catch (Exception e) {
log.warn "${device.label?device.label:device.name}: failed to read level/state: ${e.message})";
}
}
}
/*
*
*/
def doStateRefresh()
{
unscheduleStateRefresh(); // Avoid overlaps.
stateRefreshLevel();
if (settings.refreshInterval > 0) {
scheduleStateRefresh(); // the next one.
}
// TEMPORARY
/*
try {
showStaticAttributes();
} catch (Exception e2) {
log.warn "${device.label?device.label:device.name}: failed to read state attribute(s): ${e2.message})";
}
*/
}
/*
* scheduleAutoLogOff()
*
* Schedule auto off of logging.
*
*/
def scheduleAutoLogOff() {
runIn(1800, doAutoLogOff);
}
/*
* scheduleAutoLogOff()
*
* Schedule auto off of logging.
*
*/
def unscheduleAutoLogOff() {
try {
unschedule(doAutoLogOff);
} catch (Exception ex) {
log.warn "${device.label?device.label:device.name}: on trying to unschedule auto logging off: ${ex.message}"
}
}
/*
* doAutoLogOff()
*
* Schedule auto off of logging.
*
*/
def doAutoLogOff()
{
unscheduleAutoLogOff();
// This is called when the config preferences are updated by the user.
log.info "Configuration read."
log.warn "${device.label?device.label:device.name}: debug logging is: ${settings.logEnable == true}";
// If logging has been enabled, schedule to turn it off in 30 mins.
// Stops logs getting full.
if (settings.logEnable) scheduleAutoLogOff();
}
/*
* updated()
*
* Framework event: called when any device parameters have been updated.
*
*/
def updated() {
log.debug "${device.label?device.label:device.name}: update...";
unschedule();
scheduleStateRefresh();
scheduleAutoLogOff();
}
/*
* logsOff()
*
* Turns off logging when not needed (so that the logs are not flooded).
*
*/
def logsOff() {
log.warn "${device.label?device.label:device.name}: debug logging disabled.";
device.updateSetting("logEnable", [value: "false", type: "bool"]);
}