-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
zooz-universal-relay.groovy
901 lines (710 loc) · 23.9 KB
/
zooz-universal-relay.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*
* Zooz Universal Relay - ZEN17 v1.0
*
* Changelog:
*
* 1.0 (03/06/2021)
* - Initial Release
*
*
* Copyright 2021 Zooz
*
* 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
@Field static Map commandClassVersions = [
0x20: 1, // Basic
0x25: 1, // Switch Binary
0x30: 2, // SensorBinary
0x55: 1, // Transport Service
0x59: 1, // AssociationGrpInfo
0x5A: 1, // DeviceResetLocally
0x5E: 2, // ZwaveplusInfo
0x60: 3, // Multi Channel
0x6C: 1, // Supervision
0x70: 2, // Configuration
0x72: 2, // ManufacturerSpecific
0x73: 1, // Powerlevel
0x7A: 2, // Firmware Update Md
0x85: 2, // Association
0x86: 1, // Version
0x8E: 2, // Multi Channel Association
0x98: 1, // Security 0
0x9F: 1 // Security 2
]
@Field static Map associationGroups = [2:"associationGroupTwo", 3:"associationGroupThree"]
@Field static List<Map> childInputTypes = [
[configVal: 10, attribute: "switch", active: "on", inactive: "off", sensorType: 0, name: "Simulated Switch", namespace: "smartthings/testing"],
[configVal: 4, attribute: "water", active: "wet", inactive: "dry", sensorType: 6, name: "Simulated Water Sensor", namespace: "smartthings/testing"],
[configVal: 9, attribute: "smoke", active: "detected", inactive: "clear", sensorType: 4, name: "Simulated Smoke Alarm", namespace: "smartthings/testing"],
[configVal: 6, attribute: "motion", active: "active", inactive: "inactive", sensorType: 12, name: "Simulated Motion Sensor", namespace: "smartthings/testing"],
[configVal: 7, attribute: "contact", active: "open", inactive: "closed", sensorType: 10, name: "Simulated Contact Sensor", namespace: "smartthings/testing"]
]
metadata {
definition (
name: "Zooz Universal Relay",
namespace: "Zooz",
author: "Kevin LaFramboise (@krlaframboise)",
ocfDeviceType: "oic.d.switch",
vid: "6e4ebb68-977d-3da4-a6b6-fd6d5074128e",
mnmn: "SmartThingsCommunity"
) {
capability "Actuator"
capability "Switch"
capability "Outlet"
capability "Refresh"
capability "Health Check"
capability "Configuration"
capability "platemusic11009.firmware"
capability "platemusic11009.associationGroupTwo"
capability "platemusic11009.associationGroupThree"
capability "platemusic11009.syncStatus"
attribute "lastCheckIn", "string"
fingerprint manufacturer: "027A", prod: "7000", model: "A00A", deviceJoinName: "Zooz Universal Relay"
}
simulator { }
preferences {
configParams.each { param ->
if (param.options) {
input "configParam${param.num}", "enum",
title: "${param.name}:",
required: false,
displayDuringSetup: false,
options: param.options
}
else if (param.range) {
input "configParam${param.num}", "number",
title: "${param.name}:",
required: false,
displayDuringSetup: false,
range: param.range
}
}
input "debugLogging", "enum",
title: "Logging:",
required: false,
defaultValue: "1",
options: ["0":"Disabled", "1":"Enabled [DEFAULT]"]
input "assocInstructions", "paragraph",
title: "Device Associations",
description: "Associations are an advance feature that allow you to establish direct communication between Z-Wave devices. To make this remote control another Z-Wave device, get that device's Device Network Id from the My Devices section of the IDE and enter the id in one of the settings below. Group 2 and Group 3 supports up to 5 associations and you can use commas to separate the device network ids.",
required: false
input "assocDisclaimer", "paragraph",
title: "WARNING",
description: "If you add a device's Device Network ID to the list below and then remove that device from SmartThings, you MUST come back and remove it from the list below. Failing to do this will substantially increase the number of z-wave messages being sent by this device and could affect the stability of your z-wave mesh.",
required: false
input "group2AssocDNIs", "string",
title: "Enter Device Network IDs for Group 2 Relay 1 Association:",
required: false
input "group3AssocDNIs", "string",
title: "Enter Device Network IDs for Group 3 Relay 2 Association:",
required: false
}
}
def installed () {
createChildRelays()
initialize()
}
def updated() {
if (!isDuplicateCommand(state.lastUpdated, 1000)) {
state.lastUpdated = new Date().time
initialize()
executeConfigureCmds()
}
}
void initialize() {
state.debugLoggingEnabled = (safeToInt(settings?.debugOutput, 1) != 0)
sendEventIfNew("switch", "off", false)
if (!device.currentValue("checkInterval")) {
def checkInterval = (6 * 60 * 60) + (5 * 60)
sendEvent(name: "checkInterval", value: checkInterval, displayed: false, data: [protocol: "zwave", hubHardwareId: device.hub.hardwareID])
}
if (device.currentValue("associationGroupTwo") == null) {
sendEvent(name: "associationGroupTwo", value: "")
}
if (device.currentValue("associationGroupThree") == null) {
sendEvent(name: "associationGroupThree", value: "")
}
}
void executeConfigureCmds() {
runIn(6, refreshSyncStatus)
List<String> cmds = []
int changes = pendingChanges
if (changes) {
log.warn "Syncing ${changes} Change(s)"
}
configParams.each {
Integer storedVal = getParamStoredValue(it.num)
Integer settingVal = safeToInt((settings ? settings["configParam${it.num}"] : null), null)
if ((settingVal != null) && (settingVal != storedVal)) {
logDebug "CHANGING ${it.name}(#${it.num}) from ${storedVal} to ${settingVal}"
cmds << configSetCmd(it, settingVal)
cmds << configGetCmd(it)
}
}
cmds += getConfigureAssocsCmds()
sendCommands(cmds)
}
List<String> getConfigureAssocsCmds(boolean countOnly=false) {
List<String> cmds = []
boolean failedS2 = (device?.getDataValue("networkSecurityLevel") == "ZWAVE_S2_FAILED")
associationGroups.each { group, name ->
boolean changes = false
def stateNodeIds = state["${name}NodeIds"]
def settingNodeIds = getAssocDNIsSettingNodeIds(group)
def newNodeIds = settingNodeIds?.findAll { !(it in stateNodeIds) }
if (newNodeIds) {
if (!countOnly) {
logDebug "Adding Nodes ${newNodeIds} to Association Group ${group}"
}
cmds << associationSetCmd(group, newNodeIds)
changes = true
}
def oldNodeIds = stateNodeIds?.findAll { !(it in settingNodeIds) }
if (oldNodeIds) {
if (!countOnly) {
logDebug "Removing Nodes ${oldNodeIds} from Association Group ${group}"
}
cmds << associationRemoveCmd(group, oldNodeIds)
changes = true
}
if (!countOnly && !failedS2 && (changes || state.refreshAll)) {
cmds << associationGetCmd(group)
}
}
if (!countOnly && failedS2 && cmds) {
// The handler doesn't get association reports for 700 series devices when not joined with S2 so requesting manufacturer report as a way to confirm the device is responding and if it responds then it assumes the association changes were successful.
cmds << manufacturerSpecificGetCmd()
}
return cmds
}
List<Integer> getAssocDNIsSettingNodeIds(int group) {
String assocSetting = settings["group${group}AssocDNIs"] ?: ""
List<Integer> nodeIds = convertHexListToIntList(assocSetting?.split(","))
if (assocSetting && !nodeIds) {
log.warn "'${assocSetting}' is not a valid value for the 'Device Network Ids for Association Group ${group}' setting. All z-wave devices have a 2 character Device Network Id and if you're entering more than 1, use commas to separate them."
}
else if (nodeIds?.size() > 5) {
log.warn "The 'Device Network Ids for Association Group ${group}' setting contains more than 5 Ids so only the first 5 will be associated."
}
return nodeIds
}
def ping() {
logDebug "ping()..."
return sendCommands([ versionGetCmd() ])
}
def on() {
logDebug "on()..."
return [ switchBinarySetCmd(0xFF) ]
}
def off() {
logDebug "off()..."
return [ switchBinarySetCmd(0x00) ]
}
def configure() {
logDebug "configure()..."
if (!isDuplicateCommand(state.lastConfig, 5000)) {
state.lastConfig = new Date().time
createChildRelays()
runIn(4, refresh)
return delayBetween([
configGetCmd(relay1TypeParam),
configGetCmd(relay2TypeParam),
basicGetCmd(3),
sensorBinaryGetCmd(3),
basicGetCmd(4),
sensorBinaryGetCmd(4)
], 500)
}
}
def refresh() {
logDebug "refresh()..."
refreshSyncStatus()
createChildRelays()
def cmds = [
versionGetCmd()
]
(0..4).each {
cmds << switchBinaryGetCmd(it)
}
if (!isDuplicateCommand(state.lastConfig, 7500)) {
configParams.each {
cmds << configGetCmd(it)
}
}
sendCommands(cmds)
}
void sendCommands(List<String> cmds, Integer delay=500) {
if (cmds) {
def actions = []
cmds.each {
actions << new physicalgraph.device.HubAction(it)
}
sendHubCommand(actions, delay)
}
}
String versionGetCmd() {
return secureCmd(zwave.versionV1.versionGet())
}
String manufacturerSpecificGetCmd() {
return secureCmd(zwave.manufacturerSpecificV2.manufacturerSpecificGet())
}
String associationSetCmd(int group, nodes) {
return secureCmd(zwave.associationV2.associationSet(groupingIdentifier: group, nodeId: nodes))
}
String associationGetCmd(int group) {
return secureCmd(zwave.associationV2.associationGet(groupingIdentifier: group))
}
String associationRemoveCmd(int group, nodes) {
return secureCmd(zwave.associationV2.associationRemove(groupingIdentifier: group, nodeId: nodes))
}
String sensorBinaryGetCmd(int endpoint) {
return multiChannelCmdEncapCmd(zwave.sensorBinaryV1.sensorBinaryGet(), endpoint)
}
String basicGetCmd(Integer endpoint=null) {
return multiChannelCmdEncapCmd(zwave.basicV1.basicGet(), endpoint)
}
String switchBinaryGetCmd(Integer endpoint=null) {
return multiChannelCmdEncapCmd(zwave.switchBinaryV1.switchBinaryGet(), endpoint)
}
String switchBinarySetCmd(int value, Integer endpoint=null) {
return multiChannelCmdEncapCmd(zwave.switchBinaryV1.switchBinarySet(switchValue: value), endpoint)
}
String configSetCmd(Map param, int value) {
return secureCmd(zwave.configurationV2.configurationSet(parameterNumber: param.num, size: param.size, scaledConfigurationValue: value))
}
String configGetCmd(Map param) {
return secureCmd(zwave.configurationV2.configurationGet(parameterNumber: param.num))
}
String multiChannelCmdEncapCmd(cmd, Integer endpoint) {
if (endpoint) {
return secureCmd(zwave.multiChannelV3.multiChannelCmdEncap(destinationEndPoint:safeToInt(endpoint)).encapsulate(cmd))
}
else {
return secureCmd(cmd)
}
}
String secureCmd(cmd) {
try {
if (zwaveInfo?.zw?.contains("s") || ("0x98" in device?.rawDescription?.split(" "))) {
return zwave.securityV1.securityMessageEncapsulation().encapsulate(cmd).format()
}
else {
return cmd.format()
}
}
catch (ex) {
return cmd.format()
}
}
def parse(String description) {
def cmd = zwave.parse(description, commandClassVersions)
if (cmd) {
zwaveEvent(cmd)
}
else {
log.warn "Unable to parse: $description"
}
updateLastCheckIn()
return []
}
void updateLastCheckIn() {
if (!isDuplicateCommand(state.lastCheckInTime, 60000)) {
state.lastCheckInTime = new Date().time
sendEvent(name: "lastCheckIn", value: convertToLocalTimeString(new Date()), displayed: false)
}
}
String convertToLocalTimeString(dt) {
try {
def timeZoneId = location?.timeZone?.ID
if (timeZoneId) {
return dt.format("MM/dd/yyyy hh:mm:ss a", TimeZone.getTimeZone(timeZoneId))
}
else {
return "$dt"
}
}
catch (ex) {
return "$dt"
}
}
void zwaveEvent(physicalgraph.zwave.commands.securityv1.SecurityMessageEncapsulation cmd) {
def encapsulatedCmd = cmd.encapsulatedCommand(commandClassVersions)
if (encapsulatedCmd) {
zwaveEvent(encapsulatedCmd)
}
else {
log.warn "Unable to extract encapsulated cmd from $cmd"
}
}
void zwaveEvent(physicalgraph.zwave.commands.multichannelv3.MultiChannelCmdEncap cmd) {
// Workaround that was added to all SmartThings Multichannel DTHs.
if (cmd.commandClass == 0x6C && cmd.parameter.size >= 4) { // Supervision encapsulated Message
// Supervision header is 4 bytes long, two bytes dropped here are the latter two bytes of the supervision header
cmd.parameter = cmd.parameter.drop(2)
// Updated Command Class/Command now with the remaining bytes
cmd.commandClass = cmd.parameter[0]
cmd.command = cmd.parameter[1]
cmd.parameter = cmd.parameter.drop(2)
}
def encapsulatedCommand = cmd.encapsulatedCommand(commandClassVersions)
if (encapsulatedCommand) {
zwaveEvent(encapsulatedCommand, cmd.sourceEndPoint)
}
else {
logDebug "Unable to get encapsulated command: $cmd"
}
}
void zwaveEvent(physicalgraph.zwave.commands.versionv1.VersionReport cmd) {
logTrace "$cmd"
sendEventIfNew("firmwareVersion", (cmd.applicationVersion + (cmd.applicationSubVersion / 100)))
}
void zwaveEvent(physicalgraph.zwave.commands.associationv2.AssociationReport cmd) {
logTrace "$cmd"
logDebug "Group ${cmd.groupingIdentifier} Association: ${cmd.nodeId}"
saveGroupAssociations(cmd.groupingIdentifier, cmd.nodeId)
}
void zwaveEvent(physicalgraph.zwave.commands.manufacturerspecificv2.ManufacturerSpecificReport cmd) {
logTrace "$cmd"
// The handler doesn't get association reports for 700 series devices when not joined with S2 so this report was requested to confirm the device is responding and saved based on the assumption that they were applied.
associationGroups.each { group, name ->
String assocSetting = settings["group${group}AssocDNIs"] ?: ""
saveGroupAssociations(group, convertHexListToIntList(assocSetting?.split(",")))
}
}
void saveGroupAssociations(groupId, nodeIds) {
logTrace "saveGroupAssociations(${groupId}, ${nodeIds})"
runIn(3, refreshSyncStatus)
String name = associationGroups.get(safeToInt(groupId))
if (name) {
state["${name}NodeIds"] = nodeIds
def dnis = convertIntListToHexList(nodeIds)?.join(", ") ?: ""
if (dnis) {
dnis = "[${dnis}]" // wrapping it with brackets prevents ST from attempting to convert the value into a date.
}
sendEventIfNew(name, dnis, false)
}
}
void zwaveEvent(physicalgraph.zwave.commands.configurationv2.ConfigurationReport cmd) {
logTrace "${cmd}"
runIn(4, refreshSyncStatus)
Map param = configParams.find { it.num == cmd.parameterNumber }
if (param) {
logDebug "${param.name}(#${param.num}) = ${cmd.scaledConfigurationValue}"
setParamStoredValue(param.num, cmd.scaledConfigurationValue)
if (param in relayTypeParams) {
createChildInput(param)
}
}
else {
logDebug "Unknown Parameter #${cmd.parameterNumber} = ${cmd.scaledConfigurationValue}"
}
}
void refreshSyncStatus() {
int changes = pendingChanges
sendEventIfNew("syncStatus", (changes ? "${changes} Pending Changes" : "Synced"), false)
}
int getPendingChanges() {
int configChanges = safeToInt(configParams.count { ((getSettingValue(it.num) != null) && (getSettingValue(it.num) != getParamStoredValue(it.num))) })
int pendingAssocs = (getConfigureAssocsCmds(true)?.size() ? 1 : 0)
return (configChanges + pendingAssocs)
}
Integer getSettingValue(int paramNum) {
return safeToInt(settings && settings["configVal${paramNum}"], null)
}
Integer getParamStoredValue(int paramNum) {
return safeToInt(state["configVal${paramNum}"], null)
}
void setParamStoredValue(int paramNum, int value) {
state["configVal${paramNum}"] = value
}
void zwaveEvent(physicalgraph.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd, endpoint=0) {
logTrace "${cmd} (Endpoint ${endpoint})"
String value = (cmd.value == 0xFF) ? "on" : "off"
if (endpoint) {
def child = findChildByDNI(getRelayDNI(endpoint == 1 ? 1 : 2))
if (child) {
String desc = "${child.displayName}: switch is ${value}"
if (child.currentValue("switch") != value) {
logDebug "${desc}"
child.sendEvent(name: "switch", value: value, descriptionText: desc)
}
else {
logTrace "${desc}"
}
}
}
else {
sendEventIfNew("switch", value)
}
}
void zwaveEvent(physicalgraph.zwave.commands.notificationv3.NotificationReport cmd, endpoint=0) {
logTrace "${cmd} (Endpoint ${endpoint})"
// handleChildInputEvent((cmd.event && (cmd.event != 23)), endpoint)
}
void zwaveEvent(physicalgraph.zwave.commands.sensorbinaryv2.SensorBinaryReport cmd, endpoint=0) {
logTrace "${cmd} (Endpoint ${endpoint})"
if (safeToInt(state.highestSensorEndpoint) < endpoint) {
state.highestSensorEndpoint = endpoint
}
int relay = 1
if ((endpoint > 2) || ((endpoint == 2) && (state.highestSensorEndpoint == 2))) {
relay = 2
}
handleChildInputEvent(cmd.sensorValue, relay)
}
void zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd, endpoint=0) {
logTrace "${cmd} (Endpoint ${endpoint})"
handleChildInputEvent(cmd.value, (endpoint == 2 ? 1 : 2))
}
void handleChildInputEvent(rawValue, relay) {
def child = findChildInput(relay)
if (child) {
Map inputType = childInputTypes.find { child.deviceNetworkId == getInputDNI(it, relay) }
if (inputType) {
def value = (rawValue ? inputType.active : inputType.inactive)
logDebug "${child.displayName} ${inputType.attribute} ${value}"
child.sendEvent(name: inputType.attribute, value: value)
}
}
}
void zwaveEvent(physicalgraph.zwave.Command cmd, endpoint=0) {
logTrace "Unhandled zwaveEvent: ${cmd} (Endpoint ${endpoint})"
}
List<Map> getConfigParams() {
return [
powerFailureRecoveryParam,
ledIndicatorModeParam,
relay1TypeParam,
relay1InputTriggerParam,
relay2TypeParam,
relay2InputTriggerParam,
dcMotorModeParam,
relay1AutoOffParam,
relay1AutoOffUnitParam,
relay1AutoOnParam,
relay1AutoOnUnitParam,
relay2AutoOffParam,
relay2AutoOffUnitParam,
relay2AutoOnParam,
relay2AutoOnUnitParam
]
}
Map getPowerFailureRecoveryParam() {
return [num: 1, name: "On/Off Status Recovery After Power Failure", size: 1, options: [0:"Turn All Relays Off", 1:"Restore Relay States From Before Power Failure [DEFAULT]", 2:"Turn All Relays On", 3:"Restore Relay 1 and Turn On Relay 2", 4:"Restore Relay 2 and Turn On Relay 1"]]
}
List<Map> getRelayTypeParams() {
return [
relay1TypeParam,
relay2TypeParam
]
}
Map getRelay1TypeParam() {
return getRelayTypeParam(2, 1)
}
Map getRelay2TypeParam() {
return getRelayTypeParam(3, 2)
}
Map getRelayTypeParam(num, relay) {
Map options = [0:"Momentary Switch", 1:"Toggle Switch", 2:"Toggle Switch (any change) [DEFAULT]", 3:"Garage Door", 4:"Water Sensor", 6:"Motion Sensor", 7:"Contact Sensor", 9:"Smoke Detector", 10:"On/Off Report"]
// These options aren't included because ST doesn't have virtual devices for them.
//[5:"Heat Alarm", 8:"CO Detector"]
return [num: num, name: "Input Type for Relay ${relay}", size: 1, options: options, relay: relay]
}
Map getLedIndicatorModeParam() {
return [num: 5, name: "LED Indicator Control", size: 1, options: [0:"On when ALL Relays are Off [DEFAULT]", 1:"On when ANY Relay is On", 2:"Always Off", 3:"Always On"]]
}
Map getRelay1AutoOffParam() {
return getAutoOnOffParam(6, "Off", 1)
}
Map getRelay1AutoOnParam() {
return getAutoOnOffParam(7, "On", 1)
}
Map getRelay2AutoOffParam() {
return getAutoOnOffParam(8, "Off", 2)
}
Map getRelay2AutoOnParam() {
return getAutoOnOffParam(9, "On", 2)
}
Map getAutoOnOffParam(num, onOff, relay) {
return [num: num, name: "Auto Turn-${onOff} Timer for Relay ${relay} (0=Disabled, 1-65535)", size: 4, range: "0..65535"]
}
Map getRelay1InputTriggerParam() {
return getInputTriggerParam(10, 1)
}
Map getRelay2InputTriggerParam() {
return getInputTriggerParam(11, 2)
}
Map getInputTriggerParam(num, relay) {
return [num: num, name: "Input Trigger for Relay ${relay}", size: 1, options: [0:"Disabled", 1:"Enabled [DEFAULT]"]]
}
Map getRelay1AutoOffUnitParam() {
return getAutoOnOffUnitParam(15, "Off", 1)
}
Map getRelay1AutoOnUnitParam() {
return getAutoOnOffUnitParam(16, "On", 1)
}
Map getRelay2AutoOffUnitParam() {
return getAutoOnOffUnitParam(17, "Off", 2)
}
Map getRelay2AutoOnUnitParam() {
return getAutoOnOffUnitParam(18, "On", 2)
}
Map getAutoOnOffUnitParam(num, onOff, relay) {
return [num: num, name: "Auto Turn-${onOff} Timer Unit for Relay ${relay}", size: 1, options: [0:"Minutes [DEFAULT]", 1:"Seconds", 2:"Hours"]]
}
Map getDcMotorModeParam() {
return [num: 24, name: "DC Motor Mode", size: 1, options: [0:"Disabled [DEFAULT]", 1:"Enabled"]]
}
void sendEventIfNew(String name, value, boolean displayed=true) {
String desc = "${device.displayName}: ${name} is ${value}"
if (device.currentValue(name) != value) {
if (name != "syncStatus") {
logDebug(desc)
}
sendEvent(name: name, value: value, descriptionText: desc, displayed: displayed)
}
else {
logTrace(desc)
}
}
def childOn(dni) {
logDebug "childOn(${dni})..."
sendChildOnOffCmds(0xFF, dni)
}
def childOff(dni) {
logDebug "childOff(${dni})..."
sendChildOnOffCmds(0x00, dni)
}
void sendChildOnOffCmds(int value, String dni) {
sendCommands([
switchBinarySetCmd(value, (dni.endsWith(getRelaySuffix(1)) ? 1 : 2))
])
}
void createChildRelays() {
logTrace "createChildRelays()..."
(1..2).each { relay ->
String dni = getRelayDNI(relay)
def child = findChildByDNI(dni)
if (!child) {
String name = "Relay ${relay}"
logDebug "Creating Child Switch for ${name}"
addChildDevice("smartthings", "Child Switch", dni, device.hubId,
[
completedSetup: true,
label: "${device.displayName}-${name}",
isComponent: false,
data: [relay: relay]
]
)
sendCommands([ switchBinaryGetCmd(relay) ])
}
}
}
void createChildInput(Map param) {
logTrace "createChildInput(Input ${param.relay})..."
Map inputType = childInputTypes.find { it.configVal == getParamStoredValue(param.num) }
if (inputType) {
String dni = getInputDNI(inputType, param.relay)
if (!findChildByDNI(dni)) {
removeChildInput(param.relay)
String name = "Input ${param.relay}"
logDebug "Creating Child ${inputType.attribute} for ${name}"
def child = addChildDevice(inputType.namespace, inputType.name, dni, device.hubId,
[
completedSetup: true,
label: "${device.displayName}-${name} ${inputType.attribute.capitalize()}",
isComponent: false,
data: [ relayInput: param.relay ]
]
)
if (child) {
child.sendEvent(name: inputType.attribute, value: inputType.inactive)
}
}
}
else {
removeChildInput(param.relay)
}
}
void removeChildInput(int relay) {
def child = findChildInput(relay)
if (child) {
try {
log.warn "Removing ${child.displayName} "
deleteChildDevice(child.deviceNetworkId)
}
catch (e) {
log.warn "Unable to remove ${child?.displayName}"
}
}
}
String getRelayDNI(int relay) {
return "${device.deviceNetworkId}${getRelaySuffix(relay)}"
}
String getRelaySuffix(int relay) {
return ":R" + relay.toString()
}
def findChildInput(int relay) {
return childDevices?.find { it.deviceNetworkId.contains(getInputSuffix(relay)) }
}
String getInputDNI(Map inputType, int relay) {
return "${device.deviceNetworkId}${getInputSuffix(relay)}-${inputType.attribute}"
}
String getInputSuffix(int relay) {
return ":I" + relay.toString()
}
private findChildByDNI(String dni) {
return childDevices?.find { it.deviceNetworkId == dni }
}
List<String> convertIntListToHexList(List<Integer> intList) {
List<String> hexList = []
intList?.each {
hexList.add(Integer.toHexString(it).padLeft(2, "0").toUpperCase())
}
return hexList
}
List<Integer> convertHexListToIntList(String[] hexList) {
List<Integer> intList = []
hexList?.each {
try {
it = it.trim()
intList.add(Integer.parseInt(it, 16))
}
catch (e) { }
}
return intList
}
Integer safeToInt(val, Integer defaultVal=0) {
if ("${val}"?.isInteger()) {
return "${val}".toInteger()
}
else if ("${val}".isDouble()) {
return "${val}".toDouble()?.round()
}
else {
return defaultVal
}
}
boolean isDuplicateCommand(lastExecuted, allowedMil) {
!lastExecuted ? false : (lastExecuted + allowedMil > new Date().time)
}
void logDebug(String msg) {
if (state.debugLoggingEnabled != false) {
log.debug "$msg"
}
}
void logTrace(String msg) {
// log.trace "$msg"
}