/
aeon-labs-multifunction-siren.groovy
1207 lines (1056 loc) · 32.4 KB
/
aeon-labs-multifunction-siren.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
/**
* Aeon Labs Multifunction Siren v 1.10.3
* (Aeon Labs Siren - Model:ZW080-A17)
*
* (https://community.smartthings.com/t/release-aeon-labs-multifunction-siren/40652?u=krlaframboise)
*
* Capabilities:
* Switch, Alarm, Tone, Audio Notification,
* Music Player, Polling
*
* Author:
* Kevin LaFramboise (krlaframboise)
*
* Changelog:
*
* 1.10.3 (07/23/2017)
* - Added legacy fingerprint support for security cc check.
*
* 1.10.1 (07/22/2017)
* - Fixed issue caused by the hub firmware update 000.018.00018
* - If you're on hub v1 or you installed the device prior to May 2016, make sure you test the device after updating to this version.
*
* 1.9.4 (05/23/2017)
* - SmartThings fixed parse issue so changed it back to using response instead of sendhubaction.
*
* 1.9.3 (04/23/2017)
* - SmartThings broke parse method response handling so switched to sendhubaction.
* - Bug fix for location timezone issue.
*
* 1.9.2 (03/21/2017)
* - Fix for SmartThings TTS url changing.
*
* 1.9.1 (03/10/2017)
* - Improved health check
* - Removed polling capability.
*
* 1.9.0 (02/19/2017)
* - Added health check and self polling.
*
* 1.8.5 (09/22/2016)
* - Bug fix for TTS commands.
*
* 1.8.4 (09/10/2016)
* - Added setting for starting beep schedule when beep
* is executed.
*
* 1.8.3 (08/24/2016)
* - Bug Fixes
*
* 1.8.2 (08/06/2016)
* - Added Audio Notification capability.
* - Removed commands that have been removed from
* from the music player capability.
* - Added declaration for playSoundAndTrack.
*
* 1.7.1 (08/02/2016)
* - Fixed UI issue on iOS
*
* 1.7 (07/22/2016)
* - Fixed fingerprint for non-secure hub v2
* - Fixed secure command check so that it accurately
* detects if its paired securely and works either way.
* - Code cleanup and additional validation.
*
* 1.6 (05/21/2016)
* - Removed poll check.
* - Moved lastPoll event creation into parse method
* so that it gets raised every time the device responds.
* - Modified the poll command so that it only polls if the
* lastPoll event is old.
*
* 1.5 (05/12/2016)
* - Added Polling & Speech Synthesis functionality.
* - Adding attribute for lastPoll
*
* 1.4.1 (05/10/2016)
* - Bug fix for Smart Alarm music player device commands.
*
* 1.4 (03/05/2016)
* - Enhanced Logging
* - Fixed bug with beep schedule cancellation.
*
* 1.3 (03/03/2016)
* - Added startBeepDelayedAlarm command.
* - Fixed validation logging bug.
*
* 1.2 (02/29/2016)
* - Fixed IOS UI issue with beep buttons.
* - Added Music Player capability.
* - Added TTS command support so that it can be used
* with SHM, Notify with Sound, and Rule Machine
* - Added alarm delay feature.
*
* 1.1 (02/28/2016)
* - Logging Enhancements.
*
* 1.0 (02/28/2016)
* - Initial Release
*
* 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.
*/
metadata {
definition (name: "Aeon Labs Multifunction Siren", namespace: "krlaframboise", author: "Kevin LaFramboise") {
capability "Actuator"
capability "Switch"
capability "Alarm"
capability "Tone"
capability "Configuration"
capability "Music Player"
capability "Audio Notification"
capability "Speech Synthesis"
capability "Health Check"
attribute "status", "enum", ["off", "alarm", "customAlarm", "delayedAlarm", "beepDelayedAlarm", "beep", "beepSchedule", "customBeep", "customBeepSchedule"]
attribute "lastCheckin", "string"
command "playSoundAndTrack"
command "playTrackAtVolume"
command "customAlarm", ["number", "number", "number"]
command "delayedAlarm", ["number", "number", "number", "number"]
command "customBeep", ["number", "number", "number", "number", "number"]
command "startBeep"
command "startBeepDelayedAlarm"
command "startCustomBeep", ["number", "number", "number", "number", "number", "number", "number"]
command "customBeep1"
command "customBeep2"
command "customBeep3"
command "customBeep4"
command "customBeep5"
command "customBeep6"
fingerprint mfr: "0086", prod: "0104", model: "0050", deviceJoinName: "Aeon Labs Siren"
}
simulator {
reply "9881002001FF,9881002002": "command: 9881, payload: 002003FF"
reply "988100200100,9881002002": "command: 9881, payload: 00200300"
}
preferences {
input "sirenSound", "number",
title: "Siren Sound (1-5)",
defaultValue: 1,
range: "1..5",
displayDuringSetup: true,
required: false
input "sirenVolume", "number",
title: "Siren Volume (1-3)",
defaultValue: 1,
range: "1..3",
displayDuringSetup: true,
required: false
input "alarmDuration", "number",
title: "Turn siren off after: (seconds)\n(0=unlimited)",
defaultValue: 0,
displayDuringSetup: true,
required: false
input "beepSound", "number",
title: "Beep Sound (1-5)",
defaultValue: 3,
range: "1..5",
displayDuringSetup: true,
required: false
input "beepVolume", "number",
title: "Beep Volume (1-3)",
defaultValue: 1,
range: "1..3",
displayDuringSetup: true,
required: false
input "beepRepeat", "number",
title: "Beep Repeat (1-100)",
defaultValue: 1,
range: "1..100",
displayDuringSetup: true,
required: false
input "beepRepeatDelay", "number",
title: "Time Between Beeps in Milliseconds",
defaultValue: 1000,
displayDuringSetup: true,
required: false
input "beepLength", "number",
title: "Length of Beep in Milliseconds",
defaultValue: 100,
displayDuringSetup: true,
required: false
input "beepEvery", "number",
title: "Scheduled Beep Every (seconds)",
defaultValue: 10,
displayDuringSetup: true,
required: false
input "beepStopAfter", "number",
title: "Stop Scheduled Beep After (seconds)",
defaultValue: 60,
displayDuringSetup: true,
required: false
input "useBeepScheduleForBeep", "bool",
title: "Play Beep Schedule for Beep Command?",
defaultValue: false,
displayDuringSetup: true,
required: false
input "useBeepDelayedAlarm", "bool",
title: "Play Beep Schedule Before Sounding Alarm?",
defaultValue: false,
displayDuringSetup: true,
required: false
input "checkinInterval", "enum",
title: "Checkin Interval:",
defaultValue: checkinIntervalSetting,
required: false,
displayDuringSetup: true,
options: checkinIntervalOptions.collect { it.name }
input "logging", "enum",
title: "Types of messages to log:",
multiple: true,
required: false,
defaultValue: ["debug", "info"],
options: ["debug", "info", "trace"]
}
tiles(scale: 2) {
multiAttributeTile(name:"status", type: "generic", width: 6, height: 3, canChangeIcon: false){
tileAttribute ("status", key: "PRIMARY_CONTROL") {
attributeState "off", label:'off', action: "off", icon:"st.alarm.alarm.alarm", backgroundColor:"#ffffff"
attributeState "alarm", label:'Alarm Sounding!', action: "off", icon:"st.alarm.alarm.alarm", backgroundColor:"#ff9999"
attributeState "customAlarm", label:'Custom Alarm Sounding!', action: "off", icon:"st.alarm.alarm.alarm", backgroundColor:"#ff9999"
attributeState "delayedAlarm", label:'Delayed Alarm Active!', action: "off", icon:"st.alarm.alarm.alarm", backgroundColor:"#ff9999"
attributeState "beepDelayedAlarm", label:'Beep Delayed Alarm Active!', action: "off", icon:"st.alarm.alarm.alarm", backgroundColor:"#ff9999"
attributeState "beep", label:'Beeping!', action: "off", icon:"st.Entertainment.entertainment2", backgroundColor:"#99FF99"
attributeState "beepSchedule", label:'Scheduled\nBeeping!', action: "off", icon:"st.Entertainment.entertainment2", backgroundColor:"#99FF99"
attributeState "customBeep", label:'Custom Beeping!', action: "off", icon:"st.Entertainment.entertainment2", backgroundColor:"#CC99CC"
attributeState "customBeepSchedule", label:'Scheduled Custom Beeping!', action: "off", icon:"st.Entertainment.entertainment2", backgroundColor:"#CC99CC"
}
}
standardTile("playAlarm", "device.alarm", width: 2, height: 2) {
state "default", label:'Alarm', action: "both", icon:"st.alarm.alarm.alarm", backgroundColor: "#ff9999"
state "both", label:'Stop', action: "off", icon:"st.alarm.alarm.alarm", backgroundColor: "#ffffff"
}
standardTile("playBeep", "device.status", width: 2, height: 2) {
state "default", label:'Beep', action:"beep", icon:"st.Entertainment.entertainment2", backgroundColor: "#99FF99"
}
standardTile("playBeepSchedule", "device.status", width: 2, height: 2) {
state "default", label:'Start', action:"startBeep", nextState:"beepSchedule", icon: "st.Entertainment.entertainment2",backgroundColor: "#99FF99"
state "beepSchedule", label:'Stop', action:"off", icon: "st.Entertainment.entertainment2", backgroundColor: "#ffffff"
}
standardTile("playCustomBeep1", "device.status", width: 2, height: 2) {
state "default", label:'Beep 1', action:"customBeep1", icon:"st.Entertainment.entertainment2",backgroundColor: "#CC99CC"
}
standardTile("playCustomBeep2", "device.status", width: 2, height: 2) {
state "default", label:'Beep 2', action:"customBeep2", icon:"st.Entertainment.entertainment2",backgroundColor: "#CC99CC"
}
standardTile("playCustomBeep3", "device.status", width: 2, height: 2) {
state "default", label:'Beep 3', action:"customBeep3", icon:"st.Entertainment.entertainment2",backgroundColor: "#CC99CC"
}
standardTile("playCustomBeep4", "device.status", width: 2, height: 2) {
state "default", label:'Beep 4', action:"customBeep4", icon:"st.Entertainment.entertainment2",backgroundColor: "#CC99CC"
}
standardTile("playCustomBeep5", "device.status", width: 2, height: 2) {
state "default", label:'Beep 5', action:"customBeep5", icon:"st.Entertainment.entertainment2",backgroundColor: "#CC99CC"
}
standardTile("playCustomBeep6", "device.status", width: 2, height: 2) {
state "default", label:'Beep 6', action:"customBeep6", icon:"st.Entertainment.entertainment2",backgroundColor: "#CC99CC"
}
main "status"
details(["status", "playAlarm", "playBeep", "playBeepSchedule", "playCustomBeep1", "playCustomBeep2", "playCustomBeep3", "playCustomBeep4", "playCustomBeep5", "playCustomBeep6"])
}
}
def speak(text) {
logTrace "speak($text)"
playText(text)
}
// Unsuported Music Player commands
def nextTrack() { handleUnsupportedCommand("nextTrack") }
def setLevel(number) { handleUnsupportedCommand("setLevel") }
def previousTrack() { handleUnsupportedCommand("previousTrack") }
def unmute() { handleUnsupportedCommand("unmute") }
private handleUnsupportedCommand(cmd) {
log.info "Command $cmd is not supported"
return []
}
// Turns siren off
def pause() {
off()
}
def stop() {
off()
}
def mute() {
off()
}
// Turns siren on
def play() {
both()
}
// Audio Notification Commands
def playTrackAtVolume(URI, volume) {
logTrace "playTrackAtVolume($URI, $volume)"
playTrack(URI, volume)
}
def playSoundAndTrack(URI, duration=null, track, volume=null) {
logTrace "playSoundAndTrack($URI, $duration, $track, $volume)"
playTrack(URI, volume)
}
def playTrackAndRestore(URI, volume=null, ignore=null) {
if (ignore) {
// Fix for Speaker Notify w/ Sound not using command as documented.
volume = validateVolume(ignore)
}
logTrace "playTrackAndRestore($URI, $volume)"
playTrack(URI, volume)
}
def playTrackAndResume(URI, volume=null, ignore=null) {
if (ignore) {
// Fix for Speaker Notify w/ Sound not using command as documented.
volume = validateVolume(ignore)
}
logTrace "playTrackAndResume($URI, $volume)"
playTrack(URI, volume)
}
def playTrack(URI, volume=null) {
logTrace "playTrack($URI, $volume)"
playText(getTextFromTTSUrl(URI), volume)
}
def getTextFromTTSUrl(ttsUrl) {
logTrace "getTextFromTTSUrl($ttsUrl)"
if (ttsUrl?.toString()?.contains("/")) {
def startIndex = ttsUrl.lastIndexOf("/") + 1
ttsUrl = ttsUrl.substring(startIndex, ttsUrl.size())?.toLowerCase()?.replace(".mp3","")
}
return ttsUrl
}
def playTextAndResume(message, volume=null) {
logTrace "playTextAndResume($message, $volume)"
playText(message, volume)
}
def playTextAndRestore(message, volume=null) {
logTrace "playTextAndRestore($message, $volume)"
playText(message, volume=null)
}
def playText(message, volume=null) {
logTrace "playText($message, $volume)"
message = cleanMessage(message)
def cmds = []
switch (message) {
case ["1", "2", "3", "4", "5"]:
cmds = playAlarm(message, volume, settings.alarmDuration)
break
case ["siren", "strobe", "both", "on", "play"]:
cmds = both()
break
case ["stop", "off", "pause", "mute"]:
cmds = off()
break
case "beep":
cmds = beep()
break
case "startbeep":
cmds = startBeep()
break
case "startbeepdelayedalarm":
cmds = startBeepDelayedAlarm()
break
case "custombeep1":
cmds = customBeep1(volume)
break
case "custombeep2":
cmds = customBeep2(volume)
break
case "custombeep3":
cmds = customBeep3(volume)
break
case "custombeep4":
cmds = customBeep4(volume)
break
case "custombeep5":
cmds = customBeep5(volume)
break
case "custombeep6":
cmds = customBeep6(volume)
break
default:
if (message) {
cmds = parseComplexCommand(message)
}
}
if (!cmds) {
logDebug "'$message' is not a valid command."
}
return cmds
}
private cleanMessage(message) {
return "$message".
toLowerCase().
replace(",", "_").
replace(" ", "").
replace("(", "").
replace(")", "")
}
private parseComplexCommand(text) {
logTrace "parseComplexCommand($text)"
def cmds = []
def args = getComplexCmdArgs(text)
logTrace "complex command args: $args"
switch (args?.size()) {
case 3:
cmds += customAlarm(
args[0],
args[1],
args[2])
break
case 4:
cmds += delayedAlarm(
args[0],
args[1],
args[2],
args[3])
break
case 5:
cmds += customBeep(
args[0],
args[1],
args[2],
args[3],
args[4])
break
case 7:
cmds += startCustomBeep(
args[0],
args[1],
args[2],
args[3],
args[4],
args[5],
args[6])
break
}
return cmds
}
private getComplexCmdArgs(text) {
for (prefix in ["customalarm", "delayedalarm","startcustombeep","custombeep"]) {
text = removeCmdPrefix(text, prefix)
}
def args = text.tokenize("_")
if (args.every { node -> isInt(node) }) {
return args
}
else {
return null
}
}
private removeCmdPrefix(text, prefix) {
if (text.startsWith(prefix)) {
return text.replace("$prefix_", "").replace(prefix, "")
}
else {
return text
}
}
// Turns on siren and strobe
def on() {
both()
}
// Turns off siren and strobe
def off() {
logDebug "Executing off() command"
changeStatus("off")
turnOff()
}
private turnOff() {
logTrace "turnOff()"
delayBetween([
switchOffSetCmd(),
switchGetCmd()
], 100)
}
// Turns on siren and strobe
def strobe() {
both()
}
// Turns on siren and strobe
def siren() {
both()
}
// Turns on siren and strobe
def both() {
logDebug "Executing both() command"
if (settings.useBeepDelayedAlarm) {
return startBeepDelayedAlarm()
}
else {
changeStatus("alarm")
return playDefaultAlarm()
}
}
// Repeatedly plays the default beep based on the beepEvery and beepStopAfter settings and then turns on alarm.
def startBeepDelayedAlarm() {
logTrace "startBeepDelayedAlarm()"
changeStatus("beepDelayedAlarm")
return startDefaultBeepSchedule()
}
private playPendingAlarm() {
logTrace "playPendingAlarm()"
state.alarmPending = false
if (state.scheduledAlarm) {
def sound = state.scheduledAlarm?.sound
def volume = state.scheduledAlarm?.volume
def duration = state.scheduledAlarm?.duration
state.scheduledAlarm = null
return customAlarm(sound, volume, duration)
}
else {
return playDefaultAlarm()
}
}
private playDefaultAlarm() {
logTrace "playDefaultAlarm()"
return playAlarm(settings.sirenSound, settings.sirenVolume, settings.alarmDuration)
}
// Plays sound at volume for duration.
def customAlarm(sound, volume, duration) {
logTrace "customAlarm($sound, $volume, $duration)"
changeStatus("customAlarm")
return playAlarm(sound, volume, duration)
}
private playAlarm(sound, volume, duration) {
logTrace "playAlarm($sound, $volume, $duration)"
def durationMsg = (duration && durantion instanceof Integer && (int)duration > 0) ? ", duration: $duration" : ""
logDebug "Sounding Alarm: [sound: $sound, volume: $volume$durationMsg]"
sound = validateSound(sound)
volume = validateVolume(volume)
duration = validateRange(duration, 0, 0, Integer.MAX_VALUE, "Alarm Duration")
if (currentStatus() in ["alarm", "delayedAlarm", "beepDelayedAlarm"]) {
sendEvent(name:"alarm", value: "both", isStateChange: true)
sendEvent(name:"switch", value: "on", isStateChange: true, displayed: false)
}
def cmds = []
cmds << sirenSoundVolumeSetCmd(sound, volume)
if (duration > 0) {
cmds << "delay ${duration * 1000}"
cmds += turnOff()
}
return cmds
}
def delayedAlarm(sound, volume, duration, delay) {
logTrace "delayedAlarm($sound, $volume, $duration, $delay)"
changeStatus("delayedAlarm")
return startDelayedAlarm(sound, volume, duration, delay)
}
private startDelayedAlarm(sound, volume, duration, delay) {
logTrace "startDelayedAlarm($sound, $volume, $duration, $delay)"
state.scheduledAlarm = [
"sound": sound,
"volume": volume,
"duration": duration
]
delay = validateRange(delay, 3, 1, Integer.MAX_VALUE, "delay")
logDebug "Starting ${currentStatus()} [sound: $sound, volume: $volume, duration: $duration, delay: $delay]"
def result = []
result << "delay ${delay * 1000}"
result << basicGetCmd()
return result
}
// Plays the default beep.
def beep() {
logTrace "beep()"
if (!settings.useBeepScheduleForBeep) {
changeStatus("beep")
return playDefaultBeep()
}
else {
return startBeep()
}
}
private playDefaultBeep() {
logTrace "playDefaultBeep()"
return playBeep(
settings.beepSound,
settings.beepVolume,
settings.beepRepeat,
settings.beepRepeatDelay,
settings.beepLength
)
}
// Plays short beep.
def customBeep1(volume=null) {
return customBeep(3, volume, 1, 0, 50)
}
// Plays medium beep
def customBeep2(volume=null) {
return customBeep(3, volume, 1, 0, 100)
}
// Plays long beep
def customBeep3(volume=null) {
return customBeep(3, volume, 1, 0, 250)
}
// Plays 3 short beeps
def customBeep4(volume=null) {
return customBeep(3, volume, 3, 0, 50)
}
// Plays 3 medium beeps
def customBeep5(volume=null) {
return customBeep(3, volume, 3, 100 , 100)
}
// Plays 3 long beeps
def customBeep6(volume=null) {
return customBeep(3, volume, 3, 150, 200)
}
// Repeatedly plays the default beep based on the beepEvery and beepStopAfter settings.
def startBeep() {
logTrace "startBeep()"
changeStatus("beepSchedule")
return startDefaultBeepSchedule()
}
private startDefaultBeepSchedule() {
logTrace "startBeepSchedule()"
return startBeepSchedule(
settings.beepEvery,
settings.beepStopAfter,
settings.beepSound,
settings.beepVolume,
settings.beepRepeat,
settings.beepRepeatDelay,
settings.beepLength
)
}
// Repeatedly plays specified beep at specified in specified intervals.
def startCustomBeep(beepEverySeconds, stopAfterSeconds, sound, volume, repeat=1, repeatDelayMS=1000, beepLengthMS=100) {
logTrace "startCustomBeep($beepEverySeconds, $stopAfterSeconds, $sound, $volume, $repeat, $repeatDelayMS, $beepLengthMS)"
changeStatus("customBeepSchedule")
return startBeepSchedule(beepEverySeconds, stopAfterSeconds, sound, volume, repeat, repeatDelayMS, beepLengthMS)
}
private startBeepSchedule(beepEverySeconds, stopAfterSeconds, sound, volume, repeat, repeatDelayMS, beepLengthMS) {
logTrace "startBeepSchedule($beepEverySeconds, $stopAfterSeconds, $sound, $volume, $repeat, $repeatDelayMS, $beepLengthMS)"
logDebug "Starting ${currentStatus()} [beepEverySeconds: $beepEverySeconds, stopAfterSeconds: $stopAfterSeconds]"
state.beepSchedule = [
"startTime": (new Date().time),
"beepEvery": validateBeepEvery(beepEverySeconds),
"stopAfter": validateBeepStopAfter(stopAfterSeconds),
"sound": sound,
"volume": volume,
"repeat": repeat,
"repeatDelay": repeatDelayMS,
"beepLength": beepLengthMS
]
return playScheduledBeep()
}
private playScheduledBeep() {
logTrace "playScheduledBeep()"
def beepSchedule = state.beepSchedule
def cmds = []
if (beepScheduleStillActive(beepSchedule?.startTime, beepSchedule?.stopAfter)) {
cmds += playBeep(
beepSchedule.sound,
beepSchedule.volume,
beepSchedule.repeat,
beepSchedule.repeatDelay,
beepSchedule.beepLength
)
}
if (nextScheduledBeepStillActive()) {
if (beepSchedule.beepEvery > 0) {
cmds << "delay ${beepSchedule.beepEvery * 1000}"
}
cmds << basicGetCmd()
}
else {
state.beepSchedule = null
state.beepScheduleRunning = false
if (state.alarmPending) {
cmds += playPendingAlarm()
}
else {
cmds += turnOff()
}
}
return cmds
}
private nextScheduledBeepStillActive() {
def sched = state.beepSchedule
if (sched?.beepEvery != null) {
def adjustedStartTime = (sched.startTime - (sched.beepEvery * 1000))
return beepScheduleStillActive(adjustedStartTime, sched.stopAfter)
}
else {
return false
}
}
private beepScheduleStillActive(startTime, stopAfter) {
if (startTime && stopAfter) {
def endTimeMS = startTime + (stopAfter * 1000)
return (new Date().time < endTimeMS) && state.beepScheduleRunning
}
else {
return false
}
}
// Plays specified beep.
def customBeep(sound, volume, repeat=1, repeatDelayMS=1000, beepLengthMS=100) {
if (!volume) {
volume = settings.beepVolume
}
logTrace "customBeep($sound, $volume, $repeat, $repeatDelayMS, $beepLengthMS)"
changeStatus("customBeep")
return playBeep(sound, volume, repeat, repeatDelayMS, beepLengthMS)
}
private playBeep(sound, volume, repeat, repeatDelayMS, beepLengthMS) {
logTrace "playBeep($sound, $volume, $repeat, $repeatDelayMS, $beepLengthMS)"
int maxMS = 18000
sound = validateSound(sound, 3)
volume = validateVolume(volume)
beepLengthMS = validateRange(beepLengthMS, 100, 0, maxMS, "Beep Length")
repeatDelayMS = validateRepeatDelay(repeatDelayMS, beepLengthMS, maxMS)
repeat = validateRepeat(repeat, beepLengthMS, repeatDelayMS, maxMS)
def cmds = []
for (int repeatIndex = 1; repeatIndex <= repeat; repeatIndex++) {
cmds << sirenSoundVolumeSetCmd(sound, volume)
if (beepLengthMS > 0) {
cmds << "delay $beepLengthMS"
}
cmds << switchOffSetCmd()
if (repeat > 1 && repeatDelayMS > 0) {
cmds << "delay $repeatDelayMS"
}
}
if (!state.beepScheduleRunning && !state.alarmPending && currentStatus() != "off") {
cmds << turnOff()
}
return cmds
}
private changeStatus(newStatus) {
def oldStatus = currentStatus()
finalizeOldStatus(oldStatus, newStatus)
if (newStatus in ["delayedAlarm", "beepDelayedAlarm"]) {
state.alarmPending = true
}
if (newStatus in ["beepDelayedAlarm", "beepSchedule", "customBeepSchedule"]) {
state.beepScheduleRunning = true
}
def displayStatus = (
oldStatus != newStatus &&
newStatus != "alarm" &&
!(oldStatus in ["alarm", "delayedAlarm", "beepDelayedAlarm"]))
sendEvent(name:"status", value: newStatus, isStateChange: true, displayed: displayStatus)
}
private finalizeOldStatus(oldStatus, newStatus) {
if (state.alarmPending &&
oldStatus in ["delayedAlarm", "beepDelayedAlarm"] &&
!(newStatus in ["alarm", "customAlarm"])) {
logDebug "Delayed Alarm Cancelled"
}
else if (state.beepScheduleRunning) {
if (nextScheduledBeepStillActive()) {
logDebug "Beep Schedule Cancelled"
}
else {
logDebug "Beep Schedule Completed"
}
}
state.alarmPending = false
state.beepScheduleRunning = false
state.scheduledAlarm = null
state.beepSchedule = null
}
// Stores preferences and displays device settings.
def updated() {
logTrace "updated()"
if (!isDuplicateCommand(state.lastUpdated, 3000)) {
state.lastUpdated = new Date().time
initializeCheckin()
def cmds = []
cmds += configure()
return response(delayBetween(cmds, 200))
}
}
private isDuplicateCommand(lastExecuted, allowedMil) {
!lastExecuted ? false : (lastExecuted + allowedMil > new Date().time)
}
private initializeCheckin() {
// Set the Health Check interval so that it pings the device if it's 1 minute past the scheduled checkin.
def checkInterval = ((checkinIntervalSettingMinutes * 2 * 60) + (2 * 60))
sendEvent(name: "checkInterval", value: checkInterval, displayed: false, data: [protocol: "zwave", hubHardwareId: device.hub.hardwareID])
startHealthPollSchedule()
}
private startHealthPollSchedule() {
unschedule(healthPoll)
switch (checkinIntervalSettingMinutes) {
case 5:
runEvery5Minutes(healthPoll)
break
case 10:
runEvery10Minutes(healthPoll)
break
case 15:
runEvery15Minutes(healthPoll)
break
case 30:
runEvery30Minutes(healthPoll)
break
case [60, 120]:
runEvery1Hour(healthPoll)
break
default:
runEvery3Hours(healthPoll)
}
}
def healthPoll() {
logTrace "healthPoll()"
sendHubCommand(new physicalgraph.device.HubAction(versionGetCmd()))
return []
}
def ping() {
logTrace "ping()"
def result = []
if (!isDuplicateCommand(state.lastCheckinTime, 60000)) {
logDebug "Attempting to ping device."
// Restart the polling schedule in case that's the reason why it's gone too long without checking in.
startHealthPollSchedule()
result << versionGetCmd()
}
return result
}
def configure() {
logTrace "configure()"
def cmds = [
sendNotificationsSetCmd(),
switchGetCmd()
]
return cmds
}
def parse(String description) {
def result = []
def cmd = zwave.parse(description, commandClassVersions)
if (cmd) {
result += zwaveEvent(cmd)
}
else {
logDebug "Unable to parse: $description"
}
if (!isDuplicateCommand(state.lastCheckinTime, 60000)) {
state.lastCheckinTime = new Date().time
result << createLastCheckinEvent()
}
return result
}
private createLastCheckinEvent() {
logDebug "Device Checked In"
return createEvent(name: "lastCheckin", value: convertToLocalTimeString(new Date()), displayed: false)
}
private convertToLocalTimeString(dt) {
def timeZoneId = location?.timeZone?.ID
if (timeZoneId) {
return dt.format("MM/dd/yyyy hh:mm:ss a", TimeZone.getTimeZone(timeZoneId))
}
else {
return "$dt"
}
}
// Unencapsulates the secure command.
def zwaveEvent(physicalgraph.zwave.commands.securityv1.SecurityMessageEncapsulation cmd) {
def result = []
if (cmd) {
def encapCmd = cmd.encapsulatedCommand(commandClassVersions)
if (encapCmd) {
result += zwaveEvent(encapCmd)
}
else {
log.debug "Unable to encapsulate: $cmd"
}
}
return result
}
def zwaveEvent(physicalgraph.zwave.commands.versionv1.VersionReport cmd) {
logTrace "VersionReport: $cmd"
// Using this event for health monitoring to update lastCheckin
return []
}
// Handles device reporting off and alarm turning on.
def zwaveEvent(physicalgraph.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd) {
def result = []
if (cmd.value == 0) {
changeStatus("off")
def alarmDisplayed = (device.currentValue("alarm") == "both")
if (alarmDisplayed) {
logDebug "Alarm is off"
}
result << createEvent(name:"alarm", value: "off", isStateChange: true, displayed: alarmDisplayed)