/
gen_config_yaml.py
executable file
·4315 lines (3733 loc) · 181 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
#####################################################################
# This script won't update En-suite Room/Toilet configurations
# It is intentional to stop breaking automations in tenant room.
#####################################################################
import re
import yaml
import os
import json
import copy
import argparse
##################################################################
# Room Yaml Configurations
##################################################################
class RoomBase:
def __init__ (self, dashboard_type=None):
# Create Empty JSON Database
self.reset_json_environment()
# Name and basic configs
self.get_room_config()
self.get_room_name_and_property()
print ("Generating " + self.room_name + " Yaml Package")
# Entities
self.get_motion_sensor_entities()
self.get_remote_entities()
self.get_wall_switches()
self.get_light_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_entnties_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.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
self.cfg_tv = 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
# 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 get_room_name_and_property(self):
self.room_entity = self.getIDFromName(self.room_name)
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.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"]
self.buttons = self.wall_buttons + self.xiaomi_buttons
def get_wall_switches(self):
self.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'
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):
if self.cfg_tv:
# TV entities
self.tv_room_entity = self.room_entity
self.tvs = ["media_player." + self.tv_room_entity + "_tv"]
self.fire_tvs = ["media_player." + self.tv_room_entity + "_fire_tv"]
else:
self.tv_room_entity = None
self.tvs = []
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 (not 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 = []
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
# for lovelace single entity:
# entity
# entity_name
# entity_name_translation
# card name -> can be inferred from entity name -> can be inferred from entity
# card type -> can be inferred from entity type
# card icon -> can be inferred from device icon if added
# card icon color
# double tab action
def getEntityCard(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,simple=None,
secondary_info=None):
card = {}
# Set up card_type based on entity_type
if card_type is None:
# remove entity name to get entity type
# "light" = Remove ".living_room_ceiling_light" from "light.living_room_ceiling_light"
entity_type = re.sub("\.(\w+)$", "", entity)
if entity_type in ['light', 'cover', 'climate']:
card_type = 'custom:mushroom-' + entity_type + '-card'
elif entity_type in ['media_player']:
card_type = 'custom:mushroom-' + 'media-player' + '-card'
if entity_type in ['binary_sensor', 'switch', 'input_boolean']:
card_type = 'custom:mushroom-' + 'entity' + '-card'
elif entity_type in ['group']:
card_type = 'custom:group-card'
elif entity_type in ['sensor']:
card_type = 'sensor'
elif entity_type in ['input_select']:
card_type = 'custom:mushroom-select-card'
elif entity_type in ['input_number', 'number']:
card_type = 'custom:mushroom-number-card'
if entity_name is None:
entity_name = self.getName(entity)
if card_name is None:
# Remove room name from entity name to get card name
# "Ceiling Light" = Remove "Living Room" from "Living Room Ceiling Light"
card_name = re.sub(self.room_name, "", entity_name)
#if entity_name_translation != None:
# entity_name_translation =
card_name = '' if card_name is None else card_name
card_icon = '' if card_icon is None else card_icon
# light
if card_type == 'custom:mushroom-light-card':
card = {
"type": card_type,
"fill_container": True,
"use_light_color": False,
"show_brightness_control": True,
"show_color_control": False,
"show_color_temp_control": True,
"collapsible_controls": False,
"name": card_name,
"icon": card_icon,
"entity": entity
} | self.getCardMod('ios16_toggle', card_type=card_type, color='ios_yellow')
# cover
elif card_type == 'custom:mushroom-cover-card':
card = {
"type": card_type,
"fill_container": True,
"tap_action":{
"action": "toggle"
},
"double_tap_action":{
"action": "more-info"
},
"hold_action":{
"action": "more-info"
},
"show_position_control": True,
"show_buttons_control": True,
"name": card_name,
"icon": card_icon,
"entity": entity
}
# climate
elif card_type == 'custom:mushroom-climate-card':
card = {
"type": card_type,
"show_temperature_control": True,
"collapsible_controls": False,
"name": card_name,
"icon": card_icon,
"entity": entity
}
# media_player card
# tap - toggle/navigate/more-info
elif card_type == 'custom:mushroom-media-player-card':
card = {
"type": card_type,
"fill_container": True,
"tap_action":{
"action": "more-info"
},
"volume_controls":[
# "volume_mute",
"volume_set",
"volume_buttons"
],
"show_volume_level": False,
"name": card_name,
"icon": card_icon,
"entity": entity
}
# group card
elif card_type == 'custom:group-card':
card = {
"type": card_type,
"card":{
"type": "entities",
"title": card_name
},
"group": entity
}
# sensor
elif card_type == 'sensor':
if simple is True:
card = {
"type": card_type,
"graph": "line",
"name": card_name,
"icon": card_icon,
"entity": entity
}
else: # complex minigraph temperature sensor card from
# https://bbs.hassbian.com/forum.php?mod=redirect&goto=findpost&ptid=22509&pid=550976
card = {
"type": "custom:vertical-stack-in-card",
"cards": [
{
"type": "custom:mushroom-template-card",
"entity": entity,
"primary": card_name,
"secondary": "{{ states('" + entity + "') | round(0) }}\u00b0C\n",
"icon": "mdi:thermometer",
"icon_color": "{% set value = states('" + entity + "') | int %}\n{% if value < 18 %}\n blue\n{% elif value < 28 %}\n light-green\n{% elif value < 40 %}\n red\n{% else %}\n green\n{% endif %}",
"tap_action": {
"action": "more-info"
}
} | self.getCardModColor('transparent'),
{
"type": "custom:layout-card",
"layout_type": "masonry",
"layout": {
"width": 150,
"max_cols": 1,
"height": "auto",
"padding": "0px",
"card_margin": "var(--masonry-view-card-margin, -10px 8px 15px)"
},
"cards": [
{
"type": "custom:mini-graph-card",
"tap_action": {
"action": "more-info"
},
"entities": [
{
"entity": entity,
"name": "Temperature"
}
],
"color_thresholds": [
{
"value": -10,
"color": "#0000ff"
},
{
"value": 18,
"color": "#0000ff"
},
{
"value": 18.1,
"color": "#00FF00"
},
{
"value": 27,
"color": "#00FF00"
},
{
"value": 27.1,
"color": "#FF0000"
},
{
"value": 40,
"color": "#FF0000"
}
],
"hours_to_show": 24,
"line_width": 3,
"animate": True,
"show": {
"name": False,
"icon": False,
"state": False,
"legend": False,
"fill": "fade"
},
"card_mod": {
"style": "ha-card {\n background: none;\n box-shadow: none;\n --ha-card-border-width: 0;\n}"
}
}
]
}
]
}
# entity card
# binary_sensor - tap to more-info
# switch - tap to toggle
# input boolean
elif card_type == 'custom:mushroom-entity-card':
card = {
"type": card_type,
"fill_container": True,
"tap_action":{
"action": "toggle"
},
"name": card_name,
"icon": card_icon,
"entity": entity
}
# entities card
# input_timer
elif card_type == 'entities':
card = {
"type": card_type,
"entities":[
{ "name": card_name,
"entity": entity}
]
}
# schduler card
elif card_type == 'custom:scheduler-card':
card = {
"type": card_type,
"include": entity,
"exclude": [],
"title": True,
"discover_existing": True,
"time_step": 30
}
# timer card
elif card_type == 'custom:flipdown-timer-card':
card = {
"type": card_type,
"show_hour": True,
"show_title": True,
"theme": 'dark',
"styles": {
"rotor": {
"width": "50px",
"height": "80px"},
"button": {
"width": "100px",
"location": "bottom"}
},
"name": card_name,
"icon": card_icon,
"entity": entity
}
# input_number
# number
elif card_type == 'custom:mushroom-number-card':
card = {
"type": card_type,
"fill_container": True,
"tap_action":{
"action": "more-info"
},
"display_mode": "buttons",
"name": card_name,
"icon": 'mdi:counter' if card_icon is None else card_icon,
"entity": entity
}
# input_select
elif card_type == 'custom:mushroom-select-card':
card = {
"type": card_type,
"fill_container": True,
"tap_action":{
"action": "more-info"
},
"icon": "mdi:brightness-4" if card_icon is None else card_icon,
"secondary_info": state if secondary_info is None else secondary_info,
"name": card_name,
"icon": card_icon,
"entity": entity
}
return card
def getCardMod(self, style='background_color_select', card_type=None, color=None, support_dark_mode=True):
css_variable = ""
if style == 'background_color_select':
css_variable += ":host { --ha-card-background:" + self.getColor(color) + ";}\n"
elif style == 'ios16_toggle':
entity_is_on_condition = " (states(config.entity) in ['on']) " + \
("and (states('sun.sun') != 'below_horizon')" if support_dark_mode == True else '')
# Make card background white if on, dark if off
css_variable += (":host {\n"
"--ha-card-background: {% if" + entity_is_on_condition + "%} " + self.getColor("less_transparent_white") + " {% else %} " + self.getColor("more_transparent_grey") + " {% endif %};\n"
"--primary-text-color: {% if" + entity_is_on_condition + "%} black {% else %} white {% endif %};\n"
"--secondary-text-color : {% if" + entity_is_on_condition + "%} " + self.getColor("dark_grey") + "{% else %} " + self.getColor("light_grey") + " {% endif %};\n"
";}\n")
# Make icon a bit larger, similar to ios 16
css_variable += ("ha-card > mushroom-card > mushroom-state-item > mushroom-shape-icon > ha-state-icon {\n"
"--mdc-icon-size: 0.7em;"
";}\n")
# Make icon inner white if on, outter dark if off
if card_type == 'custom:mushroom-light-card':
css_variable += "ha-card > mushroom-card > mushroom-state-item > mushroom-shape-icon {" + " \n" + \
"--icon-color-disabled: " + self.getColor(color) + ";\n" + \
"--shape-color-disabled: " + self.getColor("more_transparent_grey") + ";\n" + \
"--icon-color: " + self.getColor("white") + ";\n" + \
"--shape-color: {% if state_attr(config.entity, 'color_mode') == 'color_temp' %}" + " \n" + \
self.getColor(color) + " \n" + \
"{% else %}" + " \n" + \
" rgb{{state_attr(config.entity, 'rgb_color')}}" + " \n" + \
"{% endif%}" + ";\n" + \
"}\n"
else:
css_variable += ( "ha-card > mushroom-card > mushroom-state-item > mushroom-shape-icon {\n"
"--icon-color-disabled: " + self.getColor(color) + ";\n"
"--shape-color-disabled: " + self.getColor("more_transparent_grey") + ";\n"
"--icon-color: " + self.getColor("white") + ";\n"
"--shape-color: " + self.getColor(color) + ";\n"
"}\n")
# Make brightness slider color same as the light color if it is in RGB, otherwise use ios_yellow
if card_type == 'custom:mushroom-light-card':
css_variable += "ha-card > mushroom-card > div > mushroom-light-brightness-control {" + " \n" + \
"--slider-color: {% if state_attr(config.entity, 'color_mode') == 'color_temp' %}" + " \n" + \
self.getColor(color) + " \n" + \
"{% else %}" + " \n" + \
" rgb{{state_attr(config.entity, 'rgb_color')}}" + " \n" + \
"{% endif%}" + ";\n" + \
"--slider-bg-color: {% if state_attr(config.entity, 'color_mode') == 'color_temp' %}" + " \n" + \
"{{ '" + self.getColor(color) + "' | regex_replace(',([\d\.])+\)$', ',0.2)') }}" + " \n" + \
"{% else %}" + " \n" + \
" rgba{{(state_attr(config.entity, 'rgb_color')|string)[0:-1]}}, 0.2)" + " \n" + \
"{% endif%}" + ";\n" + \
"}\n"
# Make bottom buttons background more visiable in white card
# and default in dark card
if card_type == 'custom:mushroom-light-card':
for index in [2,3]:
css_variable += "" + \
"ha-card > mushroom-card > div > mushroom-button:nth-child(" + str(index) + ") {"
css_variable += "" + \
" --bg-color: {% if " + entity_is_on_condition + " %} " + self.getColor("less_transparent_white") + " {% else %} rgba(var(--rgb-primary-text-color), 0.05)" + " {% endif %};" + \
"}\n"
else:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"getCardMod does not support style = " + style + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
return {
"card_mod": {
"style": css_variable
}
}
def getColor(self, color):
# The last digit of rgba represents opacity and the value of it is between 0.0 and 1.0
# !! always needs to follow the rgba format to maintain a compatibility
if color == 'transparent':
return "rgba(245, 245, 245, 0)"
elif color == 'more_transparent_grey':
return "rgba(10, 10, 10, 0.4)"
elif color == 'less_transparent_grey':
return "rgba(10, 10, 10, 0.7)"
elif color == 'most_transparent_white':
return "rgba(245, 245, 245, 0.1)"
elif color == 'more_transparent_white':
return "rgba(245, 245, 245, 0.3)"
elif color == 'less_transparent_white':
return "rgba(245, 245, 245, 0.9)"
elif color == 'dark_grey':
return "rgba(100, 100, 100, 1)"
elif color == 'light_grey':
return "rgba(220, 220, 220, 1)"
elif color == 'ios_yellow':
return "rgba(253,204,0,1)"
else:
return color
def getCardModColor(self, color):
return self.getCardMod('background_color_select', color=color)
# 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.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 mac.startswith('0x'):
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 i in range(1,key_num+1):
if integration == 'Xiaomi Gateway 3':
# single switch is with different postfix
i = '' if key_num == 1 else i
alias_switch_name = name + " Wall Switch " + str(i) + name_postfix
raw_switch_entity = "switch." + mac + ('_switch' if key_num == 1 else '_channel_' + str(i))
elif integration == 'Z2M':
# 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
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
}
]
# Rename switches based on switch rename
if switch_rename_dir is not None and i in switch_rename_dir:
#print (switch_rename_dir[i])
self.switch_list += [
{
"platform": "group",
"name": switch_rename_dir[i],
"entities": raw_switch_entity,
"configured": True
}
]
# added wall switches
if light_wall_switch == True:
self.wall_switches += [raw_switch_entity]
# 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"]
# MiJia Wireless Switch, WXKG01LM ZigbeeID: ["lumi.sensor_switch"]
# MiJia Wireless Switch 2, ble XMWXKG01LM
###################################################################################################
elif((model == "Aqara Wireless Switch") or \
(model == "MiJia Wireless Switch") or \
(model == "MiJia Wireless Switch 2")):
self.template_list += [
{
"sensor": [
{
"name": name + " Button" + name_postfix,
"state": '{{states.sensor["' + mac + '_action"].state}}'
}
],
"configured": True
}
]
###################################################################################################
# Light Sensors
# Xiaomi Light Detection 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
}
]
###################################################################################################
# Motion Sensors, Occupancy Sensor
#
# Aqara Motion and Illuminance Sensor, RTCGQ11LM ZigbeeID: ["lumi.sensor_motion.aq2"]
# MiJia Human Body Movement Sensor, RTCGQ01LM
# Mijia2 Motion Sensor, 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 == "Mijia2 Motion Sensor" 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'