/
Netatmo.groovy
1132 lines (1017 loc) · 49.7 KB
/
Netatmo.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
/**
* Copyright (C) Sebastian YEPES
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
import groovy.transform.Field
import groovy.json.JsonSlurper
import com.hubitat.app.ChildDeviceWrapper
@Field String VERSION = "1.2.7"
@Field List<String> LOG_LEVELS = ["error", "warn", "info", "debug", "trace"]
@Field String DEFAULT_LOG_LEVEL = LOG_LEVELS[2]
private getApiUrl() { "https://api.netatmo.com" }
private getVendorAuthPath() { "/oauth2/authorize" }
private getVendorTokenPath(){ "${apiUrl}/oauth2/token" }
private getScope() { "read_home write_home read_camera write_camera access_camera read_presence write_presence access_presence read_doorbell write_doorbell access_doorbell read_station write_station read_thermostat write_thermostat read_smokedetector write_smokedetector read_homecoach write_homecoach read_june write_june access_velux read_velux write_velux read_muller write_muller read_smarther write_smarther read_magellan write_magellan" }
private getClientId() { settings.clientId }
private getClientSecret() { settings.clientSecret }
private getServerUrl() { getFullApiServerUrl() }
private getCallbackUrl() { getServerUrl() + "/oauth/callback?access_token=${state.accessToken}" }
private getBuildRedirectUrl() { getServerUrl() + "/oauth/initialize?access_token=${state.accessToken}" }
private getWebhookUrl() { "${serverUrl}/webhook?access_token=${state.accessToken}" }
definition(
name: "Netatmo",
namespace: "syepes",
author: "Sebastian YEPES",
description: "Netatmo Product Integrations",
category: "",
oauth: true,
singleInstance: true,
iconUrl: "https://pbs.twimg.com/profile_images/1214898820577935360/cY4FJMQR_200x200.jpg",
iconX2Url: "https://pbs.twimg.com/profile_images/1214898820577935360/cY4FJMQR_400x400.jpg"
)
preferences {
page(name: "Credentials", title: "OAuth2 Credentials", content: "authPage", install: false)
page(name: "Settings", title: "Netatmo security settings", content: "Settings", install: false)
}
mappings {
path("/oauth/callback") {action: [GET: "callback"]}
path("/webhook") {action: [GET: "webhook", POST: "webhook"]}
}
def installed() {
logger("debug", "installed(${VERSION})")
if (state.driverInfo == null || state.driverInfo.isEmpty() || state.driverInfo.ver != VERSION) {
state.driverInfo = [ver:VERSION]
}
state.initialSetup = false
initialize()
}
def uninstalled() {
logger("debug", "uninstalled()")
removeChildDevices(getChildDevices())
if (state.webhookInstalled) {
dropWebhook()
}
unschedule()
}
def updated() {
logger("debug", "updated() - settings: ${settings.inspect()}")
if (!state.driverInfo?.ver || state.driverInfo.isEmpty() || state.driverInfo.ver != VERSION) {
if (state.driverInfo == null || state.driverInfo.isEmpty()) {
state.driverInfo = [ver:VERSION]
}
}
initialize()
}
def initialize() {
logger("debug", "initialize() - settings: ${settings.inspect()}")
// Pull the latest device info into state
getSecurityDevicesAndPersonList()
settings.devices.each { deviceId ->
Map detail = state?.deviceDetail[deviceId]
try {
switch(detail?.type) {
case 'NDB':
logger("info", "Creating device: Doorbell (${detail?.type}) - ${detail.name}/${detail.homeName}")
createChildDevice("Netatmo - Doorbell", detail)
break
case 'NOC':
logger("info", "Creating device: Camera Presence (${detail?.type}) - ${detail.name}/${detail.homeName}")
createChildDevice("Netatmo - Presence", detail)
break
case 'NACamera':
logger("info", "Creating device: Camera Welcome (${detail?.type}) - ${detail.name}/${detail.homeName}")
createChildDevice("Netatmo - Welcome", detail)
break
case 'NACamDoorTag':
logger("info", "Creating device: Door and Window Sensor (${detail?.type}) - ${detail.name}/${detail.cameraName}/${detail.homeName}")
createChildDevice("Netatmo - Sensor", detail)
break
case 'NSD':
logger("info", "Creating device: Smart Smoke Alarm (${detail?.type}) - ${detail.name}/${detail.homeName}")
createChildDevice("Netatmo - Smoke Alarm", detail)
break
default:
logger("warn", "initialize() - Unsupported Device (${detail?.type}) - ${detail}")
break
}
} catch (Exception e) {
logger("error", "initialize() - Device creation Exception: ${e.inspect()}")
}
}
settings.people.each { personId ->
Map detail = state?.personDetail[personId]
try {
logger("info", "Creating device: Person - ${detail.name}")
createChildDevice("Netatmo - Person", detail)
} catch (Exception e) {
logger("error", "initialize() - Device creation Exception: ${e.inspect()}")
}
}
// Cleanup any other devices that need to go away
List allInstalled = [settings.devices, settings.people].findAll().flatten()
if (allInstalled.size()>0) {
List delete = getChildDevices().findAll{ !allInstalled.contains(it.deviceNetworkId) }
logger("info", "Removing devices: ${delete.inspect()}")
delete.each { deleteChildDevice(it.deviceNetworkId) }
} else {
logger("info", "Removing all devices")
removeChildDevices(getChildDevices())
}
if (installWebhook && !state.webhookInstalled) {
addWebhook()
} else if (!installWebhook && state.webhookInstalled) {
dropWebhook()
state.webhookInstalled = false
}
// Do the initial checkState
checkState()
unschedule()
if (stateCheckInterval.toInteger()) {
if (['2' ,'5', '10', '15', '30'].contains(stateCheckInterval) ) {
schedule("0 */${stateCheckInterval} * ? * *", checkState)
} else {
schedule("0 0 */${stateCheckInterval} ? * *", checkState)
}
}
}
def checkState() {
logger("debug", "checkState()")
getSecurityDevicesAndPersonList()
def cd_devices = getChildDevices()
settings.devices.each { deviceId ->
def data = state?.deviceState[deviceId]
if (data) {
// Cameras
def child = cd_devices?.find { it.deviceNetworkId == deviceId && data['type']?.matches("^(NOC|NDB|NACamera)\$") }
child?.sendEvent(name:'switch', value: data['status'] =~ /^(connected|on)/ ? 'on' : 'off')
child?.sendEvent(name:'status', value: data['status'])
child?.sendEvent(name:'sd_status', value: data['sd_status'])
child?.sendEvent(name:'alim_status', value: data['alim_status'])
if (data['light_mode_status'] != 'N/A') { child?.sendEvent(name:'light_mode', value: data['light_mode_status']) }
if (data['quick_display_zone'] != 0) { child?.sendEvent(name:'quick_display_zone', value: data['quick_display_zone']) }
if (data['max_peers_reached'] != 'N/A') { child?.sendEvent(name:'max_peers_reached', value: data['max_peers_reached']) }
if (data['websocket_connected'] != 'N/A') { child?.sendEvent(name:'websocket_connected', value: data['websocket_connected']) }
// Sensor
child = cd_devices?.find { it.deviceNetworkId == deviceId && data['type']?.matches("^(NACamDoorTag)\$") }
child?.sendEvent(name:'contact', value: data['status'])
child?.sendEvent(name:'battery', value: data['battery_percent'])
child?.sendEvent(name:'rf', value: data['rf'])
child?.sendEvent(name:'last_activity', value: data['last_activity'])
// Smoke
child = cd_devices?.find { it.deviceNetworkId == deviceId && data['type']?.matches("^(NSD)\$") }
child?.sendEvent(name:'switch', value: data['status'] =~ /^(connected|on)/ ? 'on' : 'off')
child?.sendEvent(name:'status', value: data['status'])
child?.sendEvent(name:'battery', value: data['battery_percent'])
} else {
logger("warn", "checkState() - Could not find device ${deviceId} state")
}
}
settings.people.each { personId ->
Map data = state?.personDetail[personId]
if (data) {
def child = cd_devices?.find { it.deviceNetworkId == personId }
if (data['out_of_sight']) {
child?.away()
child?.sendEvent(name:'last_seen', value: data['last_seen'])
} else {
child?.seen()
child?.sendEvent(name:'last_seen', value: data['last_seen'])
}
} else {
logger("warn", "checkState() - status (out_of_sight) error on person ${personId}")
}
}
}
def authPage() {
logger("debug", "authPage()")
String description
Boolean uninstallAllowed = false
Boolean oauthTokenProvided = false
if (!state.initialSetup) {
state.initialSetup = true
state.webhookInstalled = false
}
if (!state.accessToken) {
try{
logger("debug", "authPage() - Creating access token")
state.accessToken = createAccessToken()
} catch(Exception e){
logger("error", "authPage() - ${e.message}")
return "${e.message}"
}
}
def redirectUrl = getBuildRedirectUrl()
logger("debug", "authPage() - redirectUrl: ${redirectUrl}")
if (state.authToken) {
description = "Tap 'Next' to proceed"
uninstallAllowed = true
oauthTokenProvided = true
} else {
description = "Click to enter Credentials"
}
if (!oauthTokenProvided) {
logger("debug", "authPage() - Showing the login page")
return dynamicPage(name: "Credentials", title: "Authorize Connection", nextPage:"Settings", uninstall: uninstallAllowed, install: false) {
section("Enter Netatmo Application Details") {
paragraph "Get these details after creating a new application on https://dev.netatmo.com/apps/createanapp"
input name: "clientId", title: "Client ID", type: "text", required: true
input name: "clientSecret", title: "Client Secret", type: "text", required: true, submitOnChange: true
}
section() {
paragraph "Click below to login to Netatmo and authorize Hubitat access"
href url: oauthInitUrl(), title: "Connect to netatmo:", description: description, external:true, required:false
}
}
} else {
logger("debug", "authPage() - Showing the device page")
return dynamicPage(name: "Credentials", title: "Connected", nextPage:"Settings", uninstall: uninstallAllowed, install: false) {
section() {
input name: "Devices", style: "embedded", title: "Netatmo is now connected to Hubitat!", description: description, required: false
}
}
}
}
def Settings() {
logger("debug", "Settings()")
Map tmpList = getSecurityDevicesAndPersonList()
Map devices = tmpList[0]
Map persons = tmpList[1]
dynamicPage(name: "Settings", title: "Settings", install: true) {
section("Security Devices") {
input name: "devices", title: "Select device(s) to track", type: "enum", required: false, multiple: true, options: devices
}
section("People") {
input name: "people", title: "Select person(s) to track (Creates presence device)", type: "enum", required: false, multiple: true, options: persons
}
section("Preferences") {
input name: "installWebhook", title: "Enable WebHook", description: "Activate webhook (Enables motion detect and person tracking)", type: "bool", defaultValue: true, required: true
}
section("Logging") {
input name: "logLevel", title: "Log Level", type: "enum", options: LOG_LEVELS, defaultValue: DEFAULT_LOG_LEVEL, required: false
input name: "logDescText", title: "Log Description Text", type: "bool", defaultValue: false, required: false
input name: "stateCheckInterval", title: "State Check", description: "Check interval of the current state", type: "enum", options:[[0:"Disabled"], [5:"5min"], [10:"10min"], [15:"15min"], [30:"30min"], [2:"1h"], [3:"3h"], [4:"4h"], [6:"6h"], [8:"8h"], [12: "12h"]], defaultValue: 2, required: true
}
}
}
def oauthInitUrl() {
try {
state.oauthInitState = UUID.randomUUID().toString()
logger("debug", "oauthInitUrl() - Set oauthInitState: ${state?.oauthInitState}")
Map oauthParams = [
response_type: 'code',
client_id: getClientId(),
client_secret: getClientSecret(),
state: state.oauthInitState,
redirect_uri: getCallbackUrl(),
scope: getScope()
]
Map params = [
uri: getApiUrl(),
path: getVendorAuthPath(),
contentType: 'application/json; charset=utf-8',
requestContentType: 'application/json; charset=utf-8',
queryString: toQueryString(oauthParams)
]
logger("debug", "oauthInitUrl() - URL: ${getApiUrl() + getVendorAuthPath()}, PARAMS: ${oauthParams.inspect()}")
logger("debug", "oauthInitUrl() - Request: ${getApiUrl() + getVendorAuthPath()}?${toQueryString(oauthParams)}")
return "${getApiUrl() + getVendorAuthPath()}?${toQueryString(oauthParams)}"
} catch(Exception e){
logger("error", "oauthInitUrl() - Request Exception: ${e.inspect()}")
}
}
def callback() {
logger("debug", "callback() - params: ${params.inspect()}")
def code = params.code
def oauthState = params.state
if (oauthState == state.oauthInitState) {
Map tokenParams = [
client_secret: getClientSecret(),
client_id : getClientId(),
grant_type: 'authorization_code',
redirect_uri: getCallbackUrl(),
code: code,
scope: getScope()
]
Map params = [
uri: getVendorTokenPath(),
contentType: 'application/x-www-form-urlencoded',
body: tokenParams
]
try {
logger("debug", "callback() - URL: ${getVendorTokenPath()}, PARAMS: ${params.inspect()}")
logger("debug", "callback() - Request: ${getVendorTokenPath()}?${toQueryString(tokenParams)}")
httpPost(params) { resp ->
logger("trace", "callback() - respStatus: ${resp?.getStatus()}, respHeaders: ${resp?.getAllHeaders()?.inspect()}, respData: ${resp?.getData()}")
JsonSlurper slurper = new JsonSlurper()
resp?.getData()?.each { key, value ->
def data = slurper.parseText(key)
state.refreshToken = data.refresh_token
state.authToken = data.access_token
state.tokenExpires = now() + (data.expires_in * 1000)
}
}
} catch (e) {
logger("debug", "callback() - Request Exception: ${e.inspect()}")
}
// Handle success and failure here, and render stuff accordingly
if (state.authToken) {
success()
} else {
fail()
}
} else {
logger("error", "callback() - Failed oauthState(${oauthState}) != state.oauthInitState(${state?.oauthInitState})")
}
}
def webhook() {
logger("trace", "webhook() - request: ${request.inspect()}")
List resp = []
resp << [name: "result" , value: "ok"]
try {
def payload = request.JSON
if (payload?.message) { logger("info", "${payload?.message}") }
logger("debug", "webhook() - requestSource: ${request.requestSource}, payload: ${payload.inspect()}")
def cd_devices = getChildDevices()
logger("debug", "webhook() - cd_devices: ${cd_devices.inspect()}")
// Smart Indoor Camera (welcome) Sensors
if (payload?.push_type?.startsWith('NACamera-tag')) {
String moduleID = payload?.module_id
ChildDeviceWrapper cd_module = cd_devices?.find { it.deviceNetworkId == moduleID }
switch (payload?.event_type) {
case 'tag_big_move':
case 'tag_small_move':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Movement detected by ${cd_module}")
cd_module?.motion(payload?.event_type?.split('_')[1])
break
case 'tag_open':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Open detected by ${cd_module}")
if (logDescText) {
log.info "${app.name} ${cd_module} is open"
} else {
logger("info", "${cd_module} is open")
}
cd_module?.sendEvent(name:'contact', value: "open")
break
case 'module_connect':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Module connected by ${cd_module}")
if (logDescText) {
log.info "${app.name} ${cd_module} is connected"
} else {
logger("info", "${cd_module} is connected")
}
cd_module?.sendEvent(name:'contact', value: "connected")
break
case 'module_disconnect':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Module disconnected by ${cd_module}")
if (logDescText) {
log.info "${app.name} ${cd_module} is disconnected"
} else {
logger("info", "${cd_module} is disconnected")
}
cd_module?.sendEvent(name:'contact', value: "disconnection")
break
case 'tag_uninstalled':
break
default:
logger("warn", "webhook() - Unhandled by ${cd_module} - event_type: ${payload?.event_type} - payload: ${payload}")
break
}
return resp
}
// Smart Indoor Camera (welcome)
if (payload?.push_type?.startsWith('NACamera')) {
String cameraID = payload?.camera_id
ChildDeviceWrapper cd_camera = cd_devices?.find { it.deviceNetworkId == cameraID }
if (!cd_camera) {
logger("warn", "webhook() - Local Camera: ${cameraID} (${payload?.home_name}) not found")
}
String personName = 'Unknown'
if (payload?.event_type == 'person') {
def cd_person = cd_devices?.find { c ->
payload?.persons?.find { p ->
if (p.id == c.deviceNetworkId) { personName = c.label; return c } else { false }
}
}
if (!cd_person) {
if (!payload?.message?.contains('Unknown')) {
logger("warn", "webhook() - Local Person: ${payload?.persons?.collect{ it?.id }} / ${payload?.persons?.collect{ it?.face_url }} (${payload?.home_name}) not found")
}
} else {
logger("debug", "webhook() - event_type: ${payload?.event_type} - Person detected (${personName}) by ${cd_camera}")
cd_person?.seen(payload?.snapshot_url)
cd_person?.contactClose(personName)
}
}
switch (payload?.event_type) {
case 'on':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera switched on by ${cd_camera}")
cd_camera?.on()
break
case 'off':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera switched off by ${cd_camera}")
cd_camera?.off()
break
case 'connection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera connection on by ${cd_camera}")
if (logDescText) {
log.info "${app.name} ${cd_camera} is connected"
} else {
logger("info", "${cd_camera} is connected")
}
cd_camera?.sendEvent(name:'switch', value: 'on')
cd_camera?.sendEvent(name:'status', value: payload?.event_type)
break
case 'disconnection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera disconnection by ${cd_camera}")
if (logDescText) {
log.warn "${app.name} ${cd_camera} is disconnected"
} else {
logger("warn", "${cd_camera} is disconnected")
}
cd_camera?.sendEvent(name:'switch', value: 'off')
cd_camera?.sendEvent(name:'status', value: payload?.event_type)
break
case 'movement':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Movement detected by ${cd_camera}")
cd_camera?.motion(payload?.snapshot_url ?: null, personName)
break
case 'human':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Movement detected (${payload?.event_type}) by ${cd_camera}")
cd_camera?.motion(payload?.snapshot_url ?: null, 'Unknown')
break
case 'person':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Person detected (${personName}) by ${cd_camera}")
cd_camera?.motion(payload?.snapshot_url ?: null, personName)
break
case 'animal':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Movement detected (${payload?.event_type}) by ${cd_camera}")
cd_camera?.motion(payload?.snapshot_url ?: null, 'Animal')
break
case 'alarm_started':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Alarm detected (${personName}) by ${cd_camera}")
cd_camera?.alarm(payload?.snapshot_url)
break
default:
logger("warn", "webhook() - Unhandled by ${cd_camera} - event_type: ${payload?.event_type} - payload: ${payload}")
break
}
}
// Smart Outdoor Camera (presence)
if (payload?.push_type?.startsWith('NOC')) {
String cameraID = payload?.camera_id
ChildDeviceWrapper cd_camera = cd_devices?.find { it.deviceNetworkId == cameraID }
if (!cd_camera) {
logger("warn", "webhook() - Local Camera: ${cameraID} (${payload?.home_name}) not found")
}
switch (payload?.event_type) {
case 'on':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera switched on by ${cd_camera}")
cd_camera?.on()
break
case 'off':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera switched off by ${cd_camera}")
cd_camera?.off()
break
case 'connection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera connection on by ${cd_camera}")
if (logDescText) {
log.info "${app.name} ${cd_camera} is connected"
} else {
logger("info", "${cd_camera} is connected")
}
cd_camera?.sendEvent(name:'switch', value: 'on')
cd_camera?.sendEvent(name:'status', value: payload?.event_type)
break
case 'disconnection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera disconnection by ${cd_camera}")
if (logDescText) {
log.info "${app.name} ${cd_camera} is disconnected"
} else {
logger("info", "${cd_camera} is disconnected")
}
cd_camera?.sendEvent(name:'switch', value: 'off')
cd_camera?.sendEvent(name:'status', value: payload?.event_type)
break
case 'light_mode':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Camera light is ${payload?.sub_type} by ${cd_camera}")
cd_camera?.sendEvent(name:'switch_light', value: payload?.sub_type)
break
case 'animal':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Animal detected by ${cd_camera}")
cd_camera?.animal(payload?.snapshot_url)
break
case 'human':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Human detected by ${cd_camera}")
cd_camera?.human(payload?.snapshot_url)
break
case 'vehicle':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Car detected by ${cd_camera}")
cd_camera?.vehicle(payload?.snapshot_url)
break
case 'movement':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Movement detected by ${cd_camera}")
cd_camera?.motion(payload?.snapshot_url)
break
default:
logger("warn", "webhook() - Unhandled by ${cd_camera} - event_type: ${payload?.event_type} - payload: ${payload}")
break
}
}
// Doorbell
if (payload?.push_type?.startsWith('NDB')) {
String doorbellID = payload?.device_id
ChildDeviceWrapper cd_doorbell = cd_devices?.find { it.deviceNetworkId == doorbellID }
if (!cd_doorbell) {
logger("warn", "webhook() - Local Doorbell: ${doorbellID} (${payload?.home_name}) not found")
return resp
}
if (payload?.push_type?.startsWith('NDB-rtc') && payload?.event_type == null) {
logger("debug", "webhook() - Ignore by ${cd_doorbell} - event_type: ${payload?.event_type} - payload: ${payload}")
return resp
}
switch (payload?.event_type) {
case 'on':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell switched on by ${cd_doorbell}")
cd_doorbell?.on()
break
case 'off':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell switched off by ${cd_doorbell}")
cd_doorbell?.off()
break
case 'connection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell connection on by ${cd_doorbell}")
if (logDescText) {
log.info "${app.name} ${cd_doorbell} is connected"
} else {
logger("info", "${cd_doorbell} is connected")
}
cd_doorbell?.sendEvent(name:'switch', value: 'on')
cd_doorbell?.sendEvent(name:'status', value: payload?.event_type)
break
case 'disconnection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell disconnection by ${cd_doorbell}")
if (logDescText) {
log.info "${app.name} ${cd_doorbell} is disconnected"
} else {
logger("info", "${cd_doorbell} is disconnected")
}
cd_doorbell?.sendEvent(name:'switch', value: 'off')
cd_doorbell?.sendEvent(name:'status', value: payload?.event_type)
break
case 'incoming_call':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell Incoming Call by ${cd_doorbell}")
cd_doorbell?.ring('incoming', payload?.snapshot_url)
break
case 'accepted_call':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell Incoming Call on by ${cd_doorbell}")
cd_doorbell?.ring('accepted', payload?.snapshot_url)
break
case 'missed_call':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Doorbell Missed Call on by ${cd_doorbell}")
cd_doorbell?.ring('missed', payload?.snapshot_url)
break
case 'human':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Human detected by ${cd_doorbell}")
cd_doorbell?.human(payload?.snapshot_url)
break
case 'movement':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Movement detected by ${cd_doorbell}")
cd_doorbell?.motion(payload?.snapshot_url)
break
default:
logger("warn", "webhook() - Unhandled by ${cd_doorbell} - event_type: ${payload?.event_type} - payload: ${payload}")
break
}
}
// Smoke
if (payload?.push_type?.startsWith('NSD')) {
String deviceID = payload?.device_id
ChildDeviceWrapper cd_device = cd_devices?.find { it.deviceNetworkId == deviceID }
if (!cd_device) {
logger("warn", "webhook() - Local Smoke Alarm: ${deviceID} (${payload?.home_name}) not found")
}
switch (payload?.event_type) {
case 'connection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Smoke Alarm connection on by ${cd_device}")
if (logDescText) {
log.info "${app.name} ${cd_device} is connected"
} else {
logger("info", "${cd_device} is connected")
}
cd_device?.sendEvent(name:'switch', value: 'on')
cd_device?.sendEvent(name:'status', value: payload?.event_type)
break
case 'disconnection':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Smoke Alarm disconnection by ${cd_device}")
if (logDescText) {
log.info "${app.name} ${cd_device} is disconnected"
} else {
logger("info", "${cd_device} is disconnected")
}
cd_device?.sendEvent(name:'switch', value: 'off')
cd_device?.sendEvent(name:'status', value: payload?.event_type)
break
case 'hush':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Smoke Alarm hushed by ${cd_device}")
if (logDescText) {
log.info "${app.name} ${cd_device} Alarm hushed for 15 min"
} else {
logger("info", "${cd_device} Alarm hushed for 15 min")
}
break
case 'smoke':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Alarm ${payload?.sub_type == 0 ? 'cleared' : 'detected'} by ${cd_device}")
cd_device?.smoke(payload?.sub_type)
break
case 'battery_status':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Battery level (${payload?.sub_type}) by ${cd_device}")
cd_device?.battery(payload?.sub_type)
break
case 'wifi_status':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Wifi Status (${payload?.sub_type == 0 ? 'error' : 'ok'}) by ${cd_device}")
if (logDescText) {
log.info "${app.name} ${cd_device} Wifi ${payload?.sub_type == 0 ? 'error' : 'ok'}"
} else {
logger("info", "${cd_device} Wifi ${payload?.sub_type == 0 ? 'error' : 'ok'}")
}
break
case 'sound_test':
logger("debug", "webhook() - event_type: ${payload?.event_type} - Sound Test (${payload?.sub_type == 0 ? 'ok' : 'error'}) by ${cd_device}")
if (logDescText) {
log.info "${app.name} ${cd_device} Sound Test ${payload?.sub_type == 0 ? 'ok' : 'error'}"
} else {
logger("info", "${cd_device} Sound Test ${payload?.sub_type == 0 ? 'ok' : 'error'}")
}
break
default:
logger("warn", "webhook() - Unhandled by ${cd_device} - event_type: ${payload?.event_type} - payload: ${payload}")
break
}
}
} catch(Exception e){
logger("debug", "webhook() - Request Exception: ${e.inspect()}")
}
return resp
}
private success() {
logger("info", "OAuth authentication flow succeeded")
String message = """
<p>We have located your netatmo account</p>
<p>Close this page and install the application again. you will not be prompted for credentials next time.</p>
"""
connectionStatus(message)
}
private fail() {
logger("warn", "OAuth authentication flow failed")
String message = """
<p>The connection could not be established with netatmo!</p>
<p>Close this page and attempt install the application again.</p>
"""
connectionStatus(message)
}
private connectionStatus(message, redirectUrl = null) {
logger("debug", "connectionStatus()")
String redirectHtml = ""
if (redirectUrl) {
redirectHtml = """
<meta http-equiv="refresh" content="3; url=${redirectUrl}" />
"""
}
String html = """
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Netatmo Connection</title>
<style type="text/css">
* {
box-sizing: border-box;
}
@font-face {
font-family: 'Swiss 721 W01 Thin';
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.eot');
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.eot?#iefix') format('embedded-opentype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.woff') format('woff'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.ttf') format('truetype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-thin-webfont.svg#swis721_th_btthin') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Swiss 721 W01 Light';
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.eot');
src: url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.eot?#iefix') format('embedded-opentype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.woff') format('woff'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.ttf') format('truetype'),
url('https://s3.amazonaws.com/smartapp-icons/Partner/fonts/swiss-721-light-webfont.svg#swis721_lt_btlight') format('svg');
font-weight: normal;
font-style: normal;
}
.container {
width: 100%;
padding: 40px;
/*background: #eee;*/
text-align: center;
}
svg {
vertical-align: middle;
}
img {
vertical-align: middle;
}
img:nth-child(2) {
margin: 0 30px;
}
p {
font-size: 2.2em;
font-family: 'Swiss 721 W01 Thin';
text-align: center;
color: #666666;
margin-bottom: 0;
}
span {
font-family: 'Swiss 721 W01 Light';
}
</style>
</head>
<body>
<div class="container">
<svg width="280px" height="150px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 613.82 111.751"><g><path fill="#193B6A" d="M581.925 39.672c-15.989 0-28.996 13.007-28.996 28.996v11.859c0 15.989 13.007 28.996 28.996 28.996h.014c15.989 0 28.996-13.007 28.996-28.996V68.668c.001-15.988-13.007-28.996-29.01-28.996zm18.687 40.855c0 10.296-8.376 18.672-18.672 18.672h-.014c-10.296 0-18.672-8.376-18.672-18.672V68.668c0-10.296 8.376-18.672 18.687-18.672 10.296 0 18.672 8.376 18.672 18.672v11.859h-.001zM243.969 39.685c-15.78.454-28.144 13.809-28.144 29.596v11.246c0 15.989 13.007 28.996 29.011 28.996h3.83a28.992 28.992 0 0020.748-8.741l2.146-2.198a2.092 2.092 0 00-.035-2.956l-4.395-4.292a2.09 2.09 0 00-2.956.035l-2.146 2.197c-3.544 3.631-8.29 5.63-13.377 5.63h-3.83c-10.312 0-18.672-8.36-18.672-18.672v-.267h45.6a2.084 2.084 0 002.084-2.084v-9.508c-.001-16.27-13.473-29.454-29.864-28.982zm19.539 30.251h-37.36v-1.268c0-10.296 8.376-18.672 18.688-18.672 10.296 0 18.673 8.376 18.673 18.672v1.268h-.001zM200.955 20.08h-6.22a2.084 2.084 0 00-2.084 2.084v66.669l-54.072-68.192a1.517 1.517 0 00-1.178-.561h-4.854a2.084 2.084 0 00-2.084 2.084v85.109a2.09 2.09 0 002.084 2.084h6.22a2.084 2.084 0 002.084-2.084v-66.98l54.072 68.504v-.001c.291.357.72.561 1.178.561h4.854a2.084 2.084 0 002.084-2.084v-85.11a2.083 2.083 0 00-2.084-2.083zM430.543 40.304h-14.292V26.899a2.09 2.09 0 00-2.09-2.09h-6.143a2.09 2.09 0 00-2.09 2.09v13.405h-6.758a2.09 2.09 0 00-2.09 2.09v6.143a2.09 2.09 0 002.09 2.09h6.758v40.824c0 9.777 7.926 17.703 17.703 17.703h6.913a2.09 2.09 0 002.09-2.09v-6.143a2.09 2.09 0 00-2.09-2.09h-6.913a7.38 7.38 0 01-7.38-7.38V50.628h14.292a2.09 2.09 0 002.09-2.09v-6.143a2.09 2.09 0 00-2.09-2.091zM313.938 40.304h-14.292V26.899a2.09 2.09 0 00-2.09-2.09h-6.143a2.09 2.09 0 00-2.09 2.09v13.405h-6.758a2.09 2.09 0 00-2.09 2.09v6.143a2.09 2.09 0 002.09 2.09h6.758v40.824c0 9.777 7.926 17.703 17.703 17.703h6.912a2.09 2.09 0 002.09-2.09v-6.143a2.09 2.09 0 00-2.09-2.09h-6.912a7.38 7.38 0 01-7.38-7.38V50.628h14.292a2.09 2.09 0 002.09-2.09v-6.143a2.09 2.09 0 00-2.09-2.091zM514.884 39.683c-8.491-.152-16.069 3.999-20.848 10.385-4.68-6.299-12.177-10.389-20.616-10.389a25.52 25.52 0 00-15.331 5.097v-2.382a2.09 2.09 0 00-2.09-2.09h-6.143a2.09 2.09 0 00-2.09 2.09v64.67a2.09 2.09 0 002.09 2.09h6.143a2.09 2.09 0 002.09-2.09v-41.73c0-8.643 7.189-15.638 15.915-15.321 8.304.302 14.76 7.383 14.76 15.693v41.358a2.09 2.09 0 002.09 2.09h6.143a2.09 2.09 0 002.09-2.09v-41.73c0-8.642 7.188-15.638 15.914-15.321 8.304.302 14.761 7.383 14.761 15.693v41.359a2.09 2.09 0 002.09 2.09h6.143a2.09 2.09 0 002.09-2.09v-41.73c.001-13.992-11.257-25.402-25.201-25.652zM359.84 39.672h-2.818a28.992 28.992 0 00-20.748 8.741l-2.146 2.198a2.09 2.09 0 00.036 2.956l4.395 4.291a2.09 2.09 0 002.956-.036l2.145-2.198c3.544-3.63 8.289-5.629 13.376-5.629h2.803c9.112 0 15 4.613 15 11.753v5.005H350.73c-11.697 0-23.537 7.222-23.537 21.026 0 14.939 12.201 21.744 23.537 21.744 9.646 0 18.027-4.257 24.109-9.711v7.253a2.09 2.09 0 002.09 2.09h6.143a2.09 2.09 0 002.09-2.09V61.748c.002-10.632-7.923-22.076-25.322-22.076zm-9.109 59.527c-12.261 0-13.214-8.741-13.214-11.42 0-3.975 2.783-10.702 13.214-10.702h24.109v4.94c-.119 3.438-9.701 17.182-24.109 17.182z"></path> <defs><path id="a" d="M3.088 41.122v62.335c0 4.884 5.603 7.645 9.476 4.669l37.246-28.62a5.886 5.886 0 017.168-.005l37.249 28.536c3.874 2.968 9.469.206 9.469-4.674v-62.24c0-1.832-.853-3.56-2.307-4.674L56.972 2.421a5.888 5.888 0 00-7.162 0L5.395 36.448a5.886 5.886 0 00-2.307 4.674z"></path></defs> <clipPath id="b"><use overflow="visible" xlink:href="#a"></use></clipPath> <linearGradient id="c" x1="-9.87" x2="31.427" y1="92.084" y2="50.788" gradientTransform="translate(0 4)" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#ff7911"></stop> <stop offset=".565" stop-color="#fe7711"></stop> <stop offset=".769" stop-color="#fb700f"></stop> <stop offset=".913" stop-color="#f5650c"></stop> <stop offset="1" stop-color="#f05909"></stop></linearGradient> <path fill="url(#c)" d="M3.643 38.641a5.88 5.88 0 00-.555 2.48v66.281c0 3.272 3.753 5.121 6.348 3.128l43.956-33.777L3.643 38.641z" clip-path="url(#b)"></path> <linearGradient id="d" x1="-13.714" x2="30.214" y1="89.133" y2="52.273" gradientTransform="translate(0 4)" gradientUnits="userSpaceOnUse"><stop offset=".5" stop-color="#ff8d15" stop-opacity="0"></stop> <stop offset=".67" stop-color="#fe8a14" stop-opacity=".34"></stop> <stop offset=".772" stop-color="#fa8212" stop-opacity=".543"></stop> <stop offset=".855" stop-color="#f3740f" stop-opacity=".711"></stop> <stop offset=".929" stop-color="#ea600a" stop-opacity=".858"></stop> <stop offset=".996" stop-color="#de4603" stop-opacity=".992"></stop> <stop offset="1" stop-color="#dd4403"></stop></linearGradient> <path fill="url(#d)" d="M3.643 38.641a5.88 5.88 0 00-.555 2.48v66.281c0 3.272 3.753 5.121 6.348 3.128l43.956-33.777L3.643 38.641z" clip-path="url(#b)"></path> <path fill="#FF8F15" d="M3.643 38.641l49.749 38.112 49.749-38.112a5.891 5.891 0 00-1.752-2.193L55.991 1.67a4.275 4.275 0 00-5.199 0l-31.91 24.446L5.394 36.448a5.87 5.87 0 00-1.751 2.193z" clip-path="url(#b)"></path> <path fill="#FFA618" d="M53.391 76.753l3.579 2.742.002.001 34.275 26.258 6.105 4.677c2.595 1.988 6.343.138 6.343-3.131V41.122c0-.87-.198-1.713-.555-2.481L53.391 76.753z" clip-path="url(#b)"></path> <linearGradient id="e" x1="78.969" x2="115.549" y1="87.97" y2="61.036" gradientTransform="translate(0 4)" gradientUnits="userSpaceOnUse"><stop offset=".436" stop-color="#ff8f15"></stop> <stop offset=".741" stop-color="#ff9916" stop-opacity=".46"></stop> <stop offset="1" stop-color="#ffa618" stop-opacity="0"></stop></linearGradient> <path fill="url(#e)" d="M53.391 76.753l3.579 2.742.002.001 34.275 26.258 6.105 4.677c2.595 1.988 6.343.138 6.343-3.131V41.122c0-.87-.198-1.713-.555-2.481L53.391 76.753z" clip-path="url(#b)"></path></g></svg>
<img src="https://s3.amazonaws.com/smartapp-icons/Partner/support/connected-device-icn%402x.png" alt="Connected device icon" />
<img src="https://cdn.shopify.com/s/files/1/2575/8806/t/20/assets/logo-image-file.png" alt="Hubitat logo" /><br/>
${message}
</div>
</body>
</html>
"""
render contentType: 'text/html', data: html
}
boolean refreshToken() {
logger("debug", "refreshToken()")
Map oauthParams = [
client_secret: getClientSecret(),
client_id: getClientId(),
grant_type: 'refresh_token',
refresh_token: state.refreshToken
]
Map params = [
uri: getVendorTokenPath(),
contentType: 'application/x-www-form-urlencoded',
body: oauthParams,
]
// OAuth Step 2: Request access token with our client Secret and OAuth "Code"
try {
boolean rc = false
logger("debug", "refreshToken() - URL: ${getVendorTokenPath()}, PARAMS: ${params.inspect()}")
logger("debug", "refreshToken() - Request: ${getVendorTokenPath()}?${toQueryString(oauthParams)}")
httpPost(params) { resp ->
logger("trace", "refreshToken() - respStatus: ${resp?.getStatus()}, respHeaders: ${resp?.getAllHeaders()?.inspect()}, respData: ${resp?.getData()}")
JsonSlurper slurper = new JsonSlurper();
if (resp && resp.getStatus() == 200) {
resp?.getData()?.each {key, value ->
def data = slurper.parseText(key)
state.refreshToken = data.refresh_token
state.netatmoAccessToken = data.access_token
state.tokenExpires = now() + (data.expires_in * 1000)
}
logger("info", "Refresh token success")
rc = true
} else {
logger("warn", "Refresh token failed")
rc = false
}
}
return rc
} catch (Exception e) {
logger("debug", "refreshToken() - Request Exception: ${e.inspect()}")
return false
}
// We didn't get an access token
if ( !state.netatmoAccessToken ) {
return false
}
}
Map getSecurityDevicesAndPersonList() {
logger("debug", "getSecurityDevicesAndPersonList()")
Map deviceList = [:]
Map personList = [:]
Map combinedList = [:]
state.deviceDetail = [:]
state.deviceState = [:]
state.personDetail = [:]
apiGet("/api/gethomedata",[:]) { resp ->
logger("trace", "getSecurityDevicesAndPersonList() - respStatus: ${resp?.getStatus()}, respHeaders: ${resp?.getAllHeaders()?.inspect()}, respData: ${resp?.getData()}")
if (resp) {
state.response = resp.data.body
resp.data.body.homes.each { home ->
home.cameras.each { camera ->
String key = camera.id
String cameraName = "${camera.name} (${home.name})"
String cameraAKey = camera?.containsKey('vpn_url') ? camera.vpn_url.split('/').getAt(5) : 'N/A'
deviceList[key] = cameraName
state.deviceDetail[key] = ["id" : key]
state.deviceDetail[key] << ["name" : cameraName]
state.deviceDetail[key] << ["type" : camera.type]
state.deviceDetail[key] << ["homeID" : home.id]
state.deviceDetail[key] << ["homeName" : home.name]
state.deviceDetail[key] << ["vpn_url" : camera.vpn_url]
state.deviceDetail[key] << ["access_key" : cameraAKey]
state.deviceState[key] = ["type": camera.type]
state.deviceState[key] << ["status": camera.status]
state.deviceState[key] << ["sd_status": camera.sd_status]
state.deviceState[key] << ["alim_status": camera.alim_status]
state.deviceState[key] << ["last_setup": camera.last_setup]
state.deviceState[key] << ["is_local": camera?.containsKey('is_local') ? camera.is_local : 'N/A']
state.deviceState[key] << ["light_mode_status": camera?.containsKey('light_mode_status') ? camera.light_mode_status : 'N/A']
state.deviceState[key] << ["quick_display_zone": camera?.containsKey('quick_display_zone') ? camera.quick_display_zone : 0]
state.deviceState[key] << ["max_peers_reached": camera?.containsKey('max_peers_reached') ? camera.max_peers_reached : 'N/A']
state.deviceState[key] << ["websocket_connected": camera?.containsKey('websocket_connected') ? camera.websocket_connected : 'N/A']
camera?.modules?.each { module ->
String key_mod = module.id
String moduleName = "${module.name} (${home.name}/${camera.name})"
deviceList[key_mod] = moduleName
state.deviceDetail[key_mod] = ["id" : key_mod]
state.deviceDetail[key_mod] << ["name" : moduleName]
state.deviceDetail[key_mod] << ["type" : module.type]
state.deviceDetail[key_mod] << ["category" : module.category]
state.deviceDetail[key_mod] << ["homeID" : home.id]
state.deviceDetail[key_mod] << ["homeName" : home.name]
state.deviceDetail[key_mod] << ["cameraID" : camera.id]
state.deviceDetail[key_mod] << ["cameraName" : camera.name]
state.deviceState[key_mod] = ["status": (module.status == 'no_news' ? 'disconnection' : module.status)]
state.deviceState[key_mod] << ["type": module.type]
state.deviceState[key_mod] << ["monitoring": module.monitoring]
state.deviceState[key_mod] << ["rf": module.rf]
state.deviceState[key_mod] << ["battery_percent": module.battery_percent]
state.deviceState[key_mod] << ["last_activity": module.last_activity]
}
}
home.persons.each { person ->
if (person.pseudo) {
String key = person.id
String personName = person.pseudo
personList[key] = personName
state.personDetail[key] = ["id" : key]
state.personDetail[key] << ["name" : personName]
state.personDetail[key] << ["homeID" : home.id]
state.personDetail[key] << ["homeName" : home.name]
state.personDetail[key] << ["last_seen" : person.last_seen]
state.personDetail[key] << ["out_of_sight" : person.out_of_sight]
}
}
home.smokedetectors.each { smokedetector ->
String key = smokedetector.id
String smokedetectorName = "${smokedetector.name} (${home.name})"
deviceList[key] = smokedetectorName
state.deviceDetail[key] = ["id" : key]
state.deviceDetail[key] << ["name" : smokedetectorName]
state.deviceDetail[key] << ["type" : smokedetector.type]
state.deviceDetail[key] << ["homeID" : home.id]
state.deviceDetail[key] << ["homeName" : home.name]
state.deviceState[key] = ["type": smokedetector.type]
state.deviceState[key] << ["last_setup": smokedetector.last_setup]
}
}
}
}
// Merge data
logger("debug", "getSecurityDevicesAndPersonList() - deviceList: ${deviceList.sort()}, personList: ${personList.sort()}")
combinedList[0] = deviceList.sort() { it.value.toLowerCase() }
combinedList[1] = personList.sort() { it.value.toLowerCase() }
return combinedList
}
def setAway(homeID) {
logger("debug", "setAway(${homeID})")
setAway(homeID, null)
}
def setAway(homeID, personID) {
logger("debug", "setAway(${homeID}, ${personID})")
Map query = ["home_id": homeID]
if (personID) {
query <<["person_id": personID]
}