forked from djdizzyd/ring_hubitat_codahq
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ring-api-virtual-device.groovy
1246 lines (1045 loc) · 45 KB
/
ring-api-virtual-device.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
/**
* Ring API Virtual Device Driver
*
* Copyright 2019-2020 Ben Rimmasch
* Copyright 2021 Caleb Morse
*
* 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.
*/
/**
* This device holds the websocket connection that controls the alarm hub and/or the lighting bridge
*/
import com.hubitat.app.ChildDeviceWrapper
import groovy.json.JsonOutput
import groovy.transform.Field
metadata {
definition(name: "Ring API Virtual Device", namespace: "ring-hubitat-codahq", author: "Ben Rimmasch") {
capability "Actuator"
capability "Initialize"
capability "Refresh"
attribute "mode", "string"
attribute "websocket", "string"
command "createDevices"
command "excludeDevice", [[name: "zid", type: "STRING",
description: "Add a zid of a device to skip creating. This will also suppress messages about this device being 'missing'"]]
command "excludeDeviceRemove", [[name: "zid", type: "STRING",
description: "Remove zid from exclusion list. Run 'createDevices()' after this to create the new device"]]
command "setMode", [[name: "Set Mode*", type: "ENUM", description: "Set the Location's mode", constraints: ["Disarmed", "Home", "Away"]]]
command "setDebugImpulseTypeExcludeList", [[name: "Debug Impulse Type Exclude list", type: "STRING", description: "DEBUG: Comma-delimited list of impulse types to skip logging. NOTE: 'command.complete' and 'error.set-info' cannot be excluded here. Use the type-specific filtering instead"]]
command "setDebugImpulseCommandCompleteExcludeList", [[name: "Debug Impulse Command Complete Exclude list", type: "STRING", description: "DEBUG: Comma-delimited list of command.complete commandType types to skip logging"]]
command "setDebugImpulseErrorSetInfoExcludeList", [[name: "Debug Impulse Error Set Info Exclude list", type: "STRING", description: "DEBUG: Comma-delimited list of error.set-info commandType types to skip logging"]]
}
preferences {
input name: "suppressMissingDeviceMessages", type: "bool", title: "Suppress log messages for missing/deleted devices. WARNING: This option is deprecated. Use excludeDevice instead", defaultValue: false
input name: "descriptionTextEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: false
input name: "traceLogEnable", type: "bool", title: "Enable trace logging", defaultValue: false
input name: "enableDebugPreferences", type: "bool", title: "Enable extra preferences that help with debugging", defaultValue: false
if (enableDebugPreferences) {
input name: "debugImpulseType", type: "bool", title: "DEBUG: Log msgs received that have impulses. Use setDebugImpulseTypeExcludeList to exclude impulse values", defaultValue: false
}
}
}
void logInfo(msg) {
if (descriptionTextEnable) { log.info msg }
}
void logDebug(msg) {
if (logEnable) { log.debug msg }
}
void logTrace(msg) {
if (traceLogEnable) { log.trace msg }
}
def setMode(mode) {
logDebug "setMode(${mode})"
if (state.alarmCapable) {
log.error "setMode supported from API device. Ring account has alarm present so use alarm modes!"
}
else {
parent.apiRequestModeSet(device.deviceNetworkId, mode.toLowerCase())
}
}
void setDebugImpulseTypeExcludeList(excludeList) {
state.debugImpulseTypeExcludeList = excludeList?.replaceAll("\\s", "")?.split(",") as Set
}
void setDebugImpulseCommandCompleteExcludeList(excludeList) {
state.debugImpulseCommandCompleteExcludeList = excludeList?.replaceAll("\\s", "")?.split(",") as Set
}
void setDebugImpulseErrorSetInfoExcludeList(excludeList) {
state.debugImpulseErrorSetInfoExcludeList = excludeList?.replaceAll("\\s", "")?.split(",") as Set
}
def installed() {
initialize()
}
def updated() {
initialize()
}
def initialize() {
logDebug "initialize()"
unschedule(silentWebsocketReconnect)
// Setup watchdog
unschedule(watchDogChecking) // For compatibility with old installs
unschedule(websocketWatchdog)
if ((getChildDevices()?.size() ?: 0) != 0) {
runEvery5Minutes(websocketWatchdog)
}
// If hubs are defined, then setup the websocket
if (state.hubs) {
updateTokensAndReconnectWebSocket()
} else {
log.warn "Nothing to initialize..."
}
}
void runCleanup() {
state.remove("updatedDate")
device.removeDataValue("device_id")
// Run on children
getChildDevices()*.runCleanup()
}
// Creates all devices
void createDevices() {
createDevicesEnable()
refresh()
}
void createDevicesEnable() {
logInfo "Enabling createDevices"
state.createDevices = true
// Turn createDevices back off after 60 seconds. This delay provides enough time for all hub refreshes to complete
runIn(60, createDevicesDisable)
}
void createDevicesDisable() {
logInfo "Disabling createDevices"
state.remove('createDevices') // Cleanup state
}
// @todo Should this delete the device as well?
void excludeDevice(zid) {
if (state.excludeDevices == null) {
state.excludeDevices = [].toSet()
}
state.excludeDevices.add(zid.trim())
logInfo "Zid ${zid} added to exclusion list"
}
void excludeDeviceRemove(zid) {
state.excludeDevices?.remove(zid.trim())
logInfo "Zid ${zid} removed from exclusion list"
}
// @note Should only be called by Ring Connect app
void setEnabledHubDoorbotIds(final Set<Integer> enabledHubDoorbotIds) {
state.enabledHubDoorbotIds = enabledHubDoorbotIds
state.remove('createableHubs') // Remove old key in case it still happens to be there
}
// @note Should only be called by Ring Connect app
boolean isHubPresent(final Integer doorbotId) {
final String zid = state.hubs?.find { it.value == doorbotId }?.key
return zid != null && getChildByZID(zid) != null
}
// @note Should only be called by Ring Connect app or Virtual Alarm Hub
void updateMode(final String mode) {
logInfo "Mode set to ${mode.capitalize()}"
sendEvent(name: "mode", value: mode)
}
// @note Should only be called by Ring Connect app
void updateTickets(final Map ticket) {
logDebug "updateTickets(${ticket})"
if (!ticket.host) {
log.error "updateTickets: Failed to get server from json: ${ticket}"
return
}
// Migrate to listing createableHubs with doorbot id and not the device kind
if (state.createableHubs != null) {
log.warn("Migrating old createableHubs list to new")
state.enabledHubDoorbotIds = (ticket.assets.findAll { state.createableHubs.contains(it.kind) }*.doorbotId).toSet()
state.remove('createableHubs')
}
final List enabledHubs = ticket.assets.findAll { state.enabledHubDoorbotIds.contains(it.doorbotId) }
state.hubs = enabledHubs.collectEntries { [(it.uuid): it.doorbotId] }
state.alarmCapable = enabledHubs.find { ALARM_CAPABLE_KINDS.contains(it.kind) } != null
final String wsUrl = "wss://${ticket.host}/ws?authcode=${ticket.ticket}&ack=false"
logTrace "wsUrl: $wsUrl"
try {
interfaces.webSocket.connect(wsUrl)
}
catch (e) {
logDebug "initialize error: ${e.message} ${e}"
log.error "WebSocket connect failed: ${e}"
sendEvent(name: "websocket", value: "error")
reconnectWebSocket()
}
}
// @return True if the hub is enabled
boolean isEnabledHub(final String zid) {
// Check if old state is still there. If it is, getting a new ticket from parent (which calls updateTickets) will update things
if (state.createableHubs != null && state.enabledHubDoorbotIds == null) {
log.warn("Detected old state. Re-initializing websocket to fix")
updateTokensAndReconnectWebSocket()
parent.schedulePeriodicMaintenance()
return false
}
return state.hubs?.containsKey(zid)
}
void refresh(final String zid=null) {
refreshInternal(zid, false)
}
void refreshInternal(final String zid, boolean quiet) {
logDebug "refresh(${zid})"
if (!state.hubs) {
log.warn "Nothing to refresh. No hubs configured. Use 'Discover Devices' in the main Ring app to add hubs"
return
}
final Set zidsToRefresh = zid == null ? state.hubs.keySet() : [zid]
for (final String curZid in zidsToRefresh) {
"${quiet ? "logDebug" : "logInfo"}"("Refreshing hub ${curZid}")
apiWebsocketRequestRefresh(curZid)
}
if (!state.alarmCapable) {
parent.apiRequestModeGet(device.deviceNetworkId)
}
}
void refreshInternalDelay(Map data) {
refreshInternal(data.zid, data.getOrDefault('quiet', false))
}
// For compatibility with old installs
void watchDogChecking() {
logInfo "Old watchdog function called. Setting up new watchdog."
initialize()
}
void websocketWatchdog() {
final Long lastWebSocketMsgTime = state.lastWebSocketMsgTime
if (lastWebSocketMsgTime == null) {
return
}
final Long timeSinceContact = (now() - lastWebSocketMsgTime) / 1000 / 60 // Time since last msg in minutes
logDebug "Watchdog checking. It has been ${timeSinceContact} minutes since a websocket msg was received"
if (timeSinceContact >= 5) {
logDebug "It has been ${timeSinceContact} minutes since a websocket msg was received"
if (device.currentValue("websocket") != "connected") {
log.warn "It has been ${timeSinceContact} minutes since a websocket msg was received. Reconnecting"
reconnectWebSocket()
}
else if (timeSinceContact >= 30) {
log.error "It has been ${timeSinceContact} minutes since a websocket msg was received, but websocket is still connected. This really shouldn't happen. Forcing a reconnect"
reconnectWebSocket()
}
else {
logDebug "It has been ${timeSinceContact} minutes since a websocket msg was received, but websocket is still connected. Doing nothing for now"
}
}
}
void apiWebsocketRequestRefresh(final String dst) {
logDebug "apiWebsocketRequestRefresh(${dst})"
sendWebsocketRequest([dst: dst, msg: "DeviceInfoDocGetList"])
}
void apiWebsocketRequestSetCommand(final String type, final String dst, final String zid, final Map data = [:]) {
logDebug "apiWebsocketRequestSetCommand(${type}, ${dst}, ${zid}, ${data})"
apiWebsocketRequestDeviceInfoSet(dst, [
zid: zid,
command: [v1: [[
commandType: type,
data : data,
]]],
])
}
void apiWebsocketRequestSetDevice(final String dst, final String zid, final Map data) {
logDebug "apiWebsocketRequestSetDevice(${dst}, ${zid}, ${data})"
apiWebsocketRequestDeviceInfoSet(dst, [zid: zid, device: [v1: data]])
}
void apiWebsocketRequestDeviceInfoSet(final String dst, final Map body) {
sendWebsocketRequest([
dst: dst,
msg: "DeviceInfoSet",
datatype: "DeviceInfoSetType",
body: [body], // Body is a List of Maps
])
}
// Some device values have to be set indirectly through the security-panel device. The security-panel zid is saved in the hub
void apiWebsocketRequestSetDeviceSecurityPanel(final String src, final Map data) {
logDebug "apiWebsocketRequestSetDeviceSecurityPanel(${src}, ${data})"
final String securityPanelZid = getChildByZID(src)?.getDataValue('security-panel-zid')
apiWebsocketRequestSetDevice(src, securityPanelZid, data)
}
void sendWebsocketRequest(Map msg) {
logTrace("sendWebsocketRequest(${msg})")
// Increment sequence number and add it to the request
state.seq = (state.seq ?: 0) + 1
msg.seq = state.seq
final String request = JsonOutput.toJson([channel: "message", msg: msg])
logTrace "request: ${request}"
if (device.currentValue("websocket") != "connected") {
log.error "Cannot send request because socket is not connected: ${request}"
return
}
try {
interfaces.webSocket.sendMessage(request)
}
catch (e) {
log.error "sendWebsocketRequest exception: ${e} cause: ${ex.getCause()}, request: ${request}"
}
}
void webSocketStatus(final String status) {
logDebug "webSocketStatus(${status})"
if (status == 'status: open') {
boolean isSilentWebsocketReconnect = false
final Long silentWebSocketReconnectTime = state.silentWebSocketReconnectTime
// This is a silent reconnect
if (silentWebSocketReconnectTime != null) {
logDebug "WebSocket is open (silent reconnect)"
// Cleanup state
state.remove('silentWebSocketReconnectTime')
unschedule(updateWebsocketAttributeClosedDelay) // Cancel pending update websocket attribute. We reconnected
Long timeSince = (now() - silentWebSocketReconnectTime).abs() / 1000
// If it had taken > 10 seconds to do a silent reconnect, then just do normal logging
if (timeSince <= 10) {
isSilentWebsocketReconnect = true
logDebug("Silent reconnect succeeded")
} else {
log.warn("Silent reconnect took too long (${timeSince}s > 10s)")
}
}
if (!isSilentWebsocketReconnect) {
logInfo "WebSocket is open"
sendEvent(name: "websocket", value: "connected")
}
// Refresh after a second delay. This gives the 'websocket' attribute time to be update
runIn(1, refreshInternalDelay, [data: [zid: null, quiet: true]])
// Schedule silent connect for ~4 hours from now. Should happen just before Ring automatically disconnects us
runIn(60 * 60 * 4 - new Random().nextInt(60), silentWebsocketReconnect)
state.reconnectDelay = 1
}
else if (status == "status: closing") {
boolean isSilentWebsocketReconnect = false
Long silentWebSocketReconnectTime = state.silentWebSocketReconnectTime
if (silentWebSocketReconnectTime != null) {
Long timeSince = (now() - silentWebSocketReconnectTime) / 1000
// If it has taken > 10 seconds to do a silent reconnect, then just do normal logging
isSilentWebsocketReconnect = timeSince <= 10
}
if (isSilentWebsocketReconnect) {
logDebug "WebSocket connection closing (silent reconnect)"
// Set websocket attribute to closed after 15 seconds. Avoids rapid changes to the websocket attribute
// Most of the time the reconnection happens very quickly. 15 seconds should be more than enough time
runIn(15, updateWebsocketAttributeClosedDelay)
} else {
log.warn "WebSocket connection closing."
sendEvent(name: "websocket", value: "closed")
}
}
else if (status.startsWith('failure: ')) {
log.warn("Failure message from web socket: ${status.substring("failure: ".length())}")
sendEvent(name: "websocket", value: "failure")
reconnectWebSocket()
}
else {
log.warn "WebSocket error, reconnecting."
sendEvent(name: "websocket", value: "error")
reconnectWebSocket()
}
}
void updateTokensAndReconnectWebSocket() {
// This results in updateTickets being called
parent.apiRequestTickets(device.deviceNetworkId)
state.seq = 0
}
// Ring disconnects websocket connections every 4 hours (+ ~3 seconds). This causes unnecessary log messages
// To reduce these messages we have a scheduled task that runs 4 hours after a websocket connection was opened
void silentWebsocketReconnect() {
logDebug("silentWebsocketReconnect")
state.silentWebSocketReconnectTime = now()
runInMillis(100, silentWebsocketReconnectCloseSocket)
}
// Called on a slight delay from silentWebsocketReconnect. Allows time for the silentWebSocketReconnectTime variable
// to be set to the state
void silentWebsocketReconnectCloseSocket() {
interfaces.webSocket.close()
updateTokensAndReconnectWebSocket()
}
// Sends a websocket event on a delay. Used to avoid rapid changes to the websocket attribute when we do a silent reconnect
void updateWebsocketAttributeClosedDelay() {
logDebug("updateWebsocketAttributeClosedDelay called")
sendEvent(name: "websocket", value: "closed")
}
void reconnectWebSocket() {
// First delay is 2 seconds, doubles every time
state.reconnectDelay = (state.reconnectDelay ?: 1) * 2
// Don't let delay get too crazy, max it out at 30 minutes
if (state.reconnectDelay > 1800) {
state.reconnectDelay = 1800
}
// If the socket is unavailable, give it some time before trying to reconnect
runIn(state.reconnectDelay, updateTokensAndReconnectWebSocket)
}
void uninstalled() {
getChildDevices().each {
deleteChildDevice(it.deviceNetworkId)
}
}
def parse(String description) {
final Long parseStart = now()
//logTrace "parse description: ${description}"
state.lastWebSocketMsgTime = now()
def json
try {
json = parseJson(description)
} catch (Exception e) {
if (description.startsWith("42")) {
log.warn("Websocket appears to be connected to older Ring API. Reconnecting. If this warning persists, report the issue.")
/* groovylint-disable-next-line ReturnNullFromCatchBlock */
updateTokensAndReconnectWebSocket()
} else {
log.error("Error parsing json: ${e}")
}
return
}
if (!(json instanceof Map)) {
if (description == "2" || description == "3") {
log.warn("Websocket appears to be connected to older Ring API. Reconnecting. If this warning persists, report the issue.")
updateTokensAndReconnectWebSocket()
} else {
log.error("Error parsing json. Expected to get a map. Msg was: '${description}'")
}
return
}
boolean gotDeviceInfoDocType = false
boolean unsupportedMsgtypeReceived = false
boolean unsupportedDatatypeReceived = false
final String channel = json.channel
final Map jsonMsg = json.msg
final String msgtype = jsonMsg?.msg
final String datatype = jsonMsg?.datatype
if (channel == "DataUpdate") {
if (msgtype == "DataUpdate") {
if (datatype == "DeviceInfoDocType") {
gotDeviceInfoDocType = true
}
else if (datatype == "HubDisconnectionEventType") {
// Appears to be sent when hub is offline
logDebug "Ignoring a DataUpdate.DataUpdate.HubDisconnectionEventType"
}
else if (datatype == "SystemStatusType") {
for (final Map systemStatus in jsonMsg.body) {
final String statusString = systemStatus.statusString
if (statusString == "device.find.configuring.begin") {
logDebug "Ring Alarm hub is starting to configure a new device"
}
else if (statusString == "device.find.configuring.finished") {
log.info "Ring Alarm hub has finished configuring a new device. To add this device to hubitat, run 'createDevices' in the 'Ring API Virtual Device'"
}
else if (statusString == "device.find.listening") {
logDebug "Ring Alarm hub is listening for new devices"
}
else if (statusString == "device.find.initialize") {
logDebug "Ring Alarm hub is getting ready to initialize a new device"
}
else if (statusString.startsWith("device.find.error")) {
logDebug "Ring alarm hub encountered a device.find.error error while configuring a new device"
}
else if (statusString == "device.remove.initialize") {
logDebug "Ring Alarm hub is getting ready to remove a device"
}
else if (statusString == "device.remove.listening") {
logDebug "Ring Alarm hub is listening for a device to remove"
}
else if (statusString == "device.remove.done") {
logDebug "Ring Alarm hub finished removing a device"
}
else if (statusString.startsWith("device.remove.error")) {
logDebug "Ring alarm hub encountered a device.remove.error error while removing a new device"
}
else {
log.warn("Got an unsupported DataUpdate.SystemStatusType statusString '${statusString}'")
}
}
}
else if (datatype == "RemovedDeviceType") {
for (final Map data in jsonMsg.body) {
ChildDeviceWrapper child = getChildByZID(data.zid)
if (child != null) {
log.warn("The ring device ${child} with zid ${data.zid} was removed. You may want to delete the device in hubitat as well")
} else {
logTrace("Didn't find child device with ${data.zid}. Device may have never been created")
}
}
}
else if (datatype == "DeviceAddDocType") {
for (final entry in parseDeviceInfoDocType(jsonMsg, jsonMsg.context.assetId, jsonMsg.context.assetKind)) {
log.info "A new ring device '${entry.value.name}' of type ${entry.value.deviceType} with zid ${entry.value.zid} was added"
}
}
else {
unsupportedDatatypeReceived = true
}
}
else if (msgtype == "Passthru") {
if (datatype == "PassthruType") {
if (isEnabledHub(jsonMsg.context.assetId)) {
handlePassThruType(jsonMsg)
}
} else {
unsupportedDatatypeReceived = true
}
}
else if (msgtype == "SessionInfo") {
if (datatype == "SessionInfoType") {
handleSessionInfoType(jsonMsg)
} else {
unsupportedDatatypeReceived = true
}
}
else if (msgtype == "SubscriptionTopicsInfo") {
if (datatype == "SubscriptionTopicType") {
logDebug "Ignoring a DataUpdate.SubscriptionTopicsInfo.SubscriptionTopicType"
} else {
unsupportedDatatypeReceived = true
}
}
else if (msgtype == "DisconnectClient") {
if (datatype == "DisconnectClientType") {
logDebug "Ignoring a DataUpdate.DisconnectClient.DisconnectClientType"
} else {
unsupportedDatatypeReceived = true
}
}
else {
unsupportedMsgtypeReceived = true
}
}
else if (channel == "message") {
if (msgtype == "DeviceInfoDocGetList") {
if (datatype == "DeviceInfoDocType") {
gotDeviceInfoDocType = true
}
else if (datatype == null && jsonMsg.context?.uiConnection != null) {
logDebug "Ignoring a message.DeviceInfoDocGetList with no datatype that has the context.uiConnection key"
}
else {
unsupportedDatatypeReceived = true
}
}
else if (msgtype == "DeviceInfoSet") {
// @todo The seq of these messages matches the seq of the command that was sent. This could be used to log when a specific command succeeds
if (jsonMsg.status == 0) {
logTrace "DeviceInfoSet with seq ${jsonMsg.seq} succeeded."
}
else {
log.warn "I think a DeviceInfoSet failed? Please report this to the developers so support can be added."
log.warn description
}
}
else if (msgtype == "RoomGetList") {
logDebug "Ignoring a message.RoomGetList"
}
else {
unsupportedMsgtypeReceived = true
}
}
else if (channel == "disconnect") {
logInfo "Websocket timeout hit. Reconnecting..."
interfaces.webSocket.close()
sendEvent(name: "websocket", value: "disconnect")
// We don't disconnect fast enough because we still get a failure from the status method after closing. Because
// of that failure message and reconnect there we do not need to reconnect here.
}
else {
log.warn "Received unsupported channel ${channel}. Please report this to the developers so support can be added."
log.warn description
}
if (unsupportedMsgtypeReceived) {
log.warn "Received unsupported ${msgtype} on channel ${channel}. Please report this to the developers so support can be added."
log.warn description
}
if (unsupportedDatatypeReceived) {
log.warn "Received unsupported ${msgtype}.${datatype} on channel ${channel}. Please report this to the developers so support can be added."
log.warn description
}
if (gotDeviceInfoDocType) {
final String assetId = jsonMsg.context.assetId
// Only parse events for hubs that were selected in the app
if (isEnabledHub(assetId)) {
final String assetKind = jsonMsg.context.assetKind
final String hubZid = jsonMsg.src
List<String> affectedDevices = []
final Long parseTimeStart = now()
final Map<String, Map> deviceInfos = parseDeviceInfoDocType(jsonMsg, assetId, assetKind)
final Long parseTime = now() - parseTimeStart
final Long sendUpdateStart = now()
// If the hub for these device infos doesn't exist then create it
if (!getChildByZID(assetId)) {
createChild(hubZid, [deviceType: assetKind, zid: assetId])
createDevicesEnable() // Create child devices after creating the hub
}
final boolean createDevices = state.createDevices && msgtype == "DeviceInfoDocGetList"
for (final entry in deviceInfos) {
final String zid = entry.key
final Map deviceInfo = entry.value
if (state.excludeDevices?.contains(zid)) {
logDebug("Skipping update for device ${zid} because it is excluded")
continue
}
affectedDevices.add(deviceInfo.name)
if (createDevices) {
createChild(hubZid, deviceInfo)
}
sendUpdate(assetKind, hubZid, deviceInfo)
}
final Long sendUpdateTime = now() - sendUpdateStart
logDebug "Handled msg for '${affectedDevices}' in ${now() - parseStart}ms (parseTime=${parseTime}ms, sendUpdate=${sendUpdateTime}ms)"
return
}
}
// @todo Log if it takes a long time to parse a message
logDebug "Handled websocket msg in ${now() - parseStart}ms"
}
// Keys to copy from general.v2 for a subset of devices
// @note lastCommTime seems to be useless for hub.redsky. It's always zero
@Field final static List generalV2Keys = [
'acStatus', 'batteryLevel', 'commStatus', 'componentDevices', 'fingerprint', 'lastCommTime', 'lastUpdate',
'manufacturerName', 'nextExpectedWakeup', 'serialNumber'
].asImmutable()
// Keys to copy from device.v1 for all devices
// @note For some devices, testMode is boolean, for some it is a string (sensor.glassbreak)
@Field final static List deviceV1Keys = [
'alarmInfo', 'batteryBackup', 'co', 'faulted', 'flood', 'freeze', 'groupMembers', 'lastConnectivityCheckError',
'lastNetworkLatencyEvent', 'locked', 'networks', 'networkConnection', 'powerSave', 'sensitivity', 'siren', 'status',
'smoke', 'testMode', 'transitionDelayEndTimestamp'
].asImmutable()
/**
* Passes all parsed passThrus to sendPassthru
* @param json PassthruType msg from websocket
*/
void handlePassThruType(final Map json) {
logDebug "handlePassThruType(json)"
logTrace("Parsing ${json.body.size()} PassThru msg parts")
for (final Map deviceJson in json.body) {
final String type = deviceJson.type
Map curDeviceInfo = deviceJson.data.subMap(['percent', 'timeLeft', 'total', 'transition'])
if (type == 'firmware-update.percent-complete') {
curDeviceInfo.zid = deviceJson.zid
}
else if (type == 'security-panel.countdown' || type == 'speaker-event' || type == 'halo.cloud' || type == 'halo-stats.latency') {
curDeviceInfo.zid = json.context.assetId
}
else {
log.warn("Received unsupported Passthru type '${type}'. Please report this to the developers so support can be added. ${JsonOutput.toJson(json)}")
continue
}
if (state.excludeDevices?.contains(curDeviceInfo.zid)) {
logDebug("Skipping passThru for device ${curDeviceInfo.zid} because it is excluded")
continue
}
sendPassthru(curDeviceInfo)
}
}
/**
* Passes all parsed sessionInfos to sendSessionInfo
* @param json SessionInfoType msg from websocket
*/
void handleSessionInfoType(final Map json) {
logDebug "handleSessionInfoType(json)"
logTrace("Parsing ${json.body.size()} SessionInfo msg parts")
for (final Map deviceJson in json.body) {
final String zid = deviceJson.assetUuid
if (isEnabledHub(zid)) {
// Not checking state.excludeDevices here because SessionInfo msgs are only sent for hubs
sendSessionInfo([connectionStatus: deviceJson.connectionStatus, zid: zid])
}
}
}
/**
* @param json DeviceInfoDocType msg from websocket
* @param assetId Zid of the associated base station
* @return Map where the key is the zid of the device and the value is the deviceInfo
*/
Map<String, Map> parseDeviceInfoDocType(final Map json, final String assetId, final String assetKind) {
logDebug "parseDeviceInfoDocType(json)"
//logTrace "json: ${JsonOutput.toJson(json)}"
final boolean debugImpulses = enableDebugPreferences && debugImpulseType
boolean impulseExcludesInitialized = false
// These will be lazily initialized on first use
Set<String> impulseTypeExcludeList = null
Set<String> impulseCommandCompleteExcludeList = null
Set<String> impulseErrorSetInfoExcludeList = null
Map<String, Map> deviceInfos = [:].withDefault { [:] }
final List<Map> jsonBody = json.body
for (final Map deviceJson in jsonBody) {
Map curDeviceInfo = [:]
final Map tmpGeneral = deviceJson.general?.v2
if (!tmpGeneral) {
log.error("parseDeviceInfoDocType Got a deviceJson without a general.v2 key: ${JsonOutput.toJson(deviceJson)}")
if (deviceJson.general?.v1) {
log.error("got a deviceJson.general.v1! Please let the developers know this value is still being used: ${JsonOutput.toJson(deviceJson)}")
}
continue
}
final String deviceType = tmpGeneral.deviceType
if (deviceType == null) {
log.error "parseDeviceInfoDocType Got a deviceJson with a null deviceType: ${JsonOutput.toJson(deviceJson)}"
continue
}
// Only include these device types if they have an impulse
if (IMPULSE_ONLY_DEVICE_TYPES.contains(deviceType)) {
if (!deviceJson.impulse?.v1) {
logDebug "parseDeviceInfoDocType Skipping impulse only deviceType ${deviceType} because it doesn't have an impulse"
continue
}
}
final boolean isPartialDeviceType = ALARM_HUB_PARTIAL_DEVICE_TYPES.contains(deviceType)
// Get basic keys
if (HUB_COMPOSITE_DEVICES.contains(deviceType)) {
curDeviceInfo.deviceType = assetKind
curDeviceInfo.zid = assetId // Use hub zid instead of child
curDeviceInfo[deviceType + '-zid'] = tmpGeneral.zid // Pass along the child zid
} else {
curDeviceInfo << tmpGeneral.subMap(['deviceType', 'zid'])
// context.v1.deviceName key seems to only show up for full refreshes, otherwise value is at general.v2.name
curDeviceInfo.name = tmpGeneral.name ?: deviceJson.context?.v1?.deviceName
}
/**
* Begin parse deviceJson impulse.v1
* @note impulse.v1 is parsed early because we skip heartbeat messages
*/
final List tmpImpulses = deviceJson.impulse?.v1
if (tmpImpulses) {
boolean gotHeartbeat = false
Map impulses = [:]
for (final Map impulse in tmpImpulses) {
final String impulseType = impulse.impulseType
if (debugImpulses) {
if (!impulseExcludesInitialized) {
impulseExcludesInitialized = true
impulseTypeExcludeList = state.debugImpulseTypeExcludeList
impulseCommandCompleteExcludeList = state.debugImpulseCommandCompleteExcludeList
impulseErrorSetInfoExcludeList = state.debugImpulseErrorSetInfoExcludeList
}
if (impulseType == 'command.complete') {
if (!impulseCommandCompleteExcludeList?.contains(impulse.data?.commandType)) {
log.debug("Special Debug: Got impulse command.complete (${impulse.data?.commandType}): ${JsonOutput.toJson(json)}")
}
}
else if (impulseType == 'error.set-info') {
if (!impulseErrorSetInfoExcludeList?.contains(impulse.data?.info?.command?.v1?.commandType)) {
log.debug("Special Debug: Got impulse error.set-info (${impulse.data.info.command.v1.commandType}): ${JsonOutput.toJson(json)}")
}
}
else if (!impulseTypeExcludeList?.contains(impulseType)) {
log.debug("Special Debug: Got impulse ${impulseType}: ${JsonOutput.toJson(json)}")
}
}
// Sometimes heartbeats come with other impulses, so keep building
if (impulseType == "comm.heartbeat") {
gotHeartbeat = true
}
else {
impulses[impulseType] = impulse.data
}
}
// Heartbeats don't have any new information. We can safely skip them. If for some weird reason an attribute
// was got an update in a heartbeat, that value will eventually get set when we have to do a websocket reconnect
if (gotHeartbeat) {
if (!impulses) {
logDebug("parseDeviceInfoDocType Got a heartbeat for '${curDeviceInfo.name}'. Skipping")
continue
}
logDebug("parseDeviceInfoDocType Got a heartbeat for '${curDeviceInfo.name}'. Not skipping because msg has other impulses")
}
curDeviceInfo.impulses = impulses
curDeviceInfo.impulseType = impulses.keySet().first()
}
// Hubs get information from multiple deviceTypes. Exclude some of the values to avoid incorrect values getting set
if (!isPartialDeviceType) {
/**
* Begin parse deviceJson general.v2
*/
curDeviceInfo << tmpGeneral.subMap(generalV2Keys)
if (tmpGeneral.batteryStatus && BATTERY_STATUS_DEVICE_TYPES.contains(deviceType)) {
// @note batteryStatus is also in context.v1. It seems to only do this when the value is not updated. Just use value from general.v2
curDeviceInfo.batteryStatus = tmpGeneral.batteryStatus
}
final tamperStatus = tmpGeneral.tamperStatus
if (tamperStatus != null) {
curDeviceInfo.tamper = tamperStatus == "tamper" ? "detected" : "clear"
}
/**
* Begin parse deviceJson adapter.v1
*/
// adapter.v1 only contains changed values when msgtype == "DataUpdate". When msgtype == "DeviceInfoDocGetList" it contains all values
// context.v1.adapter.v1 When present, seems to contain all values
final Map tmpAdapter = deviceJson.adapter?.v1
if (tmpAdapter) {
curDeviceInfo << tmpAdapter.subMap(['rfChannel', 'rssi', 'signalStrength'])
if (tmpAdapter.firmwareVersion) {
curDeviceInfo.firmware = tmpAdapter.firmwareVersion
}
final Map fingerprint = tmpAdapter?.fingerprint
if (fingerprint) {
if (fingerprint.firmware?.version) {
curDeviceInfo.firmware = fingerprint.firmware.version.toString() + '.' + fingerprint.firmware.subversion.toString()
}
curDeviceInfo.hardwareVersion = fingerprint.hardwareVersion
}
}
}
/**
* Begin parse deviceJson pending
*/
final Map tmpPending = deviceJson.pending
if (tmpPending) {
curDeviceInfo.pending = [:]
if (tmpPending.device?.v1) {
curDeviceInfo.pending << tmpPending.device.v1.subMap(['sensitivity'])
}
if (tmpPending.command?.v1) {
curDeviceInfo.pending.commands = tmpPending.command.v1
}
}
/**
* Begin parse deviceJson device.v1
*/
// device.v1 only contains changed values when msgtype == "DataUpdate". When msgtype == "DeviceInfoDocGetList" it contains all values
// When present, context.v1.device.v1 seems to contain all values
// @note Some keys, like alarminfo and transitionDelayEndTimestamp, are set to null on disable, but in subsequent
// message are omitted entirely. This could mean that there are some scenarios where
// updates to those values are missed entirely. We may need to handle this
final Map deviceV1 = deviceJson.device?.v1
if (deviceV1) {
final boolean isSecurityPanel = isPartialDeviceType && deviceType == 'security-panel'
curDeviceInfo << deviceV1.subMap(deviceV1Keys)
if (!isSecurityPanel) {
curDeviceInfo << deviceV1.subMap(['chirps'])
}
// Hubs get information from multiple deviceTypes. Exclude some of the values to avoid incorrect values getting set
if (!isPartialDeviceType) {
for (final entry in deviceV1.subMap(['brightness', 'level', 'volume'])) {
curDeviceInfo[entry.key] = (entry.value * 100).toInteger()
}
}
else if (isSecurityPanel) {
// security-panel stores some configuration for other devices. Break those out here
final String mode = deviceV1.mode
if (mode != null) {
curDeviceInfo.mode = mode
// device.v1.devices is only included when mode is 'some' or 'all', so fall back to context.v1.device.v1.devices
final Map deviceV1Devices = deviceV1.devices ?: deviceJson.context?.v1?.device?.v1?.devices
if (deviceV1Devices == null) {
log.error("Failed to get security-panel device.v1.devices: ${JsonOutput.toJson(deviceJson)}")
}
else {
// When mode changes, get values from devices to update bypassed, deviceActive, and sensorReporting
for (final entry in deviceV1Devices) {
Map extraDeviceInfo = [zid: entry.key]