-
Notifications
You must be signed in to change notification settings - Fork 3
/
a31 DO NOT USE - INCOMPLETE
10109 lines (9364 loc) · 493 KB
/
a31 DO NOT USE - INCOMPLETE
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
// 22/Aug/2023 from 192.168.0.8 [C8] - beta 3e pre 31 ALPHA
/*
Copyright Kevin Hawkins 2019, 2020, 2021 [email protected]
RESTRICTED LICENCE:
See associated licence file.
By usage of this application you accept the terms of the licence.
Currently no part of this code may be copied, redistributed, altered or used in any way without my express written/email permission.
This code may be installed for the express purpose of testing and reporting issues. You may also
alter and adapt this code for your own personal useage, but it may not be onwardly distributed to anyone in any form, or portions used in any other available application.
My current intention is to relax the terms of this licence upon general release.
No representation of any form is made to the suitability of this code or it's fitness for any purpose at all. Use entirely at your own discretion and risk . No liability accepted.
Logging code was adapted from Eric Vitale's ST LIFX application, with thanks. Copyright 2016 [email protected]
Many thanks for contributions from Casey, Jeff, Cody, Kirk, Andy, Jon and more. Much appreciated.
Support via Hubitat community beta threads
https://community.hubitat.com/t/beta-mqtt-app/32750
You can contact me via PM on that forum @kevin
Bug reports to this GitHub repository please (issues)
*/
/* #########################################################################################################
This application is provided free of charge .. enjoy.
Should you wish you can help keep me awake longer via this link
https://www.buymeacoffee.com/xAPPO
Many thanks .. much appreciated
######################################################################################################### */
// PLEASE NOTE: As this application uses static @Field variables you can't just edit/save the code and continue running (as they get purged). It will need completely restarting each time you edit the code to run correctly.
import java.security.MessageDigest
//import groovy.json.JsonSlurper
import groovy.transform.Field
@Field static String block=''
@Field static String hsvBlock='FREE'
@Field static String nodes = ''
@Field static Map topicLink = [:]
@Field static Map properties = [:]
@Field static Map nameMap = [:]
@Field static Map newMap = [:]
@Field static Map dynVars = [:]
@Field static String lastDevDim='none'
@Field static String lastDevOnOff='none'
@Field static String lastDevColTemp='none'
@Field static String lastDevColor='none'
@Field static String lastDevHue='none'
@Field static String lastDevSat='none'
@Field static String lastDevBri='none'
@Field static String lastDev='none' // deprecated
//counter = 0 // No explicit type definition.
definition(
name: "MQTT",
namespace: "ukusa",
author: "Kevin Hawkins",
importUrl: "https://raw.githubusercontent.com/GledholtHall/beta-3/master/MQTT%20app",
description: "Links MQTT with HE devices",
category: "Intranet Connectivity",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-BigButtonsAndSwitches.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]",
installOnOpen: true,
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]"
)
preferences { // can't use atomicState vars in this section
//page(name: "Configuration", title: "<h2><b>MQTT</b></h2>", nextPage: "vMQTT", uninstall: false, hideable: true,hideWhenEmpty: true){
page(name: "configuration", title: "<h2><b>MQTT</b></h2>",install: true, uninstall: true, hideable: true,hideWhenEmpty: true){
section(" ver: beta 3e pre 31 (alpha)"){}
section ("<b>MQTT Broker</b>", hideable: true, hidden: true){
//createMQTTclient()
//input "mqtt2", "device.MQTTClient", required: false, multiple: false, title: ""
//input "mqtt", "device.MQTTClient", required: false, multiple: false, title: "<b>MQTT Broker</b>", submitOnChange: false
input name: "hubName", type: "text", title: "<b>Hub Name</b>", description: " choose a unique name for this Hubitat Hub", required: true, displayDuringSetup: true, submitOnChange: false
input name: "MQTTBroker", type: "text", title: "<b>MQTT Broker Address</b>      prefixed tcp://...", description: "e.g. tcp://192.168.1.17:1883 - NB you must include tcp://...", required: true, displayDuringSetup: true
input name: "username", type: "text", title: "<b>MQTT Username</b>", description: "(leave blank if none)", required: false, displayDuringSetup: true
input name: "password", type: "password", title: "<b>MQTT Password</b>", description: "(leave blank if none)", required: false, displayDuringSetup: true
input "cleanSession", "bool", title: "<b>Clean Session:</b><br> If OFF then should your MQTT session disconnect MQTT will remember your subscriptions and send messages you missed upon reconnection. [default: ON] requires HE v2.2.2+ ", required: true, defaultValue: true, submitOnChange: false
//input "newMQTTDev", "button", title: "Create MQTT broker device driver", textColor: "green"
}
section ("<b>Configuration</b>", hideable: true, hidden: true) {
input "mqttRemoveDevices", "bool", title: "<b>Purge Discovered Devices</b><br> WARNING: Setting this will delete all your 'discovered devices' when you click 'Done'. However your selected devices from HA statestream, discovery and homie discovery will be re-added automatically when app is run again but you will need to re-add them manually in your Dashboards as the ID changes. Your selected 'published' devices and any manually created 'ad-hoc' devices will not be affected", required: true, defaultValue: false, submitOnChange: false
input "WipeDevices", "bool", title: "<b>Forget enabled devices</b><br> Warning you will have to manually re-enable your devices for export and import from MQTT", submitOnChange: false
input "logging", "enum", title: "<b>Log Level</b>", required: false, defaultValue: "INFO", options: ["TRACE", "DEBUG", "INFO", "WARN", "ERROR", "DISABLED"]
input "tempUnits", "enum", title: "<b>Temperature units</b>", required: false, defaultValue: "Celsius x.xx°C", options: ["Celsius x.xx°C", "Celsius x.x°C", "Fahrenheit °F","Fahrenheit x.x°F"]
input "lengthUnits", "enum", title: "<b>Length units (not yet functional)</b>", required: false, defaultValue: "Metric (mm,m,km)", options: ["Metric (mm,m,km)", "Imperial (inches/feet/miles)"]
input "allowMqttUnlock", "bool", title: "<b>Allow Unlock via MQTT</b><br>WARNING: Setting this will allow unlocking locks via MQTT. It is recommended that your MQTT instance requires username/password & is protected by SSL", required: false, defaultValue: false, submitOnChange: false
input "allowHSMDisarm", "bool", title: "<b>Allow HSM disarm via MQTT</b><br>WARNING: Setting this will allow HSM disarm via MQTT, provided 'Allow HSM control' is ON", required: false, defaultValue: false, submitOnChange: false
input "allowHSMControl", "bool", title: "<b>Allow HSM control via MQTT</b><br>WARNING: Setting this will allow all HSM control via MQTT (except disarming if 'Allow HSM disarm' is OFF)", required: false, defaultValue: false, submitOnChange: false
input "mqttKeypadMode", "enum", title: "<b>Allow Keypad control and specify when a code is needed</b><br>WARNING: Setting this will allow controlling Keypade code via MQTT. It is recommended that your MQTT instance requires username/password & is protected by SSL", required: false, defaultValue: "No control of keypad", options: ["No control of keypad", "No Keycode needed", "Keycode to Arm only","Keycode to Arm/Disarm"]
input name: "mqttKeypadCode", type: "text", title: "Keypad Code", description: " Code to be used via HA", submitOnChange: false, required: false
}
section ("<b>MQTT Publish Formats</b>", hideable: true, hidden: true){
//input "HEBasic", "bool", title: "<b>Hubitat basic MQTT</b>", required: true, defaultValue: false, submitOnChange: false
input "homiePublish", "bool", title: "<b>homie 3 protocol</b>", required: true, defaultValue: true, submitOnChange: false
input "minHomie", "bool", title: "<b>Complete & compliant homie topics</b>", required: true, defaultValue: false, submitOnChange: false
input "homieStatesPersist","bool",title: "<b>    ... retain homie states</b>", required: false, defaultValue: true, submitOnChange: false
input "HADiscovery", "bool", title: "<b>Home Assistant MQTT discovery protocol (requires homie3 publish enabled)</b>", required: true, defaultValue: false, submitOnChange: false
input name: "HADiscoveryTopic", type: "text", title: "<b>Home Assistant Discovery Topic</b>", description: " as configured in HA", required: false, displayDuringSetup: true, submitOnChange: false
input "HARemember", "enum", title: "<b>Home Assistant MQTT discovered devices</b>", required: false, defaultValue: "Remember",options: ["Forget", "Remember"], submitOnChange: false
}
section ("<b>Discovery into HE</b>",hideWhenEmpty: true, hideable: true, hidden: true) {
href(name: "href2",
title: "Discovery Protocols",
required: false,
page: "discovery")
}
section ("<b>Virtual Devices</b>",hideWhenEmpty: true, hideable: true, hidden: true) {
href(name: "href",
title: "HE Virtual devices associated with arbitrary MQTT topics",
required: false,
page: "vMQTT")
}
section ("<b>Publish these HE devices to MQTT</b>",hideWhenEmpty: true, hideable: true, hidden: true) {
input "modes", "bool", title: "Mode changes", required: false
input "hsm", "bool", title: "Hubitat Security Monitor", required: false
input "everything", "capability.*",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Everything (all capabilities/attributes a selected device supports)</b>", submitOnChange: false
input "alarms", "capability.alarm",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Alarms</b>", submitOnChange: false
// input "actuators", "capability.actuator",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Actuators</b>", submitOnChange: false
input "batterysensors", "capability.battery",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Battery sensors</b>", submitOnChange: false
input "bulbs", "capability.bulb",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Bulbs</b>", submitOnChange: false
input "buttons", "capability.button",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Buttons</b>", submitOnChange: false
input "buttonshold", "capability.holdableButton",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Buttons holdable</b>", submitOnChange: false
input "buttonspush", "capability.pushableButton",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Buttons pushable</b>", submitOnChange: false
input "buttonsrelease", "capability.releasableButton",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Buttons releasable</b>", submitOnChange: false
input "buttonsdtap", "capability.doubleTapableButton",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Buttons double tapable</b>", submitOnChange: false
input "carbonmonoxidesensors", "capability.carbonMonoxideDetector",hideWhenEmpty: true,multiple: true, required: false, title: "<b>Carbon monoxide detectors</b>", submitOnChange: false
input "chimes", "capability.chime",hideWhenEmpty: true,multiple: true, required: false, title: "<b>Chimes</b>", submitOnChange: false
input "colour", "capability.colorControl",hideWhenEmpty: true,multiple: true, required: false, title: "<b>Colour control light devices</b>", submitOnChange: false
input "colourT", "capability.colorTemperature",hideWhenEmpty: true,multiple: true, required: false, title: "<b>Colour temperature light devices</b>", submitOnChange: false
input "colourMode", "capability.colorMode",hideWhenEmpty: true,multiple: true, required: false, title: "<b>Colour Mode devices</b>", submitOnChange: false
input "contactsensors", "capability.contactSensor",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Contact sensors</b>", submitOnChange: false
input "dimmers", "capability.switchLevel",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Dimmers</b>", submitOnChange: false
input "fans", "capability.fanControl",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Fans</b>", submitOnChange: false
input "garagedoors", "capability.garageDoorControl",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Garage Door</b>", submitOnChange: false
input "humiditysensors", "capability.relativeHumidityMeasurement",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Humidity sensors</b>", submitOnChange: false
input "illuminancesensors", "capability.illuminanceMeasurement",hideWhenEmpty: false, multiple: true, required: false, title: "<b>Illuminance sensors</b>", submitOnChange: false
input "keypads", "capability.securityKeypad",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Keypads</b>", submitOnChange: false
input "locks", "capability.lock",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Locks</b>", submitOnChange: false //Casey
input "lights", "capability.light",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Lights</b>", submitOnChange: false //Casey
input "motionsensors", "capability.motionSensor",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Motion sensors</b>", submitOnChange: false
input "mqtttext", "capability.telnet",hideWhenEmpty: true, multiple: true, required: false, title: "<b>MQTT Text</b>", submitOnChange: false
input "musicplayers", "capability.musicPlayer",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Music Players</b>", submitOnChange: false
input "outlets", "capability.outlet",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Outlets</b>", submitOnChange: false
input "powersensors", "capability.powerMeter",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Power sensors</b>", submitOnChange: false
input "presencesensors", "capability.presenceSensor",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Presence sensors</b>", submitOnChange: false
input "ramp", "capability.switchLevel",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Ramp control via MQTT</b>", submitOnChange: false
input "refresh", "capability.*",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Refresh (if supported by device)</b>", submitOnChange: false //Casey
input "relayswitches", "capability.relaySwitch",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Relay Switches</b>", submitOnChange: false
input "sensors", "capability.sensor",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Sensors</b>", submitOnChange: false
input "shocksensors", "capability.shockSensor",hideWhenEmpty: true, title: "<b>Shock sensors</b>", multiple: true, required: false
input "smokesensors", "capability.smokeDetector",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Smoke detectors</b>", submitOnChange: false
input "speechsynthesis", "capability.speechSynthesis",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Speech Synthesis</b>", submitOnChange: false
input "switches", "capability.switch",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Switches</b>", submitOnChange: false
input "tempsensors", "capability.temperatureMeasurement",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Temperature sensors</b>", submitOnChange: false
input "thermostats", "capability.thermostat",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Thermostats</b>", submitOnChange: false
input "valves", "capability.valve",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Valves</b>", submitOnChange: false
input "globVars", "device.VirtualOmniSensor", required: false, title: "<b>Global Variables</b>",hideWhenEmpty: true, multiple: true, submitOnChange: false
input "gVars", "device.RMConnectorVariable", required: false, title: "<b>New Global Vars</b>",hideWhenEmpty: true, multiple: true, submitOnChange: false
//input "variables", "device.VirtualOmniSensor",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Variables</b>", submitOnChange: false
input "voltagesensors", "capability.voltageMeasurement",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Voltage sensors</b>", submitOnChange: false
input "watersensors", "capability.waterSensor",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Water Sensors</b>", submitOnChange: false
input "windowshades", "capability.windowShade",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Window Shades</b>", submitOnChange: false
input "listAll", "capability.*",hideWhenEmpty: true, title: "<b>List devices Capabilities and Attributes to Hubitat Topic</b>", multiple: true, required: false
//TODO add remaining sensors type (as and if requested)
}
}
//page(name: "vMQTT", nextPage: "discovery")
page(name: "vMQTT", nextPage: "configuration")
page(name: "synchDiscoveries", nextPage: "configuration")
page(name: "synchHA", nextPage: "configuration")
page(name: "discovery", title: "Select MQTT discovered devices", install: false,uninstall: false, nextPage: "configuration")
//page(name: "deleteDevice", title: "Delete virtual Device", nextPage: "configuration")
page(name: "deleteDevice", title: "Delete MQTT virtual Device", nextPage: "vMQTT")
}
def deleteDevice(params) {
log ( "params: ${params}", "INFO")
// <-- must have parameter here to access values passed in as href params
dynamicPage(name: "deleteDevice") {
section {
paragraph "<b>$params.name</b>"
section {
if (params.origin=="user" && (params.enabled)) {
input "delVDev", "button", title: "Delete device <b>$params.name</b>", textColor: "red"
}
else{
paragraph 'This device is not enabled'
}
}
paragraph 'Click "Next" to return to the Virtual MQTT Devices page.'
}
}
}
def vMQTT() {
dynamicPage(name: "vMQTT", title: "<h2><b>Virtual MQTT Devices and Data</b></h2>(optional)", install:false, nextPage: "configuration") {
atomicState.vList=["RM Connector Variable", "MQTT Text","Remote Thermostat", "Virtual audioVolume","Virtual CO Detector", "Virtual Color Temperature Light", "Virtual Color Temperature Light KH", "Virtual Contact Sensor", "Virtual Dimmer","Virtual Dimmer KH", "Virtual Fan Controller", "Virtual Garage Door Controller", "Virtual Humidity Sensor", "Virtual Illuminance Sensor", "Virtual Lock", "Virtual Moisture Sensor", "Virtual Motion Sensor", "Virtual Multi Sensor", "Virtual Omni Sensor", "Virtual Presence", "Virtual RGB Light", "Virtual RGBW Light","Virtual RGBW Light KH", "Virtual Shade","Virtual Smoke Detector", "Virtual Switch", "Virtual Temperature Sensor", "Virtual Thermostat" ]
section {
input "virtuals", "capability.virtual",hideWhenEmpty: true, multiple: true, required: false, title: "<b>Virtual Devices</b>", submitOnChange: false
input(name: "capability", type: "enum", title: "Device Type",description: null, multiple: false, required: false, submitOnChange: true, options: atomicState.vList)
}
attList=[]
//skip=false
mqtt = getChildDevice("MQTT: Child device driver")
log ("Device is $atomicState.dni", "LOG")
log ("======================================================================", "LOG")
settings.each {k,v ->
//log.debug "it.name=$k, it.value=$v"
if (k.startsWith("vAr_")) {
//log.debug "Inputs: $k $v"
//log.debug ("Stored: " + dynVars[k])
if (dynVars[k] != v) {
if (dynVars[k]==null)
{
log("ignored null dynVars ","LOG")
dynVars[k]=v
}
log("$k changed from " + dynVars[k] + " to $v","KH")
udDNI=''
index1=k.indexOf('_',8)
//log ("Index1 is $index1","KH")
if (index1 != -1) {
index2=k.indexOf('_',index1+1)
//log ("Index2 is $index2","KH")
if (index2 != -1) {
index3=k.indexOf('_',index2+1)
//log ("Index3 is $index3","KH")
if (index3 != -1) {
index4=k.indexOf('_',index3+1)
if (index4==-1) log ("string was $k", "KH")
else {
//log ("Index4 is $index4","KH")
udDNI=k.substring(index1+1, index3)
}
if (udDNI!=null) {
log ("udDNI: $udDNI","KH")
udDev=getChildDevice (udDNI)
if (udDev!=null) {
log ("udDevice: $udDev.displayName","KH")
aTopic=k.substring(index3+1)
if ((aTopic!=null)&&(aTopic!='')){
if (aTopic=="text_MAP") return // block a text_MAP being created
if (v=='~~delete~~') {
udDev.removeDataValue ("$aTopic")
log ("Deleted data $aTopic","WARN")
}
else {
log ("AttributeData: $aTopic","KH")
log ("[$udDev.displayName] Updated Data $aTopic to [$v]","KH")
udDev.updateDataValue("$aTopic", "$v")
if ((mqtt!=null)&&(aTopic.endsWith("_Topic"))){
mqtt.subscribeTopic ("$v")
// need to add to topiclink too
updateTopicLink (udDev.displayName,v, udDNI)
}
}
}
}
}
}
else {
udDNI=k.substring(index1+1)
log ("udDNI: $udDNI","KH")
log ("Attribute data is missing from this device", "KH")
}
}
}
dynVars[k]=v // recreates the current
}
}
}
log ("======================================================================", "LOG")
if (atomicState.Mappings==null) atomicState.Mappings=[:]
if (capability) {
//log ("cAPABILITY is *************************** "+ capability, "KH")
def String varCap = "var_" + "${capability}".replaceAll("\\s","")
sensor=false
//skip=false
//log ("Skip set to false $skip","KH")
settings[varCap].each { virtDev ->
try {
dOrigin = (virtDev.getDataValue("origin"))
if (virtDev !=null) {
//skip=false
//log ("Skip set to false $skip","KH")
//log ("VirtDev is *************************** "+ varCap, "KH")
//log ("VirtDev is *************************** "+ virtDev, "KH")
virtDev.updateDataValue("mqtt", "enabled") // remove ?? not useful as we don't ever disable them ...
dOrigin = (virtDev.getDataValue("origin"))
if ((dOrigin=='user')||(dOrigin=="HA Discovery")) { // This avoids registering HE devices (again)
log ("Subscribe to events from virtual device $virtDev","LOG")
registerAll (virtDev,0,'auto')
}
} }
catch (e) {
log (" [310] This has probably been deleted","KH")
//skip=true
//log ("Skip set to true $skip","KH")
}
}
dType = "capability.nowt"
if (capability.contains("Sensor")||capability.contains("Presence")) sensor=true
dType="device."+capability.replaceAll("\\s","")
// TODO iNVESTIGATE IMPACT OF USING NON VIRTUAL DEVICES
//else if (capability == "All Switches") dType= "capability.switch"
//else if (capability == "All Dimmers") dType= "capability.switchLevel"
section {
atomicState.tete =[]
input(name: varCap, type: dType, title: "MQTT enabled $capability devices",description: null, multiple: true, required: false, submitOnChange: true)
}
// if (skip==false){
//log ("Skip appears to be false $skip","KH")
section {
// vDev="Deleted"
app.removeSetting("vDev")
//log ("dType is " + dType,"KH")
input(name: "rabbit", type: dType, title: "Edit this $capability device",description: null, multiple: false, required: false, submitOnChange: true)
vDev=rabbit
//if (vDev=="Deleted") log ("vDev is DELETED", "KH")
//if (vDev != null) log ("rabbit is " + rabbit +"~~", "KH")
if (vDev == null) {
//log ("Apparently vDev is NULL","KH")
app.updateSetting("vDevName","")
input (name: "vDevName",type: "text", title: "... or create a new virtual $capability device called ..." , required: false, submitOnChange: true)
if (vDevName != null){
atomicState.newDevName=vDevName // This isn't enough to identify it from an incoming MQTT message - can only identify by topic
atomicState.newDevType=capability
input "newVDev", "button", title: "Create device $vDevName", textColor: "green"
}
else {
//atomicState.vDevName
}
}
else {
log ("editing device","KH")
// try {
//dOrigin = (virtDev.getDataValue("origin"))
enabled=false
settings[varCap].each { enab ->
log ("enab is " + enab, "KH")
log ("varCAP is " + varCap, "KH")
if (enab==null || enab=="") log ("varCAP has a NULL or empty value", "KH")
if ("$enab"=="$vDev") enabled=true
}
// input "editVDev", "button", title: "Edit device $vDev", textColor: "blue"
dOrigin = (vDev.getDataValue("origin"))
//log ("Device is $vDev.displayName","INFO")
//log ("Origin is $dOrigin","INFO")
atomicState.device=vDev
//if (dOrigin=="user" && (enabled)) input "delVDev", "button", title: "Delete device $vDev", textColor: "red"
/* }
catch (e) {
log ("Block deleted device editing","KH")
}
*/
}
}
// }// end section
// else log ("Skipped because is a deleted device","KH")
} // end capability
atomicState.first=false
if (vDev) { // && atomicState.edit) { // this is the whole page
// if ((dOrigin=='user'){ // open it to all
if ((dOrigin=='user')||(dOrigin=='HA Discovery')||(dOrigin=='Home Assistant')||(dOrigin=="C-Bus")||(dOrigin=='homie')) {
log ("Selected Device is ${vDev.displayName} - last was {$atomicState.vDev}","DEBUG")
if (vDev.displayName==atomicState.vDev) newDev=false else newDev=true
log ("newDev is $newDev","LOG")
if (newDev) {
log("#### Blocking any data updates to device from previous screen ####","INFO")
atomicState.attList=[]
temp=atomicState.attList
if (vDev.getTypeName()=="RM Connector Variable") temp.add("variable")
else if (vDev.getTypeName()=="MQTT Text") temp.add("text")
else if (vDev.getTypeName()=="Remote Thermostat") temp.add("whatsit")
vDev.capabilities.each { cap ->
log ("Found $cap in vDev $vDev.displayName","INFO")
cap.attributes.each { attr ->
log ("Found attribute $attr in $vDev.displayName [$cap]", "WARN")
temp.add("${attr}")
}
}
log ("Attribute list is now $temp","WARN")
atomicState.attList=temp
// atomicState.attList now contains every attribute that the newly selected device has but caution - may not align with attrList=vDev.getSupportedAttributes()
// this is because 1) I force text values into atomicState.attList e.g. 'variable' and 'text' and also attrList has ALL (custom) attributes not just those within a capability
}
else log ("Refresh of page with no change of device","LOG") //something changed or F5
section {
// input(name: "atts", type: "enum", title: "Device Attributes",description: null, multiple: false, required: false, submitOnChange: true, options: ["Hi","you"])
}
section {
def hrefParams = [
name: vDev.displayName,
enabled: enabled,
uid: vDev.deviceNetworkId,
origin: dOrigin,
]
href (name: "myHref", page: "deleteDevice", title: "<font color='red'> Go to Delete Device $vDev.displayName</font>" , params: hrefParams)
}
atomicState.vDev=vDev.displayName
if (enabled) { // don't display if not mqtt enabled
section {
temp=atomicState.attList.sort()
atomicState.attList=temp
//input(name: "atts", type: "enum", title: "Device atomic Attributes",description: null, multiple: false, required: false, submitOnChange: true, options: atomicState.attList)
assisted=false // beta2 and beta3 .. disabling this feature temporarily as confusing
if (assisted) input (name: "devWildcard", type: "text", title: "Please enter a wildcarded topic encompassing the whole device but no others", submitOnChange: true)
if ((assisted) && (devWildcard != null)) {
atomicState.dTopic=devWildcard
atomicState.devTopics = (atomicState.devTopics != null)?atomicState.devTopics:[]
input "topics", "button", title: "Get device topics from MQTT for ${devWildcard}"
input "deviceTopics", "enum", multiple: false, title: "<b>MQTT discovered topics within ${devWildcard} device</b>", options: atomicState.devTopics.sort()
if (!sensor){
input(name: "cmdSuffix", type: "text", title: "Command suffix for topics eg /set or /cmd", required: false, submitOnChange: true)
}
}
} //end section
atomicState.devTopics = (atomicState.devTopics != null)?atomicState.devTopics:[]
temp2=atomicState.devTopics
for (i=0; i < temp2.size(); i++) {
temp2[i]=temp2[i]+ "${cmdSuffix}"
}
section { //data value update section
atomicState.dni = vDev.displayName
for (i = 0; i < atomicState.attList.size(); i++) { // loops for every attribute device has
def String var = "vAr_Var_${i}_"+vDev.deviceNetworkId+'_'
def String varCmd = "vAr_Cmd_${i}_"+vDev.deviceNetworkId+'_'
log ("The accompanying [$i] attribute is $atomicState.attList[i]","LOG")
def String varAtt = "vAr_Att_${i}_"+vDev.deviceNetworkId+'_'
//def String varJSON = "vAr_JSN_${i}_"+vDev.deviceNetworkId+'_'
// TODO Check - if this is a new device what happens
// Purge previous device settings
log ( "var is " + var + " and attribute is "+ atomicState.attList[i],"LOG")
try {
data=vDev.getDataValue("${atomicState.attList[i]}_Topic")
log ("newDev is $newDev for ${atomicState.attList[i]}","ERROR")
if (newDev) app.updateSetting (var,data)
dataAtt=vDev.getDataValue("${atomicState.attList[i]}_MAP")
log ("Recovered data for map $i $dataAtt","LOG")
if (newDev) app.updateSetting (varAtt,dataAtt)
//dataJSON=vDev.getDataValue("${atomicState.attList[i]}_JSON")
// if (newDev) app.updateSetting (varJSON,dataJSON) //could be null
}
catch (e) {
log ("No state for $i","DEBUG")
}
if (!sensor){
try {
dataC=vDev.getDataValue("${atomicState.attList[i]}_Cmd")
log ("_Cmd recovered $varCmd -- $dataC -- $newDev","KH")
if (newDev) app.updateSetting (varCmd,dataC)
}
catch (e) {
log ("No cmd for $i","DEBUG")
}
}
if (!newDev){ // this happens when a data value was updated and the device is redisplayed (it also happens when page two is originally entered too)
log ("Settings for $var is ${settings[var]} ~ ${attList[i]}", "KH")
if ((settings[var]!=null) && (settings[var]!=data)) {
//log (" ","KH")
//log ("[$i] UPDATE device $vDev.displayName - ${atomicState.attList[i]}_Topic","KH")
//log ("[$i] UPDATING - from " + vDev.getDataValue("${atomicState.attList[i]"}_Topic),"KH")
// disabled vDev.updateDataValue("${atomicState.attList[i]}_Topic", settings[var].trim())
myVal=settings[var].trim() // this is not the latest value :-(
// ############################# TODO need to strip JSON
// log ("Updating topicLink .. $myVal = $vDev.deviceNetworkId","LOG")
//if (temp.containsKey(myVal)){
// list=temp[myVal] //.add(topic)
updateTopicLink(vDev.displayName, myval,vDev.deviceNetworkId)
log ("[$i] UPDATED on add to $myVal","KH") // ${settings[var]}","LOG")
jas=myVal.indexOf("{")
if ((jas>0) && (myVal.endsWith(':}'))){ //strip json
//log ("Strip JSON value $defOne for subscribe" , "KH")
myVal=myVal.substring(0, jas)
}
if (mqtt!=null) mqtt.subscribeTopic ("$myVal") // // ("${settings[var]}")
}
if ((settings[varAtt]!=null) && (settings[varAtt]!=dataAtt)) {
//TODO THIS IS A TEMPORARY FIX / HACK
//===================================
//attrList=vDev.getSupportedAttributes()
//if (attrList[i]!=null && attrList[i].dataType =="ENUM") {
if ((atomicState.attList[i]=='level')|(atomicState.attList[i]=='text')|(atomicState.attList[i]=='battery')) { // TODO A lot more of these shouldn't have map data
log ("Blocking adding _MAP data for $atomicState.attList[i]","KH")
log ("was to be " + settings[varAtt].trim(),"KH")
}
else {
log ("Updated[$i] " + atomicState.attList[i] +"_Map to" + settings[varAtt].trim(),"LOG")
// disabled vDev.updateDataValue("${atomicState.attList[i]}_MAP", settings[varAtt].trim())
}
}
if (!sensor) {
if ((settings[varCmd]!=null) && (settings[varCmd]!=dataC)) {
// ToDo - be able to send a JSON command
//log ("[$i] UPDATE device $vDev.displayName - ${atomicState.attList[i]}_Cmd","KH")
//log ("[$i] UPDATING - from " + vDev.getDataValue("${atomicState.attList[i]}_Cmd"),"KH")
//myVal=settings[varCmd].trim()
//log ("myVal cmd $myVal vDev $vDev.deviceNetworkId","DEBUG")
//temp[myVal] = vDev.deviceNetworkId // Don't get incoming messages on Cmd topics
//log ("[$i] UPDATED to ${settings[varCmd]}","KH")
//log (" ","DEBUG")
// subscribe this device for all attributes/events
log ("Subscribing to events from device $vDev","KH")
registerAll (vDev,0,'auto')
}
}
} // not newDev
log ("atomicState.devTopics is $atomicState.devTopics","TRACE") //maybe old - for the last looked up device
log ("atomicState.attList is $atomicState.attList","TRACE")
if (assisted) {
var=var+"${atomicState.attList[i]}_Topic"
input(name: var, type: "enum", title: "<b> ${atomicState.attList[i]} </b> attribute MQTT status topic [$var]", description: null, multiple: false, required: false, submitOnChange: true, options: atomicState.devTopics.sort() + "/sett")
if ((atomicState.attList[i]=='battery')|(atomicState.attList[i]=='networkStatus')) { // Block _Cmd data entry fields // Should really check is MQTT Text device when block networkStatus
}
else {
varCmd=varCmd+"${atomicState.attList[i]}_Cmd"
if (!sensor) input(name: varCmd, type: "enum", title: "<b> ${atomicState.attList[i]} </b> MQTT command topic", description: null, multiple: false, required: false, submitOnChange: true, options: temp2.sort())
}
}
else {
defOne=vDev.getDataValue("${atomicState.attList[i]}_Topic")
if (i==0){
if ((defOne != null)&&(defOne!='')){
log ("TODO This is now not a valid topic to use for ID [$i] [$defOne] ","KH")
// This is not now viable as one topic can now map to several devices
//if (i==0){
try {
jas=defOne.indexOf("{")
if ((jas>0) && (defOne.endsWith(':}'))){
// log ("Strip JSON value $defOne for DNI" , "KH")
defOne=defOne.substring(0, jas )
}
//vDev.setDeviceNetworkId("MQTT:virtual_" + defOne )
// log ("$vDev.displayName - Blocked updating DeviceNetworkId with $defOne","KH")
}
catch(e) {
log ("DNI nametopic [MQTT:virtual_$defOne already exists please choose another: " +e,"ERROR")
dup=getChildDevice("MQTT:virtual_"+defOne)
if (dup!=null) log ("You already have a HE device [$dup.displayName] mapped to this MQTT topic","WARN")
}
}
}
//if (i>0) log ("Several state topics for $vDev","DEBUG")
}
defTwo=vDev.getDataValue("${atomicState.attList[i]}_Cmd")
var=var+"${atomicState.attList[i]}_Topic"
app.updateSetting(var,defOne) //KH
input(name: var, type: "text", defaultValue: defOne,title: "<b> ${atomicState.attList[i]} </b> attribute MQTT status topic", description: null, multiple: false, required: false, submitOnChange: true)
if ((atomicState.attList[i]=='battery')|(atomicState.attList[i]=='networkStatus')) { // Block _Cmd data entry fields // Should really check is MQTT Text device when block networkStatus
}
else {
varCmd=varCmd+"${atomicState.attList[i]}_Cmd"
if (defTwo!=null) app.updateSetting(varCmd,defTwo) //KH
if (!sensor) input(name: varCmd, type: "text", defaultValue: defTwo, title: "<b> ${atomicState.attList[i]} </b> MQTT command topic", description: null, multiple: false, required: false, submitOnChange: true)
}
// try {
// BUG TOFIX our i values aren't aligned - we might skip attributes
attrList=vDev.getSupportedAttributes() // why do this when we have atomicState.attList[] - these may not align as this misses 'text and 'variable' but includes custom attributes and those not within a capability
// attrList is an attribute object atomicState.AttList is a text list of attributes.
log (atomicState.attList,"LOG") // << much bigger list - complete
log (attrList,"LOG")
//log(attrList[i].dataType + "- networkStatus should be ENUM - its string", "TRACE")
if (attrList[i]!=null && attrList[i].dataType =="ENUM") {
String atts = "${attrList[i].getPossibleValues()}"
log ("ENUM on " + attrList[i] + " === " +atomicState.attList[i] + " === " + atts, "LOG")
//if (vDev.getDataValue("${atomicState.attList[i]}_MAP")){
if (vDev.getDataValue("${attrList[i]}_MAP")){
// Think I already have this in dataFalse
//try {dataAtt=vDev.getDataValue("${atomicState.attList[i]}_MAP")} catch(e) {log ("FAIL OFF","WARN")}
try {dataAtt=vDev.getDataValue("${attrList[i]}_MAP")} catch(e) {log ("FAIL OFF","WARN")}
// if (newDev){
app.updateSetting(varAtt,dataAtt)
defAtt=dataAtt
// }
//if (atomicState.attList[i] != "level") { // Need to expand this to other numerics and non binaries
if ("${attrList[i]}" != "level") { // Need to expand this to other numerics and non binaries ?? Cant be it's an ENUM
if (attrList[i].dataType =="ENUM") {
//varAtt=varAtt+"${atomicState.attrList[i]}_MAP"
varAtt=varAtt+"${attrList[i]}_MAP"
//input(name: varJSON, type: "text", defaultValue: '', title: "JSON {attribute:} name (optional)" , required: false, submitOnChange: false, hideWhenEmpty: true, width: 4)
//input(name: varAtt, type: "text", defaultValue: defAtt, title: "        MQTT payload map values for <b>$atts</b> for the <b>${attrList[i]}</b> attribute <br> $varAtt         [status] or [status][command]" , required: false, submitOnChange: true)// , width: 6)
input(name: varAtt, type: "text", defaultValue: defAtt, title: "<b>${attrList[i]}</b> attribute MQTT payload map values to <b>${atts.replace(', ',',')}</b> for both [status & command] or different [status][command]" , required: false, submitOnChange: true)// , width: 6)
}
if (!newDev) {
//log ("2a Updating old ${atomicState.attList[i]}_MAP with ${settings[varAtt]} on $vDev.displayName","KH")
//if (settings[varAtt] != null) vDev.updateDataValue("${atomicState.attList[i]}_MAP", settings[varAtt])// $settings?.stateOFF)
//log.debug "2b Updating old ${atomicState.attList[i]}_JSON with ${settings[varJSON]} on $vDev.displayName"
//if ((settings[varJSON] != null)&&(settings[varJSON] != ' ')) vDev.updateDataValue("${atomicState.attList[i]}_JSON", settings[varJSON])// $settings?.stateOFF)
}
}
}
else {
log ("Adding the default value for ${atomicState.attList[i]}_MAP","KH")
//vDev.updateDataValue("${atomicState.attList[i]}_MAP", atts)
atts=atts.replace(', ', ',')
// disabled vDev.updateDataValue("${attrList[i]}_MAP", atts.trim())
log ("Updating with #$atts# #${atts.trim()}#","WARN")
}
} //not ENUM
}// end for loop on each attribute (i)
} // end data value update section
try {
if (vDev.getDataValue("max_Level")){
app.removeSetting("maxLev")
section {
currentMax=vDev.getDataValue("max_Level")
if (currentMax != "null") { //null is a text value
app.updateSetting("maxLev",currentMax)
//if (maxLev!=null) currentMax=maxLev
if (!assisted) input(name: "maxLev", type: "text",defaultValue: currentMax, title: "Please enter the maximum possible level:" , required: false, submitOnChange: true)
else input(name: "maxLev", type: "enum",defaultValue: currentMax, title: "Please enter the maximum possible level: [$maxLev]" , required: false, submitOnChange: true,multiple: false, options: atomicState.devTopics.sort())
//if (!newDev) vDev.updateDataValue("max_Level", "$Topic6")
}
}
}
}
catch (e) {}
//atomicState.edit=false
} // not enabled for MQTT
else {
if (vDev!=null) section (title: "This device <b> $vDev </b> is not enabled above") { }
}
} //dOrigin=user
else section (title: "This device <b> $vDev </b> was not created within this app and so can't be edited here") { }
} // end dynamic page (vDev)
}
}
//=========================================================================================================================================================================================================================================
def synchDiscoveries() {
mqtt = getChildDevice("MQTT: Child device driver")
if ((settings?.CBusDiscovery == true) && (settings?.CBusTopic != '') && (settings?.CBusTopic != null))
{
mqtt.setCBusTopic(settings?.CBusTopic)
numEnabled=0
if (settings.CBus_light!=null) numEnabled += settings?.CBus_light.size()
if (settings.CBus_text!=null) numEnabled += settings?.CBus_text.size()
if (numEnabled != atomicState.CBNumEnabled) {
log ("Need to run CBus Discovery $numEnabled ~ atomicState.CBNumEnabled", "WARN")
atomicState.CBNumEnabled=numEnabled
CBusDiscovery()
}
}
atomicState.homie=settings?.homieDevice
if ((settings?.homieDiscovery == true) &&(settings?.homieDevice != '') && (settings?.homieDevice != null))
{
/* numEnabled=0
if (Homie_onoff!=null) numEnabled += settings?.Homie_onoff.size()
if (Homie_dim!=null) numEnabled += settings?.Homie_dim.size()
if (Homie_sensor!=null) numEnabled += settings?.Homie_sensor.size()
if (Homie_button!=null) numEnabled += settings?.Homie_button.size()
if (Homie_variable!=null) numEnabled += settings?.Homie_variable.size()
if (Homie_unknowns!=null) numEnabled += settings?.Homie_unknowns.size()
if (numEnabled != atomicState.homieNumEnabled) {
log ("Need to run homie Discovery $numEnabled ~ $atomicState.homieNumEnabled", "WARN")
atomicState.HomieNumEnabled=numEnabled
homieDiscovery()
}
*/
/*
mqtt.subscribeTopic('homie/'+atomicState.homie+'/$nodes')
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/$properties') //Started using this
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/$type') //TODO from here could get a device > type list as this returns all devices with a type
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/onoff')
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/dim')
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/dim/$format') // to recover max_Level
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/+/$unit')
mqtt.subscribeTopic('homie/'+atomicState.homie+'/+/$name')
*/
mqtt.setHomieDevice(settings?.homieDevice)
//mqtt.subscribeTopic('homie/'+atomicState.homie+"/+/"+'$properties')
settings?.Homie_onoff.each {val ->
//log ("homie onoff Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_dim.each {val ->
//log ("homie dim Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_colTemp.each {val ->
//log ("homie colour Temperature Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_RGB.each {val ->
//log ("homie RGB Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_sensor.each {val ->
//log ("homie Sensor Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_button.each {val ->
//log ("homie Button Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_variable.each {val ->
//log ("homie variable Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
settings?.Homie_lock.each {val ->
//log ("homie Lock Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
}
/*
settings?.Homie_unknown.each {val ->
log ("homie variable Name is " + val, "KH")
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$name')
mqtt.subscribeTopic('homie/'+atomicState.homie+"/$val/"+'$type')
*/
}
atomicState.HA=settings?.HAStatestreamTopic
if ((settings?.HAStatestream == true) && (settings?.HAStatestreamTopic != '') && (settings?.HAStatestreamTopic != null))
{
if (HA_Lights!=null) log ("HA Enabled lights: " + settings?.HA_Lights,"KH")
// TODO investigate determining just new ones and also removing disabled devices TODO
/*
// always running currently - likely faster
numEnabled=0
if (HA_Sensors!=null) numEnabled += settings?.HA_Sensors.size()
if (HA_BinarySensors!=null) numEnabled += settings?.HA_BinarySensors.size()
if (HA_Groups!=null) numEnabled += settings?.HA_Groups.size()
if (HA_Presence!=null) numEnabled += settings?.HA_Presence.size()
if (HA_InputBooleans!=null) numEnabled += settings?.HA_InputBooleans.size()
if (numEnabled != atomicState.HANumEnabled) {
log ("Need to run HA Discovery $numEnabled ~ $atomicState.HANumEnabled", "WARN")
atomicState.HANumEnabled=numEnabled
HADiscovery()
}
*/
settings?.HA_Lights.each {val ->
// Now add these individually as discovery is based on device_class <<< Think wrong as device_class often absent - Need to double check this TODO
mqtt.subscribeTopic(atomicState.HA+"/light/$val/friendly_name")
//mqtt.subscribeTopic('homie/'+atomicState.normHubName+'/$pacing')
//pacingSet("HAFriendly1End")
}
settings?.HA_Switches.each {val ->
log ("Switch Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/switch/$val/friendly_name")
}
settings?.HA_Sensors.each {val ->
log ("Sensor Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/sensor/$val/friendly_name")
}
settings?.HA_BinarySensors.each {val ->
log ("BinarySensor Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/binary_sensor/$val/friendly_name")
}
settings?.HA_Groups.each {val ->
log ("Group Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/group/$val/friendly_name")
}
settings?.HA_Presence.each {val ->
log ("Presence Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/person/$val/friendly_name")
}
settings?.HA_InputBooleans.each {val ->
log ("InputBoolean Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/input_boolean/$val/friendly_name")
}
settings?.HA_Covers.each {val ->
log ("Cover Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/cover/$val/friendly_name")
}
settings?.HA_Locks.each {val ->
log ("Lock Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/lock/$val/friendly_name")
}
settings?.HA_DeviceTrackers.each {val ->
log ("Device Tracker Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/device_tracker/$val/friendly_name")
}
settings?.HA_Climates.each {val ->
log ("Climate Name is " + val, "KH")
mqtt.subscribeTopic(atomicState.HA+"/climate/$val/friendly_name")
}
// TODO Some are missing here !
}
if ((settings?.HAInDiscovery==true) && (settings?.HAInTopic!=null))
{
/*
numEnabled=0
if (HAIn_switch!=null) numEnabled = settings?.HAIn_switch.size()
if (HAIn_light!=null) numEnabled += settings?.HAIn_light.size()
if (HAIn_sensors!=null) numEnabled += settings?.HAIn_sensor.size()
if (HAIn_binary!=null) numEnabled += settings?.HAIn_binary.size()
if (HAIn_cover!=null) numEnabled += settings?.HAIn_cover.size()
if (HAIn_climate!=null) numEnabled += settings?.HAIn_climate.size()
if (HAIn_person!=null) numEnabled += settings?.HAIn_person.size()
if (HAIn_automation!=null) numEnabled += settings?.HAIn_automation.size()
if (HAIn_group!=null) numEnabled += settings?.HAIn_group.size()
if (HAIn_lock!=null) numEnabled += settings?.HAIn_lock.size()
if (numEnabled != atomicState.HAInNumEnabled) {
log ("Need to run HAIn Discovery $numHAInDevs ~ $atomicState.HAInNumEnabled", "WARN")
atomicState.HAInNumEnabled=numEnabled
HAInDiscovery()
}
*/
settings?.HAIn_switch.each {val ->
homieSubs("switch",val)
}
settings?.HAIn_light.each {val ->
homieSubs("light",val)
}
settings?.HAIn_sensor.each {val ->
homieSubs("sensor",val)
}
settings?.HAIn_binary.each {val ->
homieSubs("binary_sensor",val)
}
settings?.HAIn_cover.each {val ->
homieSubs("cover",val)
}
settings?.HAIn_climate.each {val ->
homieSubs("climate",val)
}
settings?.HAIn_person.each {val ->
homieSubs("person",val)
}
settings?.HAIn_automation.each {val ->
homieSubs("automation",val)
}
settings?.HAIn_group.each {val ->
homieSubs("group",val)
}
settings?.HAIn_lock.each {val ->
homieSubs("lock",val)
}
}
dynamicPage(name: "synchDiscoveries", title: "<h2><b>Synchronising enabled Discoveries</b></h2><b><b> .. in background - you can click 'Next' and changes will appear in HE shortly</b> ", nextPage: "configuration") {}
}
def homieSubs(type,val) {
val=normalize2(val)
log ("homie sub to " + settings?.HAInTopic+"/$type/$val/config", "WARN")
mqtt.subscribeTopic(settings?.HAInTopic+"/$type/$val/config") //two topic constructs
mqtt.subscribeTopic(settings?.HAInTopic+"/$type/+/$val/config")
//mqtt.subscribeTopic(settings?.HAInTopic+"/$type/+/+/$val/config")
//mqtt.subscribeTopic(settings?.HAInTopic+"/$type/$val/state") //two topic constructs
// mqtt.subscribeTopic(settings?.HAInTopic+"/$type/+/$val/state")
//mqtt.subscribeTopic(settings?.HAInTopic+"/$type/+/+/$val/state")
}
def discovery() { // can use atomicState vars in this section
devVersion=true
dynamicPage(name: "discovery", title: "<h2><b>MQTT Discovery Protocols into HE </b></h2>", nextPage: synchDiscoveries) {
//dynamicPage(name: "discovery", title: "<h2><b>MQTT Discovery Protocols into HE </b></h2>", install: false, uninstall: false, nextPage: "configuration") {
section ("(optional)"){
//if (settings?.homieDiscovery && settings?.homieDevice!=null && settings?.homieRunEvery) homieEnabled='<font color="green">' else homieEnabled='<font color="darkred">'
//if (settings?.HAStatestream && settings?.HAStatestreamTopic!=null && settings?.HARunEvery) HAEnabled='<font color="green">' else HAEnabled='<font color="darkred">'
if (settings?.homieDiscovery && settings?.homieDevice!=null) homieEnabled='<font color="green">' else homieEnabled='<font color="darkred">'
if (settings?.HAStatestream && settings?.HAStatestreamTopic!=null) HAEnabled='<font color="green">' else HAEnabled='<font color="darkred">'
if (settings?.CBusDiscovery && settings?.CBusTopic!=null) CBusEnabled='<font color="green">' else CBusEnabled='<font color="darkred">'
if (settings?.HAInDiscovery && settings?.HAInTopic!=null) HAInEnabled='<font color="green">' else HAInEnabled='<font color="darkred">'
if (settings?.SonoffDiscovery) SonoffEnabled='<font color="green">' else SonoffEnabled='<font color="darkred">'
if (settings?.ShellyDiscovery) ShellyEnabled='<font color="green">' else ShellyEnabled='<font color="darkred">'
}
numText=''
numhomieDevs=0 // number of homie devices
if (atomicState.onoffDevices != null) numhomieDevs+= atomicState.onoffDevices.size() // these are all homie devices
if (atomicState.dimDevices != null) numhomieDevs+= atomicState.dimDevices.size()
if (atomicState.colTempDevices != null) numhomieDevs+= atomicState.colTempDevices.size()
if (atomicState.RGBDevices != null) numhomieDevs+= atomicState.RGBDevices.size()
if (atomicState.sensorDevices != null) numhomieDevs+= atomicState.sensorDevices.size()
if (atomicState.lockDevices != null) numhomieDevs+= atomicState.lockDevices.size() //Casey
if (atomicState.buttonDevices != null) numhomieDevs+= atomicState.buttonDevices.size()
if (atomicState.varDevices != null) numhomieDevs+= atomicState.varDevices.size()
if (atomicState.homieUnknownDevices != null) numhomieDevs+= atomicState.homieUnknownDevices.size()
numHADevs=0 // number of HA devices
if (atomicState.HABinarySensorDevices != null) numHADevs+= atomicState.HABinarySensorDevices.size() // these are all homie devices
if (atomicState.HAClimateDevices != null) numHADevs+= atomicState.HAClimateDevices.size()
if (atomicState.HACoverDevices != null) numHADevs+= atomicState.HACoverDevices.size()
if (atomicState.HADeviceTrackerDevices != null) numHADevs+= atomicState.HADeviceTrackerDevices.size() //Casey
if (atomicState.HAGroupDevices != null) numHADevs+= atomicState.HAGroupDevices.size()
if (atomicState.HAInputBooleanDevices != null) numHADevs+= atomicState.HAInputBooleanDevices.size()
if (atomicState.HALightDevices != null) numHADevs+= atomicState.HALightDevices.size()
if (atomicState.HALockDevices != null) numHADevs+= atomicState.HALockDevices.size() //Casey
if (atomicState.HAPresenceDevices != null) numHADevs+= atomicState.HAPresenceDevices.size()
if (atomicState.HASensorDevices != null) numHADevs+= atomicState.HASensorDevices.size()
if (atomicState.HASwitchDevices != null) numHADevs+= atomicState.HASwitchDevices.size()
if (atomicState.HAUnknownDevices != null) numHADevs+= atomicState.HAUnknownDevices.size()
// TODO add more ? .. test for null redundant
numHAInDevs=0 // number of HAIn (discovery) devices
if (atomicState.HAInBinarySensorDevices != null) numHAInDevs+= atomicState.HAInBinarySensorDevices.size() // these are all homie devices
if (atomicState.HAInClimateDevices != null) numHAInDevs+= atomicState.HAInClimateDevices.size()
if (atomicState.HAInCoverDevices != null) numHAInDevs+= atomicState.HAInCoverDevices.size()
//if (atomicState.HAInDeviceTrackerDevices != null) numHAInDevs+= atomicState.HAInDeviceTrackerDevices.size() //Casey
if (atomicState.HAInGroups != null) numHAInDevs+= atomicState.HAInGroups.size()
//if (atomicState.HAInInputBooleanDevices != null) numHAInDevs+= atomicState.HAInInputBooleanDevices.size()