forked from ekimmagrann/hubitat-elkm1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathElk-M1-Driver.groovy
3154 lines (2976 loc) · 118 KB
/
Elk-M1-Driver.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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************************************************
*
* A Hubitat Driver using Telnet on the local network to connect to the Elk M1 via the M1XEP or C1M1
*
* License:
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* Name: Elk M1 Driver
* A Special Thanks to Doug Beard for the framework of this driver!
*
* I am not a programmer so a lot of this work is through trial and error. I also spent a good amount of time looking
* at other integrations on various platforms. I know someone else was working on an Elk driver that involved an ESP
* setup. This is a more direct route using equipment I already owned.
*
*** See Release Notes at the bottom***
***********************************************************************************************************************/
public static String version() { return "v0.2.6" }
import groovy.transform.Field
metadata {
definition(name: "Elk M1 Driver", namespace: "belk", author: "Mike Magrann") {
capability "Actuator"
capability "ContactSensor"
capability "Initialize"
capability "PushableButton"
capability "SecurityKeypad"
capability "Telnet"
//capability "TemperatureMeasurement"
capability "Lock"
command "armHomeInstant"
command "armNight"
command "armNightInstant"
command "armVacation"
command "chime"
command "push", [[name: "button*", description: "1 - 6", type: "NUMBER"]]
command "refreshArmStatus"
command "refreshCounterValues"
command "refreshCustomValues"
// command "refreshKeypadArea"
command "refreshLightingStatus"
command "refreshOutputStatus"
command "refreshTemperatureStatus"
command "refreshTroubleStatus"
command "refreshZoneStatus"
command "requestZoneDefinitions"
command "requestZoneVoltage", [[name: "zone*", description: "1 - 208", type: "NUMBER"]]
command "sendMsg"
command "showTextOnKeypads", [[name: "line1", description: "Max 16 characters", type: "STRING"],
[name: "line2", description: "Max 16 characters", type: "STRING"],
[name: "timeout*", description: "0 - 65535", type: "NUMBER"],
[name: "beep", type: "ENUM", constraints: ["no", "yes"]]]
command "speakPhrase", [[name: "phraseNumber*", description: "1 - 319", type: "NUMBER"]]
command "speakWord", [[name: "wordNumber*", description: "1 - 473", type: "NUMBER"]]
command "zoneBypass", [[name: "zone*", description: "1 - 208, 0 = Unbypass all", type: "NUMBER"]]
command "zoneTrigger", [[name: "zone*", description: "1 - 208", type: "NUMBER"]]
attribute "alarm", "enum", [Clear, Detected]
attribute "alarmState", "string"
attribute "armingIn", "number"
attribute "armState", "enum", [NotReadytoArm, ReadytoArm, ReadytoArmBut, ArmedwithExit, ArmedFully, ForceArmed, ArmedwithaBypass]
attribute "armStatus", "enum", [Disarmed, ArmedAway, ArmedHome, ArmedHomeInstant, ArmedNight, ArmedNightInstant, ArmedVacation,
ArmingAway, ArmingHome, ArmingHomeInstant, ArmingNight, ArmingNightInstant, ArmingVacation]
attribute "beep", "enum", [Off, Beeped, Beeping]
attribute "button1", "string"
attribute "button2", "string"
attribute "button3", "string"
attribute "button4", "string"
attribute "button5", "string"
attribute "button6", "string"
attribute "chime", "enum", [Off, Chimed]
attribute "chimeMode", "enum", [Off, Tone, Voice, ToneVoice]
attribute "f1LED", "enum", [Off, On, Blinking]
attribute "f2LED", "enum", [Off, On, Blinking]
attribute "f3LED", "enum", [Off, On, Blinking]
attribute "f4LED", "enum", [Off, On, Blinking]
attribute "f5LED", "enum", [Off, On, Blinking]
attribute "f6LED", "enum", [Off, On, Blinking]
attribute "invalidUser", "string"
attribute "lastUser", "string"
attribute "trouble", "enum", [Clear, Detected]
}
preferences {
input name: "ip", type: "text", title: "IP Address", required: true
input name: "port", type: "number", title: "Port", range: 1..65535, required: true, defaultValue: 2101
input name: "keypad", type: "number", title: "Keypad", description: "0 = Do not connect", range: 0..16, required: true, defaultValue: 1
input name: "code", type: "text", title: "User code"
input name: "timeout", type: "number", title: "Timeout in minutes", range: 0..1999, defaultValue: 0
input name: "dbgEnable", type: "bool", title: "Enable debug logging", defaultValue: false
input name: "txtEnable", type: "enum", title: "Enable event logging for system +",
options: ["none", "all", "keypad", "area"], defaultValue: "all", required: true
input name: "lockArm", type: "enum", title: "Lock to arm mode",
options: ["none", "away", "home", "homeInstant", "night", "nightInstant", "vacation",
"nextAway", "nextHome", "forceAway", "forceHome"], defaultValue: "away", required: true
input name: "lockDisarm", type: "bool", title: "Allow lock to disarm", defaultValue: true
}
}
//general handlers
List<hubitat.device.HubAction> installed() {
log.warn "${device.label} installed..."
initialize()
}
List<hubitat.device.HubAction> updated() {
log.info "${device.label} Updated..."
if (dbgEnable)
log.debug "${device.label}: Configuring IP: ${ip}, Port ${port}, Keypad ${keypad}, Code: ${code != ""}, Timeout: ${timeout}"
sendEvent(name: "numberOfButtons", value: 6, type: "keypad", descriptionText: "${device.label} numberOfButtons default value set")
sendEvent(name: "button1", value: "F1", type: "keypad", descriptionText: "${device.label} button1 default value set")
sendEvent(name: "button2", value: "F2", type: "keypad", descriptionText: "${device.label} button2 default value set")
sendEvent(name: "button3", value: "F3", type: "keypad", descriptionText: "${device.label} button3 default value set")
sendEvent(name: "button4", value: "F4", type: "keypad", descriptionText: "${device.label} button4 default value set")
sendEvent(name: "button5", value: "F5", type: "keypad", descriptionText: "${device.label} button5 default value set")
sendEvent(name: "button6", value: "F6", type: "keypad", descriptionText: "${device.label} button6 default value set")
sendEvent(name: "maxCodes", value: 199, type: "system", descriptionText: "${device.label} maxCodes default value set")
initialize()
}
List<hubitat.device.HubAction> initialize() {
List<hubitat.device.HubAction> commandList = null
if (state.alarmState != null) state.remove("alarmState")
if (state.armState != null) state.remove("armState")
if (state.armStatus != null) state.remove("armStatus")
state.remove("creatingZone")
device.removeSetting("locationSet")
device.removeSetting("switchFully")
device.removeSetting("tempCelsius")
if (state.entryExit == null)
state.entryExit = new long[8]
if (port == null)
device.updateSetting("port", [type: "number", value: 2101])
if (keypad == null)
device.updateSetting("keypad", [type: "number", value: 1])
if (timeout == null)
device.updateSetting("timeout", [type: "number", value: 0])
if (dbgEnable == null)
device.updateSetting("dbgEnable", [type: "bool", value: "false"])
if (txtEnable == null)
device.updateSetting("txtEnable", [type: "text", value: "all"])
if (lockArm == null)
device.updateSetting("lockArm", [type: "text", value: "away"])
if (lockDisarm == null)
device.updateSetting("lockDisarm", [type: "bool", value: "true"])
telnetClose()
if (keypad == 0) {
log.warn "${device.label} is disconnected. Change your keypad in Preferences to reconnect."
} else {
boolean success = true
try {
//open telnet connection
telnetConnect([termChars: [13, 10]], ip, port.toInteger(), null, null)
//give it a chance to start
pauseExecution(1000)
if (dbgEnable)
log.debug "${device.label}: Telnet connection to Elk M1 established"
} catch (e) {
log.warn "${device.label}: initialize error: ${e.message}"
success = false
}
if (success)
commandList = refresh()
heartbeat() // Start checking for telnet timeout
return commandList
}
}
List<hubitat.device.HubAction> refresh() {
List<hubitat.device.HubAction> cmds = []
cmds.add(refreshKeypadArea())
cmds.add(refreshUserArea())
cmds.add(refreshVersionNumber())
runIn(2, refreshSmart)
runIn(12, refreshElk)
return delayBetween(cmds, 300)
}
void refreshSmart() {
parent.smartRefresh()
}
void refreshElk() {
List<String> cmds = []
cmds.add((String) refreshTemperatureStatus())
cmds.add((String) refreshArmStatus())
cmds.add((String) refreshOutputStatus())
refreshLightingStatus(false).each { cmds.add((String) it) }
cmds.add((String) refreshTroubleStatus())
cmds.add((String) refreshAlarmStatus())
cmds.add((String) requestKeypadStatus())
cmds.add((String) requestKeypadPress())
refreshCounterValues(false).each { cmds.add((String) it) }
sendHubCommand(new hubitat.device.HubMultiAction(delayBetween(cmds, 1000), hubitat.device.Protocol.TELNET))
pauseExecution(1000)
cmds = []
cmds.add((String) refreshCustomValues())
cmds.add((String) RequestTextDescriptions("12", keypad.toInteger()))
cmds.add((String) RequestTextDescriptions("13", keypad.toInteger()))
cmds.add((String) RequestTextDescriptions("14", keypad.toInteger()))
cmds.add((String) RequestTextDescriptions("15", keypad.toInteger()))
cmds.add((String) RequestTextDescriptions("16", keypad.toInteger()))
cmds.add((String) RequestTextDescriptions("17", keypad.toInteger()))
cmds.add((String) refreshZoneStatus())
sendHubCommand(new hubitat.device.HubMultiAction(delayBetween(cmds, 1000), hubitat.device.Protocol.TELNET))
}
void uninstalled() {
telnetClose()
getChildDevices().each { deleteChildDevice(it.deviceNetworkId) }
}
//Elk M1 Command Line Request - Start of
hubitat.device.HubAction unlock() {
if (lockDisarm)
disarm()
}
hubitat.device.HubAction lock(int sysArea = state.area, String sysCode = code) {
switch (lockArm) {
case "away":
armAway(sysArea, sysCode)
break
case "home":
armHome(sysArea, sysCode)
break
case "homeInstant":
armHomeInstant(sysArea, sysCode)
break
case "night":
armNight(sysArea, sysCode)
break
case "nightInstant":
armNightInstant(sysArea, sysCode)
break
case "vacation":
armVacation(sysArea, sysCode)
break
case "nextAway":
ArmNextAway(sysArea, sysCode)
break
case "nextHome":
ArmNextHome(sysArea, sysCode)
break
case "forceAway":
ArmForceAway(sysArea, sysCode)
break
case "forceHome":
ArmForceHome(sysArea, sysCode)
break
}
}
hubitat.device.HubAction disarm(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} disarm"
String cmd = elkCommands["Disarm"]
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction armAway(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} armAway"
String cmd = elkCommands["ArmAway"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction armHome(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} armHome"
String cmd = elkCommands["ArmHome"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction armHomeInstant(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} armHomeInstant"
String cmd = elkCommands["ArmHomeInstant"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction armNight(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} armNight"
String cmd = elkCommands["ArmNight"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction armNightInstant(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} armNightInstant"
String cmd = elkCommands["ArmNightInstant"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction armVacation(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} armVacation"
String cmd = elkCommands["ArmVacation"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction ArmNextAway(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} ArmNextAway"
String cmd = elkCommands["ArmNextAway"]
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction ArmNextHome(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} ArmNextHome"
String cmd = elkCommands["ArmNextHome"]
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction ArmForceAway(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} ArmForceAway"
String cmd = elkCommands["ArmForceAway"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction ArmForceHome(int sysArea = state.area, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} area ${sysArea} ArmForceHome"
String cmd = elkCommands["ArmForceHome"]
if (!isArmed(sysArea))
sendMsg(cmd, sysArea, sysCode)
}
hubitat.device.HubAction getCodes() {
if (dbgEnable)
log.debug "${device.label} getCodes started"
state.remove("userList")
state.requestedChange = "{}"
state.creatingDevice = "02"
RequestTextDescriptions(state.creatingDevice, 1)
}
hubitat.device.HubAction deleteCode(BigDecimal codeposition, String sysCode = code) {
setCode(codeposition, null, null, sysCode)
}
hubitat.device.HubAction setCode(BigDecimal codeposition, String pincode, String name = null, String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} code ${codeposition} RequestUserChange"
String reason = null
if (state.userType == "User")
reason = "User not allowed to set user codes."
else if (device.currentState("codeLength")?.value == null)
reason = "codeLength is not known. Please save preferences, wait 30 seconds and try again."
else if (pincode != null && (pincode.length() != device.currentState("codeLength").value.toInteger() || !pincode.isInteger() ||
pincode.toInteger() < 0 || pincode != pincode.trim()))
reason = "PIN code must be numeric and " + device.currentState("codeLength").value + " digits."
else if (pincode != null && pincode.padLeft(6, '0').take(6) == "000000")
reason = "PIN code must not be zero."
if (reason != null) {
log.warn device.label + " " + reason
sendEvent(name: "codeChanged", value: "failed", type: "system", descriptionText: device.label + " codeChanged for " + codeposition +
" (" + name + ") was failed - " + reason, isStateChange: true)
return null
}
String oldCode = sysCode.padLeft(6, '0').take(6)
String newCode = (pincode ?: "0").padLeft(6, '0').take(6)
if (state.requestedChange == null)
state.requestedChange = "{}"
String extra
Map<Map> pendingList = new groovy.json.JsonSlurper().parseText(state.requestedChange)
if (newCode == "000000") {
pendingList[codeposition.toString()] = [code: pincode, name: name, status: "D"]
extra = "10"
} else {
pendingList[codeposition.toString()] = [code: pincode, name: name, status: "A"]
extra = "00"
if (name != null && name.length() > 0)
log.warn device.label + " does not support user name updates. The name will not be updated on the panel."
}
state.requestedChange = new groovy.json.JsonBuilder(pendingList).toString()
oldCode = "0" + oldCode.substring(0, 1) + "0" + oldCode.substring(1, 2) + "0" + oldCode.substring(2, 3) + "0" +
oldCode.substring(3, 4) + "0" + oldCode.substring(4, 5) + "0" + oldCode.substring(5, 6)
newCode = "0" + newCode.substring(0, 1) + "0" + newCode.substring(1, 2) + "0" + newCode.substring(2, 3) + "0" +
newCode.substring(3, 4) + "0" + newCode.substring(4, 5) + "0" + newCode.substring(5, 6)
String cmd = elkCommands["RequestUserChange"] + String.format("%03d", codeposition.toInteger()) + oldCode + newCode + state.userAreas
sendMsg(cmd, extra)
}
hubitat.device.HubAction setCodeLength(BigDecimal pincodelength) {
log.warn "${device.label} does not support setCodeLength."
}
hubitat.device.HubAction setEntryDelay(BigDecimal entrancedelay) {
log.warn "${device.label} does not support setEntryDelay."
}
hubitat.device.HubAction setExitDelay(BigDecimal exitdelay) {
log.warn "${device.label} does not support setExitDelay."
}
hubitat.device.HubAction refreshUserArea(String sysCode = code) {
if (dbgEnable)
log.debug "${device.label} refreshUserArea"
String cmd = elkCommands["RequestUserArea"] + sysCode.padLeft(6, '0').take(6)
sendMsg(cmd)
}
hubitat.device.HubAction refreshVersionNumber() {
if (dbgEnable)
log.debug "${device.label} refreshVersionNumber"
String cmd = elkCommands["RequestVersionNumber"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshArmStatus() {
if (dbgEnable)
log.debug "${device.label} refreshArmStatus"
String cmd = elkCommands["RequestArmStatus"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshTroubleStatus() {
if (dbgEnable)
log.debug "${device.label} refreshTroubleStatus"
String cmd = elkCommands["RequestTroubleStatus"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshAlarmStatus() {
if (dbgEnable)
log.debug "${device.label} requestAlarmStatus"
String cmd = elkCommands["RequestAlarmStatus"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshTemperatureStatus() {
if (dbgEnable)
log.debug "${device.label} refreshTemperatureStatus"
String cmd = elkCommands["RequestTemperatureData"]
sendMsg(cmd)
}
hubitat.device.HubAction RequestTextDescriptions(String deviceType, int deviceNumber) {
if (dbgEnable)
log.debug "${device.label} RequestTextDescriptions Type: ${deviceType} Device: ${deviceNumber}"
String cmd = elkCommands["RequestTextDescriptions"] + deviceType + String.format("%03d", deviceNumber)
sendMsg(cmd)
}
void startCreatingDevice(String deviceType) {
state.creatingDevice = deviceType
sendHubCommand(RequestTextDescriptions(deviceType, 1))
}
void stopCreatingDevice() {
if (state.creatingDevice == "02" && state.userList != null) {
TreeMap userList = state.userList
TreeMap<Map> lockCodes = [:]
TreeMap<Map> oldCodes = [:]
Map oldUser
String key
String blankCode = "0000"
if (device.currentState("codeLength")?.value != null)
blankCode = blankCode.padRight(device.currentState("codeLength").value.toInteger(), '0')
if (device.currentState("lockCodes")?.value != null)
oldCodes = new groovy.json.JsonSlurper().parseText(decrypt(device.currentState("lockCodes").value))
userList.each { userNumber, userText ->
key = userNumber.toInteger().toString()
oldUser = oldCodes[key]
if ((userText != "" && userText != "USER " + userNumber) || (oldUser != null && !(oldUser.name in [null, ""])))
lockCodes[key] = oldUser == null ? [name: userText, code: blankCode] : oldUser
}
String value = encrypt(new groovy.json.JsonBuilder(lockCodes).toString())
log.info device.label + " getCodes complete. " + lockCodes.size().toString() + " codes found."
sendEvent(name: "lockCodes", value: value, type: "system", descriptionText: device.label + " lockCodes updated. " +
lockCodes.size().toString() + " codes found by getCodes.")
if (device.currentState("lockCodes")?.value != null && device.currentState("lockCodes").value != value)
log.info device.label + " lockCodes changed to " + value
state.remove("userList")
}
state.remove("creatingDevice")
}
hubitat.device.HubAction ControlOutputOn(BigDecimal output = 0, String time = "0") {
if (dbgEnable)
log.debug "${device.label} ControlOutputOn output ${output}"
String cmd = elkCommands["ControlOutputOn"]
cmd = cmd + String.format("%03d", output.intValue()) + time.padLeft(5, '0')
sendMsg(cmd)
}
hubitat.device.HubAction ControlOutputOff(BigDecimal output = 0) {
if (dbgEnable)
log.debug "${device.label} ControlOutputOff output ${output}"
String cmd = elkCommands["ControlOutputOff"]
cmd = cmd + String.format("%03d", output.intValue())
sendMsg(cmd)
}
hubitat.device.HubAction ControlOutputToggle(BigDecimal output = 0) {
if (dbgEnable)
log.debug "${device.label} ControlOutputToggle output ${output}"
String cmd = elkCommands["ControlOutputToggle"]
cmd = cmd + String.format("%03d", output.intValue())
sendMsg(cmd)
}
hubitat.device.HubAction TaskActivation(BigDecimal task = 0) {
if (dbgEnable)
log.debug "${device.label} TaskActivation task: ${task}"
String cmd = elkCommands["TaskActivation"]
cmd = cmd + String.format("%03d", task.intValue())
sendMsg(cmd)
}
hubitat.device.HubAction speakPhrase(BigDecimal phraseNumber = 0) {
if (phraseNumber > 0 && phraseNumber < 320) {
if (dbgEnable)
log.debug "${device.label} speakPhrase ${phraseNumber}"
String cmd = elkCommands["SpeakPhrase"] + String.format("%03d", phraseNumber.toInteger())
sendMsg(cmd)
}
}
hubitat.device.HubAction speakWord(BigDecimal wordNumber = 0) {
if (wordNumber > 0 && wordNumber < 474) {
if (dbgEnable)
log.debug "${device.label} speakWord ${wordNumber}"
String cmd = elkCommands["SpeakWord"] + String.format("%03d", wordNumber.toInteger())
sendMsg(cmd)
}
}
hubitat.device.HubAction setCounterValue(BigDecimal counter, BigDecimal value) {
if (counter >= 1 && counter <= 64 && value >= 0 && value <= 65535) {
if (dbgEnable)
log.debug "${device.label} setCounterValue counter: ${counter} value ${value}"
String cmd = elkCommands["SetCounterValue"] + String.format("%02d", counter.intValue()) + String.format("%05d", value.intValue())
sendMsg(cmd)
}
}
hubitat.device.HubAction setCustomTime(BigDecimal custom, BigDecimal hours, BigDecimal minutes) {
BigDecimal value = hours * 256 + minutes
setCustomValue(custom, value)
}
hubitat.device.HubAction setCustomValue(BigDecimal custom, BigDecimal value) {
if (custom >= 1 && custom <= 20 && value >= 0 && value <= 65535) {
if (dbgEnable)
log.debug "${device.label} setCustomValue custom: ${custom} value ${value}"
String cmd = elkCommands["SetCustomValue"] + String.format("%02d", custom.intValue()) + String.format("%05d", value.intValue())
sendMsg(cmd)
}
}
hubitat.device.HubAction requestZoneDefinitions() {
if (dbgEnable)
log.debug "${device.label} requestZoneDefinitions"
String cmd = elkCommands["RequestZoneDefinitions"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshZoneStatus() {
if (dbgEnable)
log.debug "${device.label} refreshZoneStatus"
String cmd = elkCommands["RequestZoneStatus"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshOutputStatus() {
if (dbgEnable)
log.debug "${device.label} refreshOutputStatus"
String cmd = elkCommands["RequestOutputStatus"]
sendMsg(cmd)
}
List<hubitat.device.HubAction> refreshCounterValues(boolean delay = true) {
List<hubitat.device.HubAction> cmds = []
int i
for (i = 1; i <= 64; i += 1) {
if (getChildDevice(device.deviceNetworkId + "_X_" + String.format("%02d", i)) != null)
cmds.add(refreshCounterValue(i))
}
if (delay)
return delayBetween(cmds, 500)
else
return cmds
}
hubitat.device.HubAction refreshCounterValue(BigDecimal counter) {
if (dbgEnable)
log.debug "${device.label} refreshCounterValue(${counter})"
String cmd = elkCommands["RequestCounterValue"] + String.format("%02d", counter.toInteger())
sendMsg(cmd)
}
hubitat.device.HubAction refreshCustomValues() {
if (dbgEnable)
log.debug "${device.label} refreshCustomValues"
String cmd = elkCommands["RequestAllCustomValues"]
sendMsg(cmd)
}
hubitat.device.HubAction refreshCustomValue(BigDecimal custom) {
if (dbgEnable)
log.debug "${device.label} refreshCustomValue(${custom})"
String cmd = elkCommands["RequestCustomValue"] + String.format("%02d", custom.toInteger())
sendMsg(cmd)
}
hubitat.device.HubAction zoneBypass(BigDecimal zoneNumber = 0, int sysArea = state.area, String sysCode = code) {
if ((zoneNumber >= 0 && zoneNumber < 209) || zoneNumber == 999) {
String zoneNbr = String.format("%03d", zoneNumber.toInteger())
if (dbgEnable)
log.debug "${device.label} zone ${zoneNbr} zoneBypass"
String cmd = elkCommands["ZoneBypass"] + zoneNbr
sendMsg(cmd, sysArea, sysCode)
}
}
hubitat.device.HubAction zoneTrigger(BigDecimal zoneNumber = 0) {
if (zoneNumber > 0 && zoneNumber < 209) {
String zoneNbr = String.format("%03d", zoneNumber.toInteger())
if (dbgEnable)
log.debug "${device.label} zone ${zoneNbr} zoneTrigger"
String cmd = elkCommands["ZoneTrigger"] + zoneNbr
sendMsg(cmd)
}
}
hubitat.device.HubAction requestZoneVoltage(BigDecimal zoneNumber = 0) {
if (zoneNumber > 0 && zoneNumber < 209) {
String zoneNbr = String.format("%03d", zoneNumber.toInteger())
if (dbgEnable)
log.debug "${device.label} zone ${zoneNbr} zoneVoltage"
String cmd = elkCommands["ZoneVoltage"] + zoneNbr
sendMsg(cmd)
}
}
hubitat.device.HubAction controlLightingMode(String unitCode, String mode, String setting, String time) {
if (dbgEnable)
log.debug "${device.label} controlLightingMode ${unitCode} mode: ${mode} setting: ${setting} time: ${time}"
String cmd = elkCommands["ControlLightingMode"] +
unitCode + mode.padLeft(2, '0').take(2) + setting.padLeft(2, '0').take(2) + time.padLeft(4, '0').take(4)
sendMsg(cmd)
}
hubitat.device.HubAction controlLightingOn(String unitCode = "A03") {
if (dbgEnable)
log.debug "${device.label} controlLightingOn: ${unitCode}"
String cmd = elkCommands["ControlLightingOn"] + unitCode
sendMsg(cmd)
}
hubitat.device.HubAction controlLightingOff(String unitCode) {
if (dbgEnable)
log.debug "${device.label} controlLightingOff: ${unitCode}"
String cmd = elkCommands["ControlLightingOff"] + unitCode
sendMsg(cmd)
}
hubitat.device.HubAction controlLightingToggle(String unitCode) {
if (dbgEnable)
log.debug "${device.label} controlLightingToggle: ${unitCode}"
String cmd = elkCommands["ControlLightingToggle"] + unitCode
sendMsg(cmd)
}
hubitat.device.HubAction chime(int keypadNumber = keypad) {
push(7, keypadNumber)
}
hubitat.device.HubAction push(BigDecimal button, int keypadNumber = keypad) {
String buttonCode = button.toString()
if (button == 7)
buttonCode = "C"
if (button == 8)
buttonCode = "*"
if (button >= 1 && button <= 8) {
requestKeypadPress(buttonCode, keypadNumber)
}
}
List<hubitat.device.HubAction> refreshLightingStatus(boolean delay = true) {
if (dbgEnable)
log.debug "${device.label} refreshLightingStatus"
List<hubitat.device.HubAction> cmds = [refreshLightingStatus("0"), refreshLightingStatus("1"), refreshLightingStatus("2"),
refreshLightingStatus("3")]
if (delay)
return delayBetween(cmds, 500)
else
return cmds
}
hubitat.device.HubAction refreshLightingStatus(String unitCode) {
String bank = unitCode.take(1)
if (bank >= "A" && bank <= "D")
bank = "0"
else if (bank >= "E" && bank <= "H")
bank = "1"
else if (bank >= "I" && bank <= "L")
bank = "2"
else if (bank >= "M" && bank <= "P")
bank = "3"
if (dbgEnable)
log.debug "${device.label} refreshLightingStatus: ${bank}"
String cmd = elkCommands["RequestLightingStatus"] + bank
sendMsg(cmd)
}
hubitat.device.HubAction refreshKeypadArea() {
if (dbgEnable)
log.debug "${device.label} refreshKeypadArea"
String cmd = elkCommands["RequestKeypadArea"]
sendMsg(cmd)
}
hubitat.device.HubAction requestKeypadPress(String key = "0", int keypadNumber = keypad) {
if (dbgEnable)
log.debug "${device.label} requestKeypadPress"
String cmd = elkCommands["RequestKeypadPress"] + String.format("%02d", keypadNumber) + key
sendMsg(cmd)
}
hubitat.device.HubAction requestKeypadStatus(int keypadNumber = keypad) {
if (dbgEnable)
log.debug "${device.label} requestKeypadStatus"
String cmd = elkCommands["RequestKeypadStatus"] + String.format("%02d", keypadNumber)
sendMsg(cmd)
}
hubitat.device.HubAction showTextOnKeypads(BigDecimal time, String beep, int sysArea = state.area) {
return showTextOnKeypads("", "", time, beep, sysArea)
}
hubitat.device.HubAction showTextOnKeypads(String line1, BigDecimal time, String beep, int sysArea = state.area) {
return showTextOnKeypads(line1, "", time, beep, sysArea)
}
hubitat.device.HubAction showTextOnKeypads(String line1 = "", String line2 = "", BigDecimal time = 0, String beep = "no", int sysArea = state.area) {
if (dbgEnable)
log.debug "${device.label} showTextOnKeypads: line1 ${line1}, line2 ${line2}, time ${time}, beep ${beep}, area ${sysArea}"
if (line1.length() == 0) {
line1 = line2
line2 = ""
}
String clear = (time == 0 ? "1" : "2")
String hasBeep = (beep == "yes" ? "1" : "0")
int duration = (time < 1 ? 0 : time > 65535 ? 65535 : time.intValue())
if (line1.length() > 16)
line1 = line1.substring(0, 16)
else if (line1.length() == 0)
clear = "0"
else if (line1.length() < 16)
line1 = line1 + "^"
if (line2.length() > 16)
line2 = line2.substring(0, 16)
else if (line2.length() > 0 && line2.length() < 16)
line2 = line2 + "^"
String cmd = elkCommands["ShowTextOnKeypads"] + sysArea.toString().padLeft(1, '0') + clear + hasBeep + String.format("%05d", duration) +
line1.padRight(16, ' ') + line2.padRight(16, ' ')
sendMsg(cmd)
}
//Elk M1 Command Line Request - End of
//Elk M1 Message Send Lines - Start of
hubitat.device.HubAction sendMsg(String cmd, int sysArea = 0, String sysCode = null) {
return sendMsg(cmd, "00", sysArea, sysCode)
}
hubitat.device.HubAction sendMsg(String cmd, String extra, int sysArea = 0, String sysCode = null) {
String msg
if (sysArea < 0 || sysArea > 8 || sysCode == null)
msg = cmd + extra
else
msg = cmd + sysArea.toString() + sysCode.padLeft(6, '0').take(6) + extra
String msgStr = addChksum(Integer.toHexString(msg.length() + 2).toUpperCase().padLeft(2, '0') + msg)
if (dbgEnable)
log.debug "${device.label} sendMsg: $msgStr"
return new hubitat.device.HubAction(msgStr, hubitat.device.Protocol.TELNET)
}
hubitat.device.HubAction sendMsg(hubitat.device.HubAction action = null) {
return action
}
String addChksum(String msg) {
char[] msgArray = msg.toCharArray()
int msgSum = 0
msgArray.each { (msgSum += (int) it) }
String chkSumStr = Integer.toHexString(256 - (msgSum % 256)).toUpperCase().padLeft(2, '0')
if (chkSumStr.length() == 2)
return msg + chkSumStr
else
return msg + chkSumStr.substring(1)
}
//Elk M1 Message Send Lines - End of
//Elk M1 Event Receipt Lines
private List parse(String message) {
List statusList = null
if (dbgEnable)
log.debug "${device.label} Parsing Incoming message: " + message
switch (message.substring(2, 4)) {
case "ZC":
zoneChange(message)
break
case "XK":
heartbeat()
break
case "CC":
outputChange(message)
break
case "TC":
taskChange(message)
break
case "EE":
statusList = entryExitChange(message)
break
case "AS":
statusList = armStatusReport(message)
break
case "ZS":
zoneStatusReport(message)
break
case "CS":
outputStatus(message)
break
case "DS":
lightingDeviceStatus(message)
break
case "PC":
lightingDeviceChange(message)
break
case "LW":
statusList = temperatureData(message)
break
case "ST":
statusList = statusTemperature(message)
break
case "TR":
thermostatData(message)
break
case "IC":
statusList = userCodeEntered(message)
break
case "EM":
statusList = sendEmail(message)
break
case "AR":
alarmReporting(message)
break
case "PS":
lightingBankStatus(message)
break
case "LD":
logData(message)
break
case "SD":
statusList = stringDescription(message)
break
case "AM":
statusList = updateAlarmAreas(message)
break
case "AZ":
updateAlarmZones(message)
break
case "KA":
keypadAreaAssignments(message)
break
case "KC":
statusList = keypadKeyChangeUpdate(message)
break
case "KF":
statusList = keypadFunctionKeyUpdate(message)
break
case "VN":
versionNumberReport(message)
break
case "ZD":
zoneDefinitionReport(message)
break
case "IE":
statusList = refresh()
break
case "RP":
statusList = connectionStatus(message)
break
case "SS":
statusList = updateSystemTrouble(message)
break
case "AP":
receiveTextString(message)
break
case "CU":
statusList = responseUserChange(message)
break
case "CR":
updateCustom(message)
break
case "CV":
updateCounter(message)
break
case "CA":
audioData(message, message.substring(4, 6)) // Audio Zone number added
break;
case "CD":
audioData(message, message.substring(8, 10)) // Audio Zone number added
break
case "ZV":
zoneVoltage(message)
break
case "UA":
statusList = userAreaReport(message)
break
case "RR":
case "ZB":
break
default:
if (txtEnable) log.info "${device.label}: The ${message.substring(2, 4)} command is unknown"
break
}
if (statusList != null && statusList.size() > 0) {
//log.debug "Trying: ${statusList}"
List rtn = []
statusList.each {
if (it instanceof Map) {
if (device.currentState(it.name)?.value == null || device.currentState(it.name).value != it.value.toString() || it.isStateChange) {
if (it.descriptionText == null)
it.descriptionText = device.label + " " + it.name + " was " + it.value
else
it.descriptionText = device.label + " " + it.descriptionText
rtn << createEvent(it)
if ((txtEnable == "all" || it?.type == "system" || txtEnable == it?.type) && it.name != "armState" && it.name != "contact" &&
it.name != "lock" && it.name != "securityKeypad" && it.name != "trouble" && it.name != "temperature")
log.info it.descriptionText
}
} else {
rtn << it
}
}
statusList = rtn
}
return statusList
}
void zoneChange(String message) {
String zoneNumber = message.substring(4, 7)
String zoneStatusCode = message.substring(7, 8)
String zoneStatus = elkZoneStatuses[zoneStatusCode]
switch (zoneStatusCode) {
case "1":
case "2":
case "3":
zoneNormal(zoneNumber, zoneStatus)
break;
case "9":
case "A":
case "B":
zoneViolated(zoneNumber, zoneStatus)
break;
case "5":
case "6":
case "7":
case "C":
case "D":
case "E":
case "F":
zoneTrouble(zoneNumber, zoneStatus)
break
default:
if (dbgEnable) log.debug "${device.label} Unknown zone status: zone ${zoneStatus} - ${zoneStatus}"
break
}
}
void zoneVoltage(String message) {
String zoneNumber = message.substring(4, 7)
BigDecimal zoneVoltageNumber = new BigDecimal(message.substring(7, 10)) / 10
com.hubitat.app.DeviceWrapper zoneDevice = getChildDevice("${device.deviceNetworkId}_Z_${zoneNumber}")