/
gen_config_yaml.py
executable file
·4552 lines (3965 loc) · 214 KB
/
gen_config_yaml.py
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
#!/usr/bin/env python3
#####################################################################
# This script won't update En-suite Room/Toilet configurations
# It is intentional to stop breaking automations in tenant room.
#####################################################################
from HA_Composite_Card_Lib.src.main import HA_Composite_Card_Lib
import re
import yaml
import os
import json
import copy
import argparse
#from HA_Composite_Card_Lib import HA_Composite_Card_Lib as self.hccl
#from translate import Translator
#global translator
#translator = Translator(to_lang="zh")
##################################################################
# Room Yaml Configurations
##################################################################
class RoomBase:
def __init__ (self, dashboard_type=None, dashboard_language='English'):
#cc = self.hccl()
#self.translation = translator.translate("This is a pen.")
#print (self.translation)
self.hccl = HA_Composite_Card_Lib()
# Create Empty JSON Database
self.reset_json_environment()
# Name and basic configs
self.get_room_config()
self.get_room_name_and_property()
self.info ("Generating " + self.room_name + " Yaml Package")
# Entities
self.get_time_entities()
self.get_motion_sensor_entities()
self.get_remote_entities()
self.get_wall_switches()
self.get_light_entities()
self.get_mirror_sensor_entities()
self.get_lighting_control_entities()
self.get_tv_entities()
self.get_time_setup()
self.get_cover_entities()
self.get_window_entities()
self.get_temperature_control_entities()
self.get_post_room_config()
# Populate entities into database
self.get_entity_declarations()
self.get_automation_declarations()
self.populate_entities_into_database()
# Remove disabled entities
self.remove_disabed_entities()
# Generate user control group including automations and other controls
self.get_automation_group_declarations()
# Get dashboard settings
self.dashboard_type = dashboard_type
#self.dashboard_language = dashboard_language
#print (self.dashboard_language)
self.getDashboardSettings()
self.dashboard_root = self.dashboard_default_root if dashboard_type == 'default' else \
'dashboard-tablet' if dashboard_type == 'tablet' else \
'dashboard-mobile' if dashboard_type == 'mobile' else \
'uninitialized_dashboard_root'
# Render config
self.writeConfig()
def get_room_config(self):
# Room configuration
self.room_entity = False
self.num_of_xiaomi_button = 0
self.num_of_lamps = 0
# Basic Enables
self.cfg_scene = False
self.cfg_occupancy = False
self.cfg_group_auto = False
self.cfg_motion_light = False
self.cfg_remote_light = False
self.cfg_temp_control = False
self.cfg_temp_calibration = False
# Adavanced Enables
self.cfg_scene_color_led = False
self.cfg_scene_color_lamp = False
self.cfg_custom_scene = False
self.cfg_led_only_scene = False
self.cfg_motion_bed_led = False
self.cfg_auto_curtain_ctl = False
self.cfg_adaptive_lighting = False
# Automatically loaded by settings later
self.cfg_flex_switch = False
self.room_entity = 'uninitialized_room_entity'
self.room_name = 'Uninitialized_room_name'
self.room_short_name = 'uninitialized_room_short_name'
def error(self, msg):
raise TypeError( "\n" +\
"###################################################################################\n" + \
msg + "\n" + \
"###################################################################################\n")
def warn(self, str):
warnings.warn("\n" +\
"-----------------------------------------------------\n" + \
str + "\n" + \
"-----------------------------------------------------\n")
def info(self, str):
print(str)
def get_time_entities(self):
self.end_of_sleep_time = '06:30:00'
self.start_of_sleep_time = '23:00:00'
def get_room_name_and_property(self):
self.room_entity = self.getIDFromName(self.room_name)
self.room_navi_path = self.room_entity.replace("_", "-")
self.automation_room_name = self.room_short_name + " "
self.room_type = 'bedroom' if (('_room' in self.room_entity) and ('living_room' != self.room_entity)) else \
'toilet' if '_toilet' in self.room_entity else \
'common_area'
self.west_face_windows = True if self.room_entity in ['en_suite_room', 'master_room', 'kitchen'] else False
def get_motion_sensor_entities(self):
self.motion_group = "group." + self.room_entity + "_motion_group"
self.occupancy_group = "group." + self.room_entity + "_occupancy_group"
# Motion sensor entities
self.bed_motion_sensors = ["binary_sensor." + self.room_entity + "_bed_motion_sensor_motion"] if self.room_type == 'bedroom' else []
self.non_bed_motion_sensors = []
self.all_motion_sensors = ["uninitialized_all_motion_sensors"]
self.entrance_motion_sensors = ["binary_sensor." + self.room_entity + "_entrance_motion_sensor_motion"] if self.room_type == 'bedroom' else \
[self.motion_group]
self.room_occupancy = "input_select." + self.room_entity + "_occupancy"
#self.force_stay_inside = 'input_boolean.' + self.room_entity + '_force_stay_insde'
self.sleep_time = 'input_boolean.master_room_sleep_time' if self.room_type != 'bedroom' else \
'input_boolean.' + self.room_entity + '_sleep_time'
self.automation_occupancy = { "alias":"ZOc-" + self.automation_room_name + "Occupancy Update" + "-" + self.room_name}
self.automation_occupancy['id'] = self.getIDFromAlias(self.automation_occupancy['alias'])
self.occupancy_state_duration= 4 # Minutes
self.occupancy_on_x_min_ratio_sensor = "unitialized_ratio_sensor"
self.occupancy_on_2x_min_ratio_sensor = "unitialized_ratio_sensor"
def get_remote_entities(self):
# Button (sensor) entities
self.xiaomi_buttons = []
for i in range(self.num_of_xiaomi_button):
self.xiaomi_buttons += ["sensor." + self.room_entity + "_button"]
if self.num_of_xiaomi_button != 1:
self.xiaomi_buttons[i] += "_" + str(i+1)
self.wall_buttons = ["sensor." + self.room_entity + "_wall_button",
"sensor." + self.room_entity + "_wall_button_2",] # sometimes the entity is duplicated and need _2 postfix
self.buttons = self.wall_buttons + self.xiaomi_buttons
self.curtain_buttons = []
self.six_key_buttons = []
self.four_key_buttons = []
self.eight_key_knob_buttons = []
def get_wall_switches(self):
self.wall_switches = []
self.decouple_wall_switches = []
self.raw_wall_switches = []
self.alias_wall_switches = []
def get_light_entities(self):
# Light/Switch entities
self.ceiling_lights = ["light." + self.room_entity + "_ceiling_light"]
self.lamps = []
for i in range(self.num_of_lamps):
self.lamps += ["light." + self.room_entity + "_lamp"]
if self.num_of_lamps != 1:
self.lamps[i] += "_" + str(i+1)
self.leds = []
self.lights = self.leds + self.lamps + self.ceiling_lights
self.light_group = 'group.' + self.room_entity + '_light_group'
# Adaptive Light settings
self.al_sleep_mode = 'switch.adaptive_lighting_sleep_mode_' + self.room_entity
self.al_adapt_brightness = 'switch.adaptive_lighting_adapt_brightness_' + self.room_entity
if self.cfg_adaptive_lighting == True:
self.al_light_list += \
[
{
"configured": True,
"name": self.room_name,
"lights": [],
"prefer_rgb_color": False,
"transition": 45,
"initial_transition": 1,
"interval": 90,
"min_brightness": 50,
"max_brightness": 100,
"min_color_temp": 2700,
"max_color_temp": 4000,
"sleep_brightness": 20,
"sleep_color_temp": 2200,
#"sunrise_time": None,
#"sunrise_offset": None,
#"sunset_time": None,
#"sunset_offset": None,
"take_over_control": True, # Does not work, after update brightness manually on lovelace, it still applies adaptive lighting at each interval
"autoreset_control_seconds": 86400,
"detect_non_ha_changes": False,
"only_once": False,
"adapt_only_on_bare_turn_on": True,
"separate_turn_on_commands": False, # Only setting it False will make "take_over_control" work. But adaptive on color transition may fail, such as living room lamps.
"send_split_delay": 500,
"adapt_delay": 0.5,
"intercept": True,
"multi_light_intercept": True,
"include_config_in_attributes": False
}
]
def get_mirror_sensor_entities(self):
self.mirror_sensors = []
def get_lighting_control_entities(self):
self.ceiling_light_control_when = {}
self.ceiling_light_control_when['Lights on in hot sunshine'] = "input_boolean.ceiling_light_control_" + "in_hot_sunshine_" + self.room_entity
self.ceiling_light_control_when['Lights on when bright outdoor'] = "input_boolean.ceiling_light_control_" + "when_bright_outdoor_" + self.room_entity
self.ceiling_light_control_when['Lights on when dark outdoor'] = "input_boolean.ceiling_light_control_" + "when_dark_outdoor_" + self.room_entity
self.lamp_control_when = {}
self.lamp_control_when['Lights on in hot sunshine'] = "input_boolean.lamp_control_" + "in_hot_sunshine_" + self.room_entity
self.lamp_control_when['Lights on when bright outdoor'] = "input_boolean.lamp_control_" + "when_bright_outdoor_" + self.room_entity
self.lamp_control_when['Lights on when dark outdoor'] = "input_boolean.lamp_control_" + "when_dark_outdoor_" + self.room_entity
self.led_control_when = {}
self.led_control_when['Lights on in hot sunshine'] = "input_boolean.led_control_" + "in_hot_sunshine_" + self.room_entity
self.led_control_when['Lights on when bright outdoor'] = "input_boolean.led_control_" + "when_bright_outdoor_" + self.room_entity
self.led_control_when['Lights on when dark outdoor'] = "input_boolean.led_control_" + "when_dark_outdoor_" + self.room_entity
self.curtain_control_when = {}
self.curtain_control_when['Lights off'] = "input_boolean.curtain_control_" + "when_left_" + self.room_entity
self.curtain_control_when['Lights on in hot sunshine'] = "input_boolean.curtain_control_" + "in_hot_sunshine_" + self.room_entity
self.curtain_control_when['Lights on when bright outdoor'] = "input_boolean.curtain_control_" + "when_bright_outdoor_" + self.room_entity
self.curtain_control_when['Lights on when dark outdoor'] = "input_boolean.curtain_control_" + "when_dark_outdoor_" + self.room_entity
def getPostfix(self, entity):
postfix = re.sub("^.*\.", "", entity)
# This regex does not seem to work to capture special characters
if re.search('[\./!"£$%^&*()]', postfix) != None:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Postfix " + postfix + " still have specical characters $./!\"£$%^&*()" + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
return postfix
def convertPostfixToName(self, postfix):
return re.sub("_", " ", postfix)
# match the beginning of the string or a space, followed by a non-space
def captilizeSentence(self, s):
return re.sub("(^|\s)(\S)", lambda m: m.group(1) + m.group(2).upper(), s)
def getName(self, entity):
postfix = self.getPostfix(entity)
name = self.convertPostfixToName(postfix)
name = self.captilizeSentence(name)
return name
def get_tv_entities(self):
self.tv_room_entity = None
self.tvs = []
self.tv_picture_mode = []
self.tv_soundbars = []
self.fire_tvs = []
def get_time_setup(self):
# Time setup
self.daytime_lights_off_timeout = "00:15:00"
self.nighttime_lights_off_timeout = "02:00:00" if self.room_type == "bedroom" else \
self.daytime_lights_off_timeout
self.daytime_start = "06:00:00"
self.afternoon_start = "13:00:00"
self.daytime_end = "21:00:00"
def get_cover_entities(self):
# Cover entities
self.curtains = []
self.aqara_shutter_blind = False
self.curtain_group = 'group.' + self.room_entity + '_curtain_group'
# Cover extra controls
# self.light_in_daytime_postfix = self.room_entity + "_light_in_daytime"
# self.light_in_daytime = "input_boolean." + self.room_entity + "_light_in_daytime"
# self.curtain_in_nighttime_postfix = self.room_entity + "_curtain_in_nighttime"
# self.curtain_in_nighttime = "input_boolean." + self.room_entity + "_curtain_in_nighttime"
def get_window_entities(self):
self.windows = []
self.window_group = 'group.' + self.room_entity + '_window_group'
def get_temperature_control_entities(self):
# Temperature sensor entities
self.outside_temperature = "sensor.met_office_cambridge_city_airport_temperature_3_hourly"
self.room_default_temperature = "input_number." + self.room_entity + "_default_temperature"
self.thermostat = "climate." + self.room_entity
self.thermostat_schedule = "switch.schedule_" + self.room_entity + "_temperature"
self.temperature_sensor = "sensor." + self.room_entity + "_temperature_sensor"
self.room_heating_override = "input_boolean." + self.room_entity + "_heating_override"
# Getting heating required sensor
self.template_list += [
{
"binary_sensor": [
{
"name": self.room_name + " Heating Required",
"state": '{% set climate = "climate.' + self.room_entity + '" %}' + \
'{% if states(climate) == "unavailable" %}' + \
'off' + \
'{% else %} ' + \
'{{ ((state_attr(climate, "temperature") > state_attr(climate, "current_temperature")) and (states(climate) != "off")) }}' + \
'{% endif %}'
}
],
"configured": self.cfg_temp_control
}
]
def get_post_room_config(self):
# Scene
self.room_scene_ctl = "input_select." + self.room_entity + "_scene"
# Scene automation internal variable
self.cur_scene = 'unintialized_cur_scene'
if self.cfg_group_auto:
self.room_auto_gen_automations = "group." + self.room_entity + "_auto_gen_automations"
#self.config = { nameof(self.room_entity ) : self.room_entity ,
# nameof(self.room_name ) : self.room_name ,
# nameof(self.room_type ) : self.room_type ,
# nameof(self.bed_motion_sensors ) : self.bed_motion_sensors ,
# nameof(self.entrance_motion_sensors ) : self.entrance_motion_sensors ,
# nameof(self.buttons ) : self.buttons ,
# nameof(self.xiaomi_buttons ) : self.xiaomi_buttons ,
# nameof(self.bed_leds ) : self.bed_leds ,
# nameof(self.leds ) : self.leds ,
# nameof(self.ceiling_lights ) : self.ceiling_lights ,
# nameof(self.lamps ) : self.lamps ,
# nameof(self.room_entity ) : self.room_entity ,
# nameof(self.room_entity ) : self.room_entity ,
# nameof(self.room_entity ) : self.room_entity ,
# nameof(self.room_entity ) : self.room_entity }
def reset_json_environment(self):
self.automations = []
self.entity_declarations = {}
self.input_select_dict = {}
self.sensor_list = []
self.group_dict = {}
self.input_boolean_dict = {}
self.script_dict = {}
self.switch_list = []
self.cover_list = []
self.template_list = []
self.binary_sensor_list = []
self.light_list = []
self.room_cards = []
self.views = []
self.dashboard_type = None
self.header_card_list = []
self.main_card_list = []
self.tail_card_list = []
self.al_light_list = []
def get_occupancy_ratio_sensor_config(self, x_minutes_multiple_str):
x_minutes_multiple = 1 if x_minutes_multiple_str == '1x' else \
2 if x_minutes_multiple_str == '2x' else \
0;
x_minutes_total = x_minutes_multiple * self.occupancy_state_duration
sensor_name = self.room_name + " Motion On Ratio For Last " + str(x_minutes_total) +" Minutes"
if x_minutes_multiple == 1:
self.occupancy_on_x_min_ratio_sensor = "sensor." + self.getIDFromName(sensor_name)
elif x_minutes_multiple == 2:
self.occupancy_on_2x_min_ratio_sensor = "sensor." + self.getIDFromName(sensor_name)
#print (self.room_name + " occupancy_on_x_min_ratio_sensor is " + self.occupancy_on_x_min_ratio_sensor)
ratio_sensor_config = {
"platform": "history_stats",
"name": sensor_name,
"entity_id": self.motion_group,
"state": "on",
"type": "ratio",
"duration": {
"minutes": str(x_minutes_total)
},
"end": "{{ (now() | as_timestamp) | as_datetime | as_local }}",
"configured": self.cfg_occupancy
}
return ratio_sensor_config
# def addEntityCard(self, entity, entity_name=None, entity_name_translation=None,
# card_name=None, card_type=None,card_icon=None,card_icon_color=None,double_tab_action=None, card_group=None):
#
#
# self.room_cards += [self.hccl.getEntityCard(entity=entity,
# entity_name=entity_name,
# entity_name_translation=entity_name_translation,
# card_name=card_name,
# card_type=card_type,
# card_icon=card_icon,
# card_icon_color=card_icon_color,
# double_tab_action=double_tab_action
# )]
def add_mac_device(self, mac, name, comment, model, postfix=None, integration='Xiaomi Gateway 3', flex_switch=None, power_on_threshold=None, switch_rename_dir=None, light_wall_switch=True, belong_to_group=None):
# Check if inputs are legal
group = belong_to_group
if belong_to_group != None and type(belong_to_group) is not list:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Model " + model + " belong_to_group is not a list. Name = " + name + name_postfix + ', MAC Address:' + mac + ", Integration " + integration + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
name_postfix = ' ' + postfix if postfix != None else ''
# 0 padding mac address to 64-bit for Zigbee device
if re.match("^0x[A-Fa-f0-9]*$", mac):
mac_in_decimal = int(mac, 16) # convert hexadecimal to decimal
mac = "0x{:0>16x}".format(mac_in_decimal) # format hexadecimal with 0 padding to 16 characters (64-bit)
###################################################################################################
# Wall Switches
#
# Aqara D1 Wall Switch (With Neutral, Triple Rocker) QBKG26LM ZigbeeID: ["lumi.switch.n3acn3"]
# Aqara D1 Wall Switch (With Neutral, Double Rocker) QBKG24LM ZigbeeID: ["lumi.switch.b2nacn02"]
# Aqara D1 Wall Switch (With Neutral, Single Rocker) QBKG23LM ZigbeeID: ["lumi.switch.b1nacn02"]
# Aqara Single Key Wired Wall Switch Without Neutral Wire QBKG04LM
###################################################################################################
if((model == "Aqara D1 Wall Switch (With Neutral, Triple Rocker)" ) or \
(model == "Aqara D1 Wall Switch (With Neutral, Double Rocker)" ) or \
(model == "Aqara D1 Wall Switch (With Neutral, Single Rocker)" ) or \
(model == "Aqara Single Key Wired Wall Switch Without Neutral Wire")):
key_num = 0
if model == "Aqara D1 Wall Switch (With Neutral, Triple Rocker)":
key_num = 3
elif model == "Aqara D1 Wall Switch (With Neutral, Double Rocker)":
key_num = 2
elif model == "Aqara D1 Wall Switch (With Neutral, Single Rocker)":
key_num = 1
elif model == "Aqara Single Key Wired Wall Switch Without Neutral Wire":
key_num = 1
else:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Model " + model + " does not have a key_num. Name = " + name + name_postfix + ', MAC Address:' + mac + ", Integration " + integration + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
# Wall Switches Per Each Physical Wall Switch
for index in range(1,key_num+1):
if integration == 'Xiaomi Gateway 3':
# single switch is with different postfix
i = '' if key_num == 1 else index
alias_switch_name = name + " Wall Switch " + str(i) + name_postfix
raw_switch_entity = "switch." + mac + ('_switch' if key_num == 1 else '_channel_' + str(i))
raw_decouple_switch_entity = "switch." + mac + '_wireless' + ('' if key_num == 1 else '_' + str(i))
elif integration == 'Z2M':
i = index
# Hack Z2M renamed devices to standard name
postfix = '' if key_num == 1 else \
'_left' if i == 1 else \
'_center' if i == 2 and key_num == 3 else \
'_right' if i == 3 and key_num == 3 or i == 2 and key_num == 2 else ''
alias_switch_name = name + " Wall Switch " + str(i) + name_postfix
raw_switch_entity = "switch." + self.room_entity + '_wall_switch' + postfix
raw_decouple_switch_entity = 'unintialized_raw_decouple_switch_entity'
else:
raise TypeError("\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Model " + model + " does not support this integration. Name = " + name + name_postfix + ', MAC Address:' + mac + ", Integration " + integration + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
self.switch_list += [
{
"platform": "group",
"name": alias_switch_name,
"entities": raw_switch_entity,
"configured": True
}
]
#print (alias_switch_name + " " + str(index))
# Rename switches based on switch rename
if switch_rename_dir is not None and index in switch_rename_dir:
self.switch_list += [
{
"platform": "group",
"name": switch_rename_dir[index],
"entities": raw_switch_entity,
"configured": True
}
]
# added wall switches
self.wall_switches += [raw_switch_entity]
self.decouple_wall_switches += [raw_decouple_switch_entity] if raw_decouple_switch_entity != 'unintialized_raw_decouple_switch_entity' else []
# added Yeelight Flex automation
if flex_switch != None:
# Disable wall button single automation
self.cfg_flex_switch = True
# Generate flex automation instead
if flex_switch is True:
self.gen_flex_wall_switch_automations(flex_wall_switch_index=1, flex_wall_switch_entity=raw_switch_entity)
else:
for flex_switch_index_local in flex_switch:
if flex_switch_index_local == i:
self.gen_flex_wall_switch_automations(flex_wall_switch_index=i, flex_wall_switch_entity=raw_switch_entity)
# Wall Buttons
# Hack Z2M integration and no need to rename the devices for now
if integration == 'Xiaomi Gateway 3':
self.template_list += [
{
"sensor": [
{
"name": name + " Wall Button" + name_postfix,
# Entity id starts with 0 so have to use a different format
# https://community.home-assistant.io/t/error-in-template-i-am-missing-something/92464/3
"state": '{{states.sensor["' + mac + '_action"].state}}'
}
],
"configured": True
}
]
###################################################################################################
# Contacts
#
# Aqara Door & Window Sensor, MCCGQ11LM ZigbeeID: ["lumi.sensor_magnet.aq2"]
# Mijia2 Contact, MCCGQ02HL
###################################################################################################
elif((model == "Aqara Door & Window Sensor" and integration == 'Xiaomi Gateway 3') or \
(model == "Mijia2 Contact" and integration == 'Xiaomi Gateway 3')):
#entity_name = name + name_postfix
#if group != None:
# group += ["binary_sensor." + self.getIDFromName(entity_name)]
self.binary_sensor_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "binary_sensor." + mac + '_contact',
"configured": True
}
]
elif((model == "Aqara Pressure Sensor") or \
(model == "Mijia2 Pressure Sensor")):
self.template_list += [
{
"binary_sensor": [
{
"name": name + " Pressure Sensor" + name_postfix,
"state": '{% set state = states.binary_sensor["' + mac + '_contact"].state %} {% if state == "on"%} off {% elif state == "off"%} on {% else%} {{state}} {% endif %}'
}
],
"configured": True
}
]
###################################################################################################
# Buttons
# Aqara Wireless Switch, WXKG11LM ZigbeeID: ["lumi.sensor_switch.aq2"] action_list:[]
# MiJia Wireless Switch, WXKG01LM ZigbeeID: ["lumi.sensor_switch"] action_list:['single', 'double', 'triple', 'quadruple', 'many','release', 'hold']
# MiJia Wireless Switch 2, ble XMWXKG01LM action_list:['single', 'double', 'hold']
# Aqara Opple switch 3 bands, WXCJKG13LM, ZigbeeID: ["lumi.remote.b686opcn01"] action_list:['button_1_single', 'button_2_single', 'button_3_single', 'button_4_single', 'button_5_single', 'button_6_single'] - more buttons for double/hold etc
###################################################################################################
elif((model == "Aqara Wireless Switch") or \
(model == "MiJia Wireless Switch") or \
(model == "Aqara Opple switch 3 bands") or \
(model == "MiJia Wireless Switch 2")):
if model == "Aqara Opple switch 3 bands" and integration == 'Z2M':
mac = mac + "_" + mac
mac_postfix = '_action'
else:
mac_postfix = '_button_action' if integration == 'Z2M' else \
'_action'
self.template_list += [
{
"sensor": [
{
"name": name + " Button" + name_postfix,
"state": '{{states.sensor["' + mac + mac_postfix + '"].state}}'
}
],
"configured": True
}
]
###################################################################################################
# Light Sensors
# Xiaomi Light Detection Sensor GZCGQ01LM ZigbeeID: ["lumi.sen_ill.mgl01"]
# Xiaomi Light Detection Sensor To Mirror Sensor GZCGQ01LM ZigbeeID: ["lumi.sen_ill.mgl01"]
###################################################################################################
elif((model == "Xiaomi Light Detection Sensor")):
self.template_list += [
{
"sensor": [
{
"name": name + " Light Sensor" + name_postfix,
"state": '{{states.sensor["' + mac + '_illuminance"].state}}'
}
],
"configured": True
}
]
elif((model == "Xiaomi Light Detection Sensor To Mirror Sensor")):
self.template_list += [
{
"binary_sensor": [
{
"name": name + name_postfix,
"state": '{{states.sensor["' + mac + '_illuminance"].state |int > 500 }}'
}
],
"configured": True
}
]
###################################################################################################
# Motion Sensors, Occupancy Sensor
#
# Aqara Motion and Illuminance Sensor, RTCGQ11LM ZigbeeID: ["lumi.sensor_motion.aq2"]
# MiJia Human Body Movement Sensor, RTCGQ01LM
# Mijia Motion Sensor 2, RTCGQ02LM
# Ziqing Occupancy Sensor, Mesh model: "mesh IZQ-24"
###################################################################################################
elif((model == "Aqara Motion and Illuminance Sensor" ) or \
(model == "MiJia Human Body Movement Sensor" ) or \
(model == "Mijia Motion Sensor 2" and integration == 'Xiaomi Gateway 3')):
motion_entity_postfix = '_occupancy' if integration == 'Z2M' else \
'_motion' # if integration == 'Xiaomi Gateway 3'
self.binary_sensor_list += [
{
"platform": "group",
"name": name + " Motion Sensor Motion" + name_postfix,
"entities": "binary_sensor." + mac + motion_entity_postfix,
"configured": True
}
]
light_entity_postfix = '_illuminance_lux' if integration == 'Z2M' else \
'_illuminance' # if integration == 'Xiaomi Gateway 3'
self.template_list += [
{
"sensor": [
{
"name": name + " Motion Sensor Light" + name_postfix,
"unit_of_measurement": "lx",
"state": '{{states.sensor["' + mac + light_entity_postfix + '"].state}}'
}
],
"configured": True
}
]
self.template_list += [
{
"sensor": [
{
"name": name + " Motion Sensor Battery" + name_postfix,
"unit_of_measurement": "%",
"state": '{{states.sensor["' + mac + '_battery"].state}}'
}
],
"configured": True
}
]
elif model == "Ziqing Occupancy Sensor" and integration == 'Xiaomi Gateway 3':
self.binary_sensor_list += [
{
"platform": "group",
"name": name + " Occupancy Sensor Occupancy" + name_postfix,
"entities": "binary_sensor." + mac + '_occupancy',
"configured": True
}
]
self.template_list += [
{
"sensor": [
{
"name": name + " Occupancy Sensor Light" + name_postfix,
"unit_of_measurement": "lx",
"state": '{{states.sensor["' + mac + '_illuminance"].state}}'
}
],
"configured": True
}
]
###################################################################################################
# Generic Binary Sensor
###################################################################################################
elif(model == "Generic Binary Sensor"):
self.binary_sensor_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "binary_sensor." + mac,
"configured": True
}
]
###################################################################################################
# Temperature Sensor in Xiaomi Gateway 3
# Qingping Temperature Sensor, id: "ble LYWSDCGQ/01ZM"
# Qingping Lite Temperature Sensor, id: "ble CGDK2"
# Mijia2 Temperature Sensor, id: "ble LYWSD03MMC"
#
# Temperature Sensor in Passive BLE
# "Mijia2 Temperature Clock", id:"ble LYWSD02MMC"
###################################################################################################
elif((model == "Qingping Temperature Sensor" and integration == 'Xiaomi Gateway 3') or \
(model == "Mijia2 Temperature Sensor" and integration == 'Xiaomi Gateway 3') or \
(model == "Qingping Lite Temperature Sensor" and integration == 'Xiaomi Gateway 3')):
self.template_list += [
{
"sensor": [
{
"name": name + " Temperature Sensor" + name_postfix,
"unit_of_measurement": "°C",
"state": '{{states.sensor["' + mac + '_temperature"].state | float | round(1)}}'
}
],
"configured": True
}
]
elif model in [ "Mijia2 Temperature Clock" ] and integration == 'Passive BLE Monitor':
self.template_list += [
{
"sensor": [
{
"name": name + " Temperature Sensor" + name_postfix,
"unit_of_measurement": "°C",
"state": '{{states.sensor["temperature_humidity_sensor_' + mac + '_temperature"].state | float | round(1)}}'
}
],
"configured": True
}
]
elif((model in ["Aqara B1 curtain motor", "Aqara roller shade motor"] and integration == 'Xiaomi Gateway 3')):
self.cover_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "cover." + mac + "_motor",
"configured": True
}
]
self.template_list += [
{
"sensor": [
{
"name": name + " Curtain Battery" + name_postfix,
"unit_of_measurement": "%",
"state": '{{states.sensor["' + mac + '_battery"].state}}'
}
],
"configured": True
}
]
self.aqara_shutter_blind = True if model in ["Aqara roller shade motor"] else False
###################################################################################################
# Generic lights
# Mijia BLE Lights, MJDP09YL
###################################################################################################
elif((model == "Generic Lights" ) or \
(model == "TRADFRI LED Bulb GU10 400 Lumen, Dimmable, White spectrum" and integration == 'Z2M')):
self.light_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "light." + mac,
"configured": True
}
]
elif model == "Mijia BLE Lights":
self.light_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "light." + mac + "_light",
"configured": True
}
]
###################################################################################################
# Generic switches
# Mi Power Plug ZigBee, ZNCZ02LM ZigbeeID: ["lumi.plug"]
###################################################################################################
elif((model == "Generic Switches" ) or \
(model == "Mi Power Plug ZigBee" )):
switch_entity_postfix = '_plug' if model == "Mi Power Plug ZigBee" else ''
self.switch_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "switch." + mac + switch_entity_postfix,
"configured": True
}
]
elif(model == "Generic Power Measurement Switch"):
if power_on_threshold == None:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Model " + model + " needs power_on_threshold to be dinfed. Name = " + name + name_postfix + ', MAC Address:' + mac + ", Integration " + integration + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
self.template_list += [
{
"binary_sensor": [
{
"name": name + name_postfix,
# round(1) means to round to 0.1
"state": '{% if (states.sensor["' + mac + '"].state) | float | round(1) > ' + str(power_on_threshold) + ' %} on {% else %} off {% endif %}'
}
],
"configured": True
}
]
###################################################################################################
# Generic covers/Generic curtains
#
###################################################################################################
elif model == "Generic Curtains":
self.cover_list += [
{
"platform": "group",
"name": name + name_postfix,
"entities": "cover." + mac,
"configured": True
}
]
###################################################################################################
# Model not found, throw an error
###################################################################################################
else:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Model " + model + " is not supported. Name = " + name + name_postfix + ', MAC Address:' + mac + ", Integration " + integration + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
def add_average_temperature_sensor(self, sensor_1, sensor_2, name):
self.template_list += [
{
"sensor": [
{
"name": name,
"unit_of_measurement": "°C",
"state": "" + \
"{% if states('"+sensor_1 +"') != 'unavailable' and states('" + sensor_2 + "') == 'unavailable' %}" + \
"{{states('"+sensor_1 +"')}}" + \
"{% elif states('"+sensor_1 +"') == 'unavailable' and states('" + sensor_2 + "') != 'unavailable' %}" + \
"{{states('"+sensor_2 +"')}}" + \
"{% else %}" + \
"{% set sensor_1 = states('"+sensor_1 +"') | float | round(1) %}" + \
"{% set sensor_2 = states('"+sensor_2 +"') | float | round(1) %}" + \
"{% if sensor_1 < 1.0 or sensor_2 < 1.0 or (sensor_1 + sensor_2) < 1.0 %}" + \
"22.0" + \
"{% else %}" + \
"{{ ((sensor_1 + sensor_2) / 2) | round(1) }}" + \
"{% endif %}" + \
"{% endif %}"
}
],
"configured": True
}
]
def get_entity_declarations(self):
self.get_script_dict()
self.input_select_dict |= {
self.getPostfix(self.room_scene_ctl) : {
"name" : self.getName(self.room_scene_ctl),
"configured": self.cfg_scene,
"options": \
['Idle']
+ (["Hue" ] if self.cfg_scene_color_led or self.cfg_scene_color_lamp else [])
+ (["Sleep Mode",
"Night Mode",
"Dark Night Mode"] if self.cfg_custom_scene else[])
+ (["Lamp LED White" ] if len(self.lamps) > 0 else [])
+ (["LED White" ] if len(self.leds) > 0 else [])
#+ (["Ceiling Light White with Curtain Open"] if len(self.curtains) > 0 else [])
+ [ "All White",
"Ceiling Light White",
"All Off"
]
#+ ["Lights on in hot sunshine",
# "Lights on when bright outdoor",
# "Lights on when dark outdoor"
# ]
}
}
self.input_select_dict |= {
self.getPostfix(self.room_occupancy) : {
"name" : self.getName(self.room_occupancy),
"options":[
"Outside",
"Just Entered",
"Stayed Inside",
"In Sleep"
],
"configured": self.cfg_occupancy
}
}
self.input_boolean_dict |= {
#self.getPostfix(self.force_stay_inside) : {
# "name" : self.getName(self.force_stay_inside),
# "initial": "off",
# "configured": self.cfg_occupancy
#},
self.getPostfix(self.sleep_time) : {
"name" : self.getName(self.sleep_time),
"initial": "off",
"configured": self.cfg_occupancy
},
self.getPostfix(self.room_heating_override) : {
"name" : self.getName(self.room_heating_override),
"initial": "off",
"configured": self.cfg_temp_control
}