-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
zipato-multisound-siren.groovy
1016 lines (898 loc) · 25.1 KB
/
zipato-multisound-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
/**
* Zipato Multisound Siren v1.6.1
* (Zipato Z-Wave Indoor Multi-Sound Siren -
* Model:PH-PSE02)
*
*https://community.smartthings.com/t/release-zipato-phileo-multisound-siren-ph-pse02-us/53748?u=krlaframboise
*
* Capabilities:
* Configuration, Alarm, Audio Notification,
* Switch, Tone, Tamper Alert, Refresh
*
* Author:
* Kevin LaFramboise (krlaframboise)
*
* Changelog:
*
* 1.6.1 (07/23/2017)
* - Added legacy fingerprint support for security cc check.
*
* 1.6 (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.5.6 (04/23/2017)
* - SmartThings broke parse method response handling so switched to sendhubaction.
* - Bug fix for location timezone issue.
*
* 1.5.5 (03/21/2017)
* - Fix for SmartThings TTS url changing.
*
* 1.5.4 (03/10/2017)
* - Improved health check
* - Removed polling capability.
*
* 1.5.3 (02/19/2017)
* - Minor bug fixes.
*
* 1.5.1 (02/18/2017)
* - Switched from basic to notification for playing sounds.
* - Added Health Check Capability with hourly self polling.
*
* 1.4.2 (08/06/2016)
* - Adjusted chirp timing resulting in 97% accuracy.
*
* 1.4 (08/05/2016)
* - Fixed UI issue with buttons sticking.
* - Added Beep Repeat and Beep Repeat Delay fields
* so that the Door and Beep sound can be played
* one or more times when beep button is pressed.
*
* 1.3.3 (08/01/2016)
* - Fix iOS value tile issue for enabled button
* and possible casting issue in the configreport method.
*
* 1.3 (07/28/2016)
* - Bug fixes
*
* 1.2 (07/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: "Zipato Multisound Siren", namespace: "krlaframboise", author: "Kevin LaFramboise") {
capability "Actuator"
capability "Configuration"
capability "Alarm"
capability "Audio Notification"
capability "Music Player"
capability "Speech Synthesis"
capability "Switch"
capability "Tone"
capability "Tamper Alert"
capability "Refresh"
capability "Health Check"
attribute "status", "enum", ["off", "on", "alarm", "beep"]
attribute "alarmState", "enum", ["enabled", "disabled"]
attribute "lastCheckin", "string"
command "playTrackAtVolume"
command "playSoundAndTrack"
command "enableAlarm"
command "disableAlarm"
fingerprint mfr: "013C", prod: "0004", model: "000A"
fingerprint deviceId: "0x1005", inClusters: "0x71,0x20,0x25,0x85,0x70,0x72,0x86,0x30,0x59,0x73,0x5A,0x98,0x7A"
}
simulator {
// reply messages
reply "9881002001FF,9881002002": "command: 9881, payload: 002003FF"
reply "988100200100,9881002002": "command: 9881, payload: 00200300"
reply "9881002001FF,delay 3000,988100200100,9881002002": "command: 9881, payload: 00200300"
}
preferences {
input "sirenSound", "enum",
title: "Siren Sound:",
defaultValue: "Emergency",
displayDuringSetup: true,
required: false,
options: getSoundNames()
input "strobeSound", "enum",
title: "Strobe Sound:",
defaultValue: "Emergency",
displayDuringSetup: true,
required: false,
options: getSoundNames()
input "bothSound", "enum",
title: "Both Sound:",
defaultValue: "Emergency",
displayDuringSetup: true,
required: false,
options: getSoundNames()
input "switchOnSound", "enum",
title: "Switch On Sound:",
defaultValue: "Emergency",
displayDuringSetup: true,
required: false,
options: getSoundNames()
input "alarmDuration", "enum",
title: "Alarm Duration:",
defaultValue: "3 Minutes",
displayDuringSetup: true,
required: false,
options: ["30 Seconds", "1 Minute", "2 Minutes", "3 Minutes", "5 Minutes", "10 Minutes", "15 Minutes", "30 Minutes", "45 Minutes", "1 Hour", "Unlimited"]
input "beepSound", "enum",
title: "Beep Sound:",
defaultValue: "Beep",
displayDuringSetup: true,
required: false,
options: ["Beep","Door"]
input "repeatBeepSound", "number",
title: "Repeat Beep Sound (0-20):",
defaultValue: "0",
range: "0..20",
displayDuringSetup: true,
required: false
input "repeatBeepDelay", "number",
title: "Beep Delay in Milliseconds:",
defaultValue: "2000",
displayDuringSetup: true,
required: false
input "checkinInterval", "enum",
title: "Checkin Interval:",
defaultValue: checkinIntervalSetting,
required: false,
displayDuringSetup: true,
options: checkinIntervalOptions.collect { it.name }
input "debugOutput", "bool",
title: "Enable debug logging?",
defaultValue: true,
displayDuringSetup: false,
required: false
}
tiles(scale: 2) {
multiAttributeTile(name:"status", type: "generic", width: 6, height: 3, canChangeIcon: true){
tileAttribute ("device.status", key: "PRIMARY_CONTROL") {
attributeState "off",
label:'Off',
action: "off",
icon:"st.security.alarm.clear",
backgroundColor:"#ffffff"
attributeState "on",
label:'On!',
action: "off",
icon:"st.alarm.alarm.alarm",
backgroundColor: "#99c2ff"
attributeState "alarm",
label:'Alarm!',
action: "off",
icon:"st.alarm.alarm.alarm",
backgroundColor:"#ff9999"
attributeState "beep",
label:'Beeping!',
action: "off",
icon:"st.Entertainment.entertainment2",
backgroundColor:"#99FF99"
}
}
standardTile("playSiren", "device.alarm", width: 2, height: 2) {
state "default",
label:'Siren',
action:"alarm.siren",
icon:"st.security.alarm.clear",
backgroundColor:"#ff9999"
state "siren",
label:'Turn Off',
action:"alarm.off",
icon:"st.alarm.alarm.alarm",
background: "#ffffff"
}
standardTile("playStrobe", "device.alarm", width: 2, height: 2){
state "default",
label:'Strobe',
action:"alarm.strobe",
icon:"st.security.alarm.clear",
backgroundColor:"#ff9999"
state "strobe",
label:'Turn Off',
action:"alarm.off",
icon:"st.alarm.alarm.alarm",
background: "#ffffff"
}
standardTile("playBoth", "device.alarm", width: 2, height: 2) {
state "default",
label:'Both',
action:"alarm.both",
icon:"st.security.alarm.clear",
backgroundColor:"#ff9999"
state "both",
label:'Turn Off',
action:"alarm.off",
icon:"st.alarm.alarm.alarm",
background: "#ffffff"
}
standardTile("playOn", "device.switch", width: 2, height: 2) {
state "default",
label:'Turn On',
action:"switch.on",
icon:"st.security.alarm.clear",
backgroundColor:"#99c2ff"
state "on",
label:'Turn Off',
action:"switch.off",
icon:"st.alarm.alarm.alarm",
background: "#ffffff"
}
standardTile("playBeep", "device.status", width: 2, height: 2) {
state "default",
label:'Beep',
action:"tone.beep",
icon:"st.Entertainment.entertainment2",
backgroundColor: "#99FF99"
state "beep",
label:'Stop',
action:"off",
icon:"st.Entertainment.entertainment2",
background: "#ffffff"
}
standardTile("refresh", "device.refresh", width: 2, height: 2) {
state "refresh", label:'Refresh', action: "refresh", icon:"st.secondary.refresh-icon"
}
standardTile("alarmState", "device.alarmState", width: 2, height: 2) {
state "enabled", label:'Disable', action: "disableAlarm", icon:"st.custom.sonos.unmuted", backgroundColor: "#FFFFFF"
state "disabled", label:'Enable', action: "enableAlarm", icon:"st.custom.sonos.muted", backgroundColor: "#cccccc"
}
main "status"
details(["status", "playSiren", "playStrobe", "playBoth", "playOn", "playBeep", "alarmState", "refresh"])
}
}
private getSoundNames() {
[
"Ambulance",
"Beep",
"Chirp",
"Door",
"Emergency",
"Fire",
"Police"
]
}
def updated() {
if (!isDuplicateCommand(state.lastUpdated, 3000)) {
state.lastUpdated = new Date().time
initializeCheckin()
def cmds = []
if (!state.isConfigured) {
cmds += configure()
}
else {
cmds << alarmDurationSetCmd()
cmds += refresh()
}
return sendResponse(cmds)
}
}
private sendResponse(cmds) {
def actions = []
cmds?.each { cmd ->
actions << new physicalgraph.device.HubAction(cmd)
}
sendHubCommand(actions)
return []
}
private isDuplicateCommand(lastExecuted, allowedMil) {
!lastExecuted ? false : (lastExecuted + allowedMil > new Date().time)
}
private initializeCheckin() {
// Set the Health Check interval so that it can miss 2 checkins plus 2 minutes.
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()))
}
def ping() {
logTrace "ping()"
// Don't allow it to ping the device more than once per minute.
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()
return versionGetCmd()
}
}
// Initializes variables and sends settings to device
def configure() {
def cmds = []
cmds += delayBetween([
alarmDurationSetCmd(),
alarmDurationGetCmd(),
disableAlarmSetCmd(false),
disableAlarmGetCmd(),
notificationTypeGetCmd(),
basicGetCmd()
], 500)
return cmds
}
def refresh() {
logTrace "Executing refresh()"
logDebug "\nStrobe Sound: ${settings.strobeSound}\nSiren Sound: ${settings.sirenSound}\nBoth Sound: ${settings.bothSound}\nOn Sound: ${settings.switchOnSound}\nBeep Sound: ${settings.beepSound}"
if (device.currentValue("tamper") != "clear") {
sendEvent(getTamperEventMap("clear"))
}
delayBetween([
alarmDurationGetCmd(),
disableAlarmGetCmd(),
notificationTypeGetCmd(),
basicGetCmd()
], 250)
}
// Unsuported Music Player commands
def unmute() { handleUnsupportedCommand("unmute") }
def nextTrack() { handleUnsupportedCommand("nextTrack") }
def setLevel(number) { handleUnsupportedCommand("setLevel") }
def previousTrack() { handleUnsupportedCommand("previousTrack") }
private handleUnsupportedCommand(cmd) {
log.info "Command $cmd is not supported"
}
// Turns siren off
def pause() { off() }
def stop() { off() }
def mute() { off() }
// Turns siren on
def play() { both() }
// Audio Notification Capability Commands
def playSoundAndTrack(URI, duration=null, track=null, volume=null) {
speak(URI)
}
def playText(message, volume=null) {
speak(message)
}
def playTextAndResume(message, volume=null, otherVolume=null) {
speak(message)
}
def playTextAndRestore(message, volume=null, otherVolume=null) {
speak(message)
}
def playTrack(URI, volume=null) {
speak(URI)
}
def playTrackAndResume(URI, volume=null, otherVolume=null) {
speak(URI)
}
def playTrackAndRestore(URI, volume=null, otherVolume=null) {
speak(URI)
}
// Documented as part of the Audio Notification capability
// but not actually part of it so it must be declared.
def playTrackAtVolume(URI, volume) {
speak(URI)
}
def speak(text) {
def status
def soundNumber = getSoundNumber(text)
switch (soundNumber) {
case 1..5:
status = "alarm"
break
case 6..7:
status = "beep"
break
default:
status = "off"
}
setPlayStatus(status, "off", "off")
logDebug "Executing speakText($text)"
playSound(soundNumber)
}
def enableAlarm() {
logTrace "Executing enableAlarm()"
[
disableAlarmSetCmd(false),
"delay 200",
disableAlarmGetCmd()
]
}
def disableAlarm() {
logTrace "Executing disableAlarm()"
[
disableAlarmSetCmd(true),
"delay 200",
disableAlarmGetCmd()
]
}
def on() {
logTrace "Executing on()"
setPlayStatus("on", "off", "on")
return playSound(getSoundNumber(settings.switchOnSound))
}
def off() {
logTrace "Executing off()"
setPlayStatus("off", "off", "off")
[
basicSetCmd(0x00),
basicGetCmd()
]
}
def strobe() {
playAlarm(settings.strobeSound, "strobe")
}
def siren() {
playAlarm(settings.sirenSound, "siren")
}
def both() {
playAlarm(settings.bothSound, "both")
}
private playAlarm(soundName, alarmType) {
logTrace "Executing ${alarmType}()"
setPlayStatus("alarm", alarmType, "off")
playSound(getSoundNumber(soundName))
}
def beep() {
logTrace "Executing beep()"
setPlayStatus("beep", "off", "off")
playSound(getSoundNumber(settings.beepSound))
}
private setPlayStatus(statusVal, alarmVal, switchVal) {
state.playStatus = [
status: statusVal,
alarm: alarmVal,
switch: switchVal
]
}
private getSoundNumber(soundName) {
soundName = (soundName == null) ? "" : soundName?.toString()?.toLowerCase()
if (soundName?.contains("/")) {
def startIndex = soundName.lastIndexOf("/") + 1
soundName = soundName.substring(startIndex, soundName.size())?.toLowerCase()?.replace(".mp3","")
}
soundName = soundName?.toString()?.toLowerCase()
switch (soundName) {
case ["stop", "off", "0"]:
return 0
break
case ["emergency", "1", "255"]: // 0x01, 0x07
return 1
break
case ["fire", "2"]: // 0x02, 0x0A
return 2
break
case ["ambulance", "3"]: // 0x03, 0x0A
return 3
break
case ["police", "4"]: // 0x01, 0x0A
return 4
break
case ["door", "5"]: // 0x16, 0x06
return 5
break
case ["beep", "6"]: // 0x05, 0x0A
return 6
break
case ["chirp", "7"]:
return 7
break
case ["siren", "strobe", "both"]:
return getSoundNumber(settings?."${soundName}Sound")
break
case "on":
return getSoundNumber(settings?.switchOnSound)
break
default:
return 1
}
}
private playSound(soundNumber) {
def result = []
soundNumber = validateRange(soundNumber, 1, 1, 7)
if (soundNumber == 7) {
logInfo "Chirping"
result << notificationReportCmd(1)
result << "delay 100"
result << basicSetCmd(0x00)
}
else if (state.playStatus?.status == "beep") {
def beepCount = safeToInt(settings.repeatBeepSound, 0)
def repeatDelay = safeToInt(settings.repeatBeepDelay, 2000)
logInfo "Playing sound #$soundNumber $beepCount time(s)"
for (int beep = 0; beep <= beepCount; beep++) {
result << notificationReportCmd(soundNumber)
result << "delay $repeatDelay"
}
}
else {
logInfo "Playing Sound #$soundNumber"
result << notificationReportCmd(soundNumber)
}
state.lastSound = soundNumber
result << basicGetCmd()
return result
}
def parse(String description) {
def result = []
def cmd = zwave.parse(description, commandClassVersions)
if (cmd) {
result += zwaveEvent(cmd)
}
else {
log.warn "Unable to parse: $description"
}
if (!isDuplicateCommand(state.lastCheckinTime, 60000)) {
result << createLastCheckinEvent()
}
return result
}
def zwaveEvent(physicalgraph.zwave.commands.securityv1.SecurityMessageEncapsulation cmd) {
def encapsulatedCmd = cmd.encapsulatedCommand(commandClassVersions)
def result = []
if (encapsulatedCmd) {
result += zwaveEvent(encapsulatedCmd)
}
else {
log.warn "Unable to extract encapsulated cmd from $cmd"
}
return result
}
private getCommandClassVersions() {
[
0x25: 1, // Switch Binary
0x30: 2, // Sensor Binary
0x59: 1, // AssociationGrpInfo
0x5A: 1, // DeviceResetLocally
0x5E: 2, // ZwaveplusInfo
0x70: 1, // Configuration
0x71: 3, // Notification v4
0x72: 2, // ManufacturerSpecific
0x73: 1, // Powerlevel
0x7A: 2, // Firmware Update Md
0x85: 2, // Association
0x86: 1, // Version (2)
0x98: 1 // Security
]
}
def zwaveEvent(physicalgraph.zwave.commands.configurationv1.ConfigurationReport cmd) {
def result = []
def parameterName
def configVal = cmd.configurationValue ? cmd.configurationValue[0] : null
def displayVal = "$configVal"
state.isConfigured = true
switch (cmd.parameterNumber) {
case 7:
parameterName = "Notification Type"
displayVal = (configVal == 0) ? "Notification Report" : "Sensor Binary Report"
break
case 29:
parameterName = "Alarm State"
displayVal = (configVal == 0) ? "enabled" : "disabled"
result << createEvent(name: "alarmState", value: displayVal, displayed: (device.currentValue("alarmState") != displayVal), descriptionText: "Alarm is $displayVal")
break
case 31:
parameterName = "Alarm Duration"
displayVal = (configVal == 0) ? "Unlimited" : "${configVal * 30} Seconds"
break
default:
parameterName = "Parameter #${cmd.parameterNumber}"
}
if (parameterName) {
logDebug "${parameterName}: ${displayVal}"
}
return result
}
def zwaveEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd) {
def result = []
def forceEvent = false
if (cmd.value == 0 && device.currentValue("status") == "off" && state.lastSound >= 5) {
// Door Sound or Beep Sound played and they don't
// turn on the device so force the on event.
state.lastSound = null
result += createStatusEvents(0xFF, false)
forceEvent = true
}
result += createStatusEvents(cmd.value, forceEvent)
return result
}
def zwaveEvent(physicalgraph.zwave.commands.sensorbinaryv2.SensorBinaryReport cmd) {
logTrace "SensorBinaryReport: $cmd"
// Ignoring these reports.
return []
}
def zwaveEvent(physicalgraph.zwave.commands.versionv1.VersionReport cmd) {
logTrace "VersionReport: $cmd"
// Using this event for health monitoring to update lastCheckin
return []
}
private createLastCheckinEvent() {
state.lastCheckinTime = new Date().time
logDebug "Device Checked In"
return createEvent(name: "lastCheckin", value: convertToLocalTimeString(new Date()), displayed: false)
}
def createStatusEvents(val, forceEvent) {
def newSwitch
def newAlarm
def newStatus = val
def currentPlayStatus = state.playStatus
def result = []
if (val == 0x00) {
logDebug "${device.displayName} turned off"
newStatus = (device.currentValue("status") != "off" || canForceOffEvent(forceEvent, currentPlayStatus?.status)) ? "off" : null
newAlarm = (device.currentValue("alarm") != "off" || canForceOffEvent(forceEvent, currentPlayStatus?.alarm)) ? "off" : null
newSwitch = (device.currentValue("switch") != "off" || canForceOffEvent(forceEvent, currentPlayStatus?.switch)) ? "off" : null
}
else {
logDebug "${device.displayName} turned on"
if (!currentPlayStatus) {
currentPlayStatus = [status: "alarm", alarm: "off",switch: "off"]
}
newStatus = currentPlayStatus.status
newAlarm = (device.currentValue("alarm") != currentPlayStatus.alarm) ? currentPlayStatus.alarm : null
newSwitch = (device.currentValue("switch") != currentPlayStatus.switch) ? currentPlayStatus.switch : null
}
if (newStatus) {
result << createEvent(name: "status", value: newStatus, displayed: (device.currentValue("status") != newStatus), isStateChange: true)
}
if (newAlarm) {
result << createEvent(name: "alarm", value: newAlarm, displayed: false, isStateChange: true)
}
if (newSwitch) {
result << createEvent(name: "switch", value: newSwitch, displayed: false, isStateChange: true)
}
return result
}
private canForceOffEvent(forceEvent, playStatusVal) {
return (forceEvent && playStatusVal != null && playStatusVal != "off")
}
def zwaveEvent(physicalgraph.zwave.commands.notificationv3.NotificationReport cmd) {
def result = []
logDebu "NotificationReport: $cmd"
if (cmd.notificationType == 7 && cmd.event == 3) {
result << createTamperEvent(cmd.notificationStatus)
}
return result
}
def createTamperEvent(val) {
def tamperState = (val == 0xFF) ? "detected" : "clear"
if (device.currentValue("tamper") != tamperState) {
logDebug "Tamper is $tamperState"
return createEvent(getTamperEventMap(tamperState))
}
}
def getTamperEventMap(val) {
def isNew = device.currentValue("tamper") != val
[
name: "tamper",
value: val,
displayed: isNew,
descriptionText: "Tamper is $val"
]
}
def zwaveEvent(physicalgraph.zwave.Command cmd) {
logDebug "Unhandled zwaveEvent: $cmd"
return []
}
private basicSetCmd(val) {
return secureCmd(zwave.basicV1.basicSet(value: val))
}
private notificationReportCmd(sound) {
return secureCmd(zwave.notificationV3.notificationReport(event: getSoundEvent(sound), notificationType: getSoundNotificationType(sound)))
}
private getSoundEvent(sound) {
def result
switch (sound) {
case 2:
result = 0x02
break
case 3:
result = 0x03
break
case 5:
result = 0x16
break
case 6:
result = 0x05
break
default:
result = 0x01
}
return result
}
private getSoundNotificationType(sound) {
def result
switch (sound) {
case 1:
result = 0x07
break
case 5:
result = 0x06
break
default:
result = 0x0A
}
return result
}
private basicGetCmd() {
return secureCmd(zwave.basicV1.basicGet())
}
private versionGetCmd() {
return secureCmd(zwave.versionV1.versionGet())
}
private alarmDurationSetCmd() {
//(0 - 127)
//0: disabled
//1: 30 seconds
//6: 3 Minutes (default)
//127: 63.5 Minutes (max)
def val = validateRange(getAlarmDurationNumber(settings.alarmDuration), 6, 0, 127)
return configSetCmd(31, val)
}
private getAlarmDurationNumber(val) {
switch(val) {
case "Unlimited":
return 0
break
case "30 Seconds":
return 1
break
case "1 Minute":
return 2
break
case "2 Minutes":
return 4
break
case "3 Minutes":
return 6
break
case "5 Minutes":
return 10
break
case "10 Minutes":
return 20
break
case "15 Minutes":
return 30
break
case "30 Minutes":
return 60
break
case "45 Minutes":
return 90
break
case "1 Hour":
return 120
break
default:
return 6 // 3 Minutes
}
}
private alarmDurationGetCmd() {
return configGetCmd(31)
}
private disableAlarmGetCmd() {
return configGetCmd(29)
}
private disableAlarmSetCmd(disable) {
return configSetCmd(29, disable ? 1 : 0)
}
private notificationTypeGetCmd() {
return configGetCmd(7)
}
private configSetCmd(paramNumber, configValue) {
return secureCmd(zwave.configurationV1.configurationSet(parameterNumber: paramNumber, size: 1, scaledConfigurationValue: configValue))
}
private configGetCmd(paramNumber) {
return secureCmd(zwave.configurationV1.configurationGet(parameterNumber: paramNumber))
}
private secureCmd(cmd) {
if (zwaveInfo?.zw?.contains("s") || ("0x98" in device.rawDescription?.split(" "))) {
return zwave.securityV1.securityMessageEncapsulation().encapsulate(cmd).format()
}
else {
return cmd.format()
}
}
private getCheckinIntervalSettingMinutes() {
return convertOptionSettingToInt(checkinIntervalOptions, checkinIntervalSetting)
}
private getCheckinIntervalSetting() {
return settings?.checkinInterval ?: findDefaultOptionName(checkinIntervalOptions)
}
private getCheckinIntervalOptions() {
[
[name: "5 Minutes", value: 5],
[name: "10 Minutes", value: 10],
[name: "15 Minutes", value: 15],
[name: "30 Minutes", value: 30],
[name: "1 Hour", value: 60],
[name: "2 Hours", value: 120],
[name: "3 Hours", value: 180],
[name: "6 Hours", value: 360],
[name: "9 Hours", value: 540],
[name: formatDefaultOptionName("12 Hours"), value: 720],
[name: "18 Hours", value: 1080],
[name: "24 Hours", value: 1440]
]
}
private convertOptionSettingToInt(options, settingVal) {
return safeToInt(options?.find { "${settingVal}" == it.name }?.value, 0)
}
private formatDefaultOptionName(val) {
return "${val}${defaultOptionSuffix}"
}
private findDefaultOptionName(options) {
def option = options?.find { it.name?.contains("${defaultOptionSuffix}") }
return option?.name ?: ""
}
private getDefaultOptionSuffix() {
return " (Default)"
}
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"
}
}
int validateRange(val, int defaultVal, int minVal, int maxVal) {
def intVal = safeToInt(val, defaultVal)
if ("$val" != "$intVal") {
logDebug "Using $defaultVal because $val is invalid"
return defaultVal
}
else if (intVal > maxVal) {
logDebug "Using $maxVal because $intVal is too high"
return maxVal
}
else if (intVal < minVal) {
logDebug "Using $minVal because $intVal is too low"
return minVal
}
else {
return intVal
}
}
private int safeToInt(val, int defaultVal=0) {