/
gen_config_yaml.py
executable file
·2474 lines (2153 loc) · 95.8 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
import re
import yaml
import os
import json
import copy
import argparse
##################################################################
# Room Yaml Configurations
##################################################################
class RoomBase:
def __init__ (self):
# Basic Parameter
self.get_room_config()
self.get_room_name_and_property()
self.get_motion_sensor_entities()
self.get_remote_entities()
self.get_light_entities()
self.get_lighting_control_entities()
self.get_tv_entities()
self.get_time_setup()
self.get_cover_entities()
self.get_temperature_control_entities()
self.get_post_room_config()
# Render json database
self.reset_json_environment()
# 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.getDashboardSettings()
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_colour_led = False
self.cfg_scene_colour_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.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):
# 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.room_motion_sensor = "group." + self.room_entity + "_motion"
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.room_motion_sensor]
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_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.room_lights_and_tvs = 'group.' + self.room_entity + '_lights_and_tvs'
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 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):
return re.sub("^.*\.", "", entity)
def convertPostfixToName(self, postfix):
return re.sub("_", " ", postfix)
def getName(self, entity):
postfix = self.getPostfix(entity)
name = self.convertPostfixToName(postfix)
name = self.captilizeSentence(name)
return name
# 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 get_tv_entities(self):
if self.cfg_tv:
# TV entities
self.tv_room_entity = 'portable' if self.room_entity == 'en_suite_room' else 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 = "10:00:00"
self.afternoon_start = "13:00:00"
self.daytime_end = "22:00:00"
def get_cover_entities(self):
# Cover entities
self.curtains = []
# 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_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"
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'
#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.template_list = []
self.binary_sensor_list = []
self.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.room_motion_sensor,
"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 add_mac_device(self, mac, name, model, integration='XiaoMiGateway3'):
###################################################################################################
# Wall Switches
#
# Aqara D1 Wall Switch (With Neutral, Triple Rocker) QBKG26LM ZigbeeID: ["lumi.switch.n3acn3"]
# Aqara D1 Wall Switch (With Neutral, Single Rocker) QBKG23LM ZigbeeID: ["lumi.switch.b1nacn02"]
###################################################################################################
if model == "Aqara D1 Wall Switch (With Neutral, Triple Rocker)" or \
model == "Aqara D1 Wall Switch (With Neutral, Single Rocker)":
key_num = 0
if model == "Aqara D1 Wall Switch (With Neutral, Triple Rocker)":
key_num = 3
elif model == "Aqara D1 Wall Switch (With Neutral, Single Rocker)":
key_num = 1
# Wall Switches
for i in range(1,key_num+1):
# single switch is with different postfix
i = '' if key_num == 1 else i
self.switch_list += [
{
"platform": "group",
"name": name + " Wall Switch " + str(i),
"entities": "switch." + mac + ('_switch' if key_num == 1 else '_channel_' + str(i)),
"configured": True
}
]
# Wall Buttons
self.template_list += [
{
"sensor": [
{
"name": name + " Wall Button",
# 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"]
###################################################################################################
elif model == "Aqara Door & Window Sensor":
self.binary_sensor_list += [
{
"platform": "group",
"name": name + " Door",
"entities": "binary_sensor." + mac + '_contact',
"configured": True
}
]
###################################################################################################
# Light Sensors
# Xiaomi Mi Light Detection Sensor GZCGQ01LM ZigbeeID: ["lumi.sen_ill.mgl01"]
###################################################################################################
###################################################################################################
# Motion Sensors, Occupancy Sensor
#
# Aqara Motion and Illuminance Sensor RTCGQ11LM ZigbeeID: ["lumi.sensor_motion.aq2"]
# Ziqing Occupancy Sensor, Mesh model: "mesh IZQ-24"
###################################################################################################
elif model == "Aqara Motion and Illuminance Sensor":
self.binary_sensor_list += [
{
"platform": "group",
"name": name + " Motion Sensor Motion",
"entities": "binary_sensor." + mac + '_motion',
"configured": True
}
]
self.template_list += [
{
"sensor": [
{
"name": name + " Motion Sensor Light",
"unit_of_measurement": "lx",
"state": '{{states.sensor["' + mac + '_illuminance"].state}}'
}
],
"configured": True
}
]
self.template_list += [
{
"sensor": [
{
"name": name + " Motion Sensor Battery",
"unit_of_measurement": "%",
"state": '{{states.sensor["' + mac + '_battery"].state}}'
}
],
"configured": True
}
]
elif model == "Ziqing Occupancy Sensor":
self.binary_sensor_list += [
{
"platform": "group",
"name": name + " Occupancy Sensor Occupancy",
"entities": "binary_sensor." + mac + '_occupancy',
"configured": True
}
]
self.template_list += [
{
"sensor": [
{
"name": name + " Occupancy Sensor Light",
"unit_of_measurement": "lx",
"state": '{{states.sensor["' + mac + '_illuminance"].state}}'
}
],
"configured": True
}
]
###################################################################################################
# Temperature Sensor
# Qingping Temperature Sensor, id: "ble LYWSDCGQ/01ZM"
# Mijia2 Temperature Sensor, id: "ble LYWSD03MMC"
###################################################################################################
elif model in [ "Qingping Temperature Sensor",
"Mijia2 Temperature Sensor" ] :
self.template_list += [
{
"sensor": [
{
"name": name + " Temperature Sensor",
"unit_of_measurement": "°C",
"state": '{{states.sensor["' + mac + '_temperature"].state | float | round(1)}}'
}
],
"configured": True
}
]
###################################################################################################
# Generic lights
###################################################################################################
elif model == "Generic Lights":
self.light_list += [
{
"platform": "group",
"name": name + " Light",
"entities": "light." + mac,
"configured": True
}
]
###################################################################################################
# Model not found, throw an error
###################################################################################################
else:
raise TypeError("Model " + model + " is not supported. Name = " + name)
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),
"options": \
[ "Idle" ]
+ (["Hue" ] if self.cfg_scene_colour_lamp else [])
+ (["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"
],
"configured": self.cfg_scene
}
}
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.room_heating_override) : {
"name" : self.getName(self.room_heating_override),
"initial": "off",
"configured": self.cfg_temp_control
}
}
self.input_boolean_dict |= {
self.getPostfix(self.ceiling_light_control_when['Lights on in hot sunshine']) : {
"name" : self.getName(self.ceiling_light_control_when['Lights on in hot sunshine']),
#"initial": "on",
"configured": True
} | ({"initial": "off"} if len(self.ceiling_lights) == 0 or len(self.curtains) == 0 else {}),
self.getPostfix(self.lamp_control_when['Lights on in hot sunshine']) : {
"name" : self.getName(self.lamp_control_when['Lights on in hot sunshine']),
#"initial": "off",
"configured": True
} | ({"initial": "off"} if len(self.lamps) == 0 or len(self.curtains) == 0 else {}),
self.getPostfix(self.led_control_when['Lights on in hot sunshine']) : {
"name" : self.getName(self.led_control_when['Lights on in hot sunshine']),
#"initial": "off",
"configured": True
} | ({"initial": "off"} if len(self.leds) == 0 or len(self.curtains) == 0 else {}),
self.getPostfix(self.curtain_control_when['Lights on in hot sunshine']) : {
"name" : self.getName(self.curtain_control_when['Lights on in hot sunshine']),
#"initial": "off",
"configured": True
} | ({"initial": "off"} if len(self.curtains) == 0 else {}),
self.getPostfix(self.ceiling_light_control_when['Lights on when bright outdoor']) : {
"name" : self.getName(self.ceiling_light_control_when['Lights on when bright outdoor']),
#"initial": "on",
"configured": True
} | ({"initial": "off"} if len(self.ceiling_lights) == 0 else {}),
self.getPostfix(self.lamp_control_when['Lights on when bright outdoor']) : {
"name" : self.getName(self.lamp_control_when['Lights on when bright outdoor']),
#"initial": "on",
"configured": True
} | ({"initial": "off"} if len(self.lamps) == 0 else {}),
self.getPostfix(self.led_control_when['Lights on when bright outdoor']) : {
"name" : self.getName(self.led_control_when['Lights on when bright outdoor']),
#"initial": "off",
"configured": True
} | ({"initial": "off"} if len(self.leds) == 0 else {}),
self.getPostfix(self.curtain_control_when['Lights on when bright outdoor']) : {
"name" : self.getName(self.curtain_control_when['Lights on when bright outdoor']),
#"initial": "off",
"configured": True
} | ({"initial": "off"} if len(self.curtains) == 0 else {}),
self.getPostfix(self.ceiling_light_control_when['Lights on when dark outdoor']) : {
"name" : self.getName(self.ceiling_light_control_when['Lights on when dark outdoor']),
#"initial": "on",
"configured": True
} | ({"initial": "off"} if len(self.ceiling_lights) == 0 else {}),
self.getPostfix(self.lamp_control_when['Lights on when dark outdoor']) : {
"name" : self.getName(self.lamp_control_when['Lights on when dark outdoor']),
#"initial": "on",
"configured": True
} | ({"initial": "off"} if len(self.lamps) == 0 else {}),
self.getPostfix(self.led_control_when['Lights on when dark outdoor']) : {
"name" : self.getName(self.led_control_when['Lights on when dark outdoor']),
#"initial": "on",
"configured": True
} | ({"initial": "off"} if len(self.leds) == 0 else {}),
self.getPostfix(self.curtain_control_when['Lights on when dark outdoor']) : {
"name" : self.getName(self.curtain_control_when['Lights on when dark outdoor']),
#"initial": "off",
"configured": True
} | ({"initial": "off"} if len(self.curtains) == 0 else {}),
}
# self.input_boolean_dict |= {
# self.light_in_daytime_postfix : {
# "name" : self.room_name + " Light in Daytime Enable",
# "configured": True
# # Initialized it to 'off' if auto curtain control is not supported
# } | ({} if self.cfg_auto_curtain_ctl else {"initial": "off"}),
#
# self.curtain_in_nighttime_postfix : {
# "name" : self.room_name + " Curtain In Nighttime Enable",
# "configured": True
# } | ({} if self.cfg_auto_curtain_ctl else {"initial": "off"}),
# }
self.input_boolean_dict |= {
self.getPostfix(self.room_heating_override): {
"name" : self.getName(self.room_heating_override),
"initial": "off",
"configured": self.cfg_temp_control
}
}
self.sensor_list += [
self.get_occupancy_ratio_sensor_config('1x'),
self.get_occupancy_ratio_sensor_config('2x')
]
# Group is special for "configured" as we need to remove disabled
# automations for generating groups
if self.cfg_occupancy:
self.group_dict |= {
self.room_entity + "_occupancy_group" : {
"name": self.room_name + " Occuapancy Group",
"entities": [self.room_occupancy, self.room_motion_sensor] + self.all_motion_sensors
}
}
self.group_dict |= {
self.room_entity + "_motion" : {
"name": self.room_name + " Motion",
"entities": self.all_motion_sensors
}
}
self.group_dict |= {
self.getPostfix(self.room_lights_and_tvs): {
"name" : self.getName(self.room_lights_and_tvs),
"entities": self.lights + self.tvs
}
}
def get_script_dict(self):
pass
# Lighting Automations
def get_automation_declarations(self):
self.gen_motion_light_automations()
self.gen_temp_control_automations()
self.gen_xiaomi_button_automations()
self.gen_wall_button_single_automations()
self.gen_wall_button_double_automations()
self.gen_occupancy_automations()
for self.automation in self.automations:
self.automation['id'] = self.getIDFromAlias(self.automation['alias'])
self.entity_declarations |= {'automation' : self.automations}
def get_automation_group_declarations(self):
self.automation_entity_list = []
# TODO rename this method because it includes other controls
# Including auto curtain controls
self.automation_entity_list = [] + \
([self.ceiling_light_control_when['Lights on in hot sunshine'] ] if len(self.ceiling_lights) > 0 and len(self.curtains) > 0 else []) + \
([self.lamp_control_when['Lights on in hot sunshine'] ] if len(self.lamps) > 0 and len(self.curtains) > 0 else []) + \
([self.led_control_when['Lights on in hot sunshine'] ] if len(self.leds) > 0 and len(self.curtains) > 0 else []) + \
([self.curtain_control_when['Lights on in hot sunshine'] ] if len(self.curtains) > 0 and len(self.curtains) > 0 else []) + \
([self.ceiling_light_control_when['Lights on when bright outdoor']] if len(self.ceiling_lights) > 0 else []) + \
([self.lamp_control_when['Lights on when bright outdoor'] ] if len(self.lamps) > 0 else []) + \
([self.led_control_when['Lights on when bright outdoor'] ] if len(self.leds) > 0 else []) + \
([self.curtain_control_when['Lights on when bright outdoor'] ] if len(self.curtains) > 0 else []) + \
([self.ceiling_light_control_when['Lights on when dark outdoor'] ] if len(self.ceiling_lights) > 0 else []) + \
([self.lamp_control_when['Lights on when dark outdoor'] ] if len(self.lamps) > 0 else []) + \
([self.led_control_when['Lights on when dark outdoor'] ] if len(self.leds) > 0 else []) + \
([self.curtain_control_when['Lights on when dark outdoor'] ] if len(self.curtains) > 0 else [])
# Generate automations group
for automation in self.entity_declarations['automation']:
self.automation_entity_list += [automation['id']]
if self.cfg_group_auto:
self.group_dict |= {
self.room_entity + "_auto_gen_automations" : {
"name": self.room_name + " Auto-gen Automations",
"entities": self.automation_entity_list ,
}
}
self.entity_declarations |= {
"group": self.group_dict
}
def populate_entnties_into_database(self):
self.entity_declarations |= {
"input_select": self.input_select_dict,
"sensor": self.sensor_list,
"input_boolean": self.input_boolean_dict,
"switch": self.switch_list,
"template": self.template_list,
"binary_sensor": self.binary_sensor_list,
"light": self.light_list
}
def gen_motion_light_automations(self):
self.automation_lights_on = {"alias":"ZL-" + self.automation_room_name + "Ceiling Lights On Or Open Curtains If Entering to Room" + "-" + self.room_name }
self.automation_lights_on['id'] = self.getIDFromAlias(self.automation_lights_on['alias'])
self.automations += [self.automation_lights_on | {
# Lights on automation are initially off until it is automatically turned on when no present detected
"configured" : self.cfg_motion_light,
"trigger" :
{
"entity_id": self.entrance_motion_sensors,
"platform": "state",
"to": "on"
},
"action": [
{
"service": "automation.turn_off",
"entity_id": self.automation_lights_on['id'],
"data": { "stop_actions": False }
},
# if hot summer
# turn on lights and open curtain based on control setting
# else
# if bright (not hot summer)
# turn on lights and open curtain based on control setting
# else (if dark)
# turn on lights and open curtain based on control setting
{
"if": self.condition_list_is('Hot sunshine'), "then": self.setNewScene('Lights on in hot sunshine'),
"else": {
"if": self.condition_list_is('Outdoor is bright'), "then": self.setNewScene('Lights on when bright outdoor'),
"else": self.setNewScene('Lights on when dark outdoor')
}
}
]
}]
self.automation_lights_off = {"alias":"ZL-" + self.automation_room_name + "Lights Off If No Person" + "-" + self.room_name}
self.automation_lights_off['id'] = self.getIDFromAlias(self.automation_lights_off['alias'])
self.automations += [self.automation_lights_off | {
"configured": self.cfg_motion_light,
"trigger": [
{ "platform": "state",
"entity_id": self.room_occupancy,
"to": "Outside"
},
{
"minutes": "/5",
"platform": "time_pattern"
}
],
"condition": [
{ "condition": "state",
"entity_id": self.room_occupancy,
"state": "Outside"
},
# Make sure that if room_occupany is forced to Outside because of people override (by button for example)
# and people are going back to the room, the lights should not be turned off
{ "condition": "state",
"entity_id": self.room_motion_sensor,
"state": "off",
"for": "00:01:00"
}],
"action": {
'parallel': [
# Re-enable lights on automation
{
"service": "homeassistant.turn_on",
"entity_id": self.automation_lights_on['id']
},
# Turn off lights/curtains/tv
self.turn(self.curtains, 'off'),
self.turn(self.tvs, 'off'),
self.setNewScene("All Off")
]
}
}
]
self.automations += [
{
"alias" : "ZL-" + self.automation_room_name + "LED On for 3 Min if Walking In the Dark" + "-" + self.room_name,
"configured": self.cfg_motion_light and self.cfg_motion_bed_led,
"mode": "restart",
"trigger": [
{
"entity_id": self.non_bed_motion_sensors,
"platform": "state",
"to": "on"
}
],
"condition": [
{
"condition": "state",
"entity_id": "input_select.indoor_brightness",
"state": "dark"
},
{
"entity_id":self.leds + self.ceiling_lights + self.lamps if self.room_entity != 'guest_room' else \
self.leds + self.ceiling_lights,
"condition": "state",
"state": "off"
}
],
"action": [
self.setNewScene("Dark Night Mode") if self.room_entity == 'master_room' else self.turn(self.leds, 'on', light_brightness=40),
#self.turn(self.leds, 'on', light_brightness=40),
{"delay": "00:03:00"},
{
"entity_id": self.ceiling_lights + self.lamps if self.room_entity != 'guest_room' else \
self.ceiling_lights,
"condition": "state",
"state": "off"
},
self.turn(self.leds, 'off')
]
}
]
def gen_temp_control_automations(self):
self.automation_heating_on = {"alias":"ZH-" + self.automation_room_name + "Heating Schedule On If Staying In the Room" + "-" + self.room_name}
self.automation_heating_on['id'] = self.getIDFromAlias(self.automation_heating_on['alias'])
self.automations += [self.automation_heating_on | {
"configured": self.cfg_temp_control and self.cfg_occupancy,
"trigger": [
{ "platform": "state",
"entity_id": self.room_occupancy,
"from": [ "Just Entered",
"Outside" ],
"to": "Stayed Inside"
}
],
"action": [
{
"if": [
{
"condition": "not",
"conditions": [
{ "condition": "state",
"entity_id": self.thermostat,
"state": "heat"
}
]
}
],
"then": self.turn('heating', 'on')
}
]
}
]
self.automation_heating_off = {"alias":"ZH-"+self.automation_room_name+"Heating Schedule Off If People Left the Room"+"-"+self.room_name}
self.automation_heating_off['id'] = self.getIDFromAlias(self.automation_heating_off['alias'])
self.automations += [self.automation_heating_off | {
"configured": self.cfg_temp_control and self.cfg_occupancy,
"trigger": [
{ "platform": "state",
"entity_id": self.room_occupancy,
"to": "Outside"
},
{
"minutes": "/5",
"platform": "time_pattern"
}
],
"action":
[
{ "condition": "state",
"entity_id": self.room_occupancy,
"state": "Outside"
},
## Make sure that if room_occupany is forced to Outside because of people override (by button for example)
## and people are going back to the room, the lights should not be turned off
#{ "condition": "state",
# "entity_id": self.room_motion_sensor,
# "state": "off",
# "for": "00:01:00"
#},
# Won't turn off heating unless override is off
{ "condition": "state",
"entity_id": self.room_heating_override,
"state": "off"
}
] + self.turn('heating', 'off')
}
]
self.automations += [
{
"alias" : "ZH-" + self.automation_room_name + "Heating Manual Override" + "-" + self.room_name,
"configured": self.cfg_temp_control,
"mode": "restart",
"trigger": [
{ "platform": "state",
"entity_id": self.room_heating_override
}
],
"action": [
{
"if": [
{
"condition": "state",
"entity_id": self.room_heating_override,
"state": "on"
}
],
"then": [
#{ "service": "automation.turn_off",
# "entity_id": [self.automation_heating_on['id'],
# self.automation_heating_off['id']]
#},
{ "service": "automation.trigger",
"entity_id": [self.automation_heating_on['id']]
}
],
"else": [
#{ "service": "automation.turn_on",
# "entity_id": [self.automation_heating_on['id'],
# self.automation_heating_off['id']]
#},
{ "service": "automation.trigger",
"entity_id": [self.automation_heating_off['id']]
}
]
}
]
}
]
self.automations += [
{
"alias" : "ZH-" + self.automation_room_name + "Heating Manual Override Timeout in Few Hours" + "-" + self.room_name,
"configured": self.cfg_temp_control,
"trigger": [
{
"platform": "state",
"entity_id": self.room_heating_override,
"from": "off",
"to": "on"
}
],
"action": [
{
"delay": {
"hours": 2,
"minutes": 0,
"seconds": 0,
"milliseconds": 0
}
},
{
"service": "homeassistant.turn_off",
"entity_id": self.room_heating_override
}
]
}
]
self.automations += [
{
"alias" : "ZH-" + self.automation_room_name + "Valve Calibrate Temperature Using External Sensor" + "-" + self.room_name,
"configured": self.cfg_temp_control and self.cfg_temp_calibration,
"use_blueprint": {
"path": "calibrate_valve_temperature.yaml",
"input": {
"tado_valve_entity": self.thermostat,
"external_temperature_sensor_entity": self.temperature_sensor
}
}
}
]
# State Machine
# "All Off"
# -> "All White" -> "Lamp LED White" -> "LED White"
# -> "Hue" -> "Night Mode" -> "Dark Night Mode"
#
# The code is essentially equivilent to the below, however, the code below
# is evaluated backwards - i.e. first evaluation is the last statement, so
# explicitly strictly order this evaluation can have the programme correctly run.
#
# [self.setNewSceneFromOldScene(cur_scene="All Off", nxt_scene="All White"),
# self.setNewSceneFromOldScene(nxt_scene="Lamp LED White")]
# + ([self.setNewSceneFromOldScene(nxt_scene="Night Mode")] if self.cfg_custom_scene else [])
# + ([self.setNewSceneFromOldScene(nxt_scene="LED White")]) if self.cfg_led_only_scene else []
# + ([self.setNewSceneFromOldScene(nxt_scene="All Off")])
#
def get_scene_state_machine(self):
cond_seq = []
cond_seq += [ self.setNewSceneFromOldScene(cur_scene="All Off", nxt_scene="All White"),
self.setNewSceneFromOldScene(nxt_scene="Lamp LED White")]
if self.cfg_scene_colour_led or self.cfg_scene_colour_lamp:
cond_seq += [self.setNewSceneFromOldScene(nxt_scene="Hue")]
if self.cfg_led_only_scene:
cond_seq += [self.setNewSceneFromOldScene(nxt_scene="LED White")]
if self.cfg_custom_scene:
#cond_seq += [self.setNewSceneFromOldScene(nxt_scene="Night Mode")]
cond_seq+= [self.setNewSceneFromOldScene(nxt_scene="Dark Night Mode")]
cond_seq += [self.setNewSceneFromOldScene(nxt_scene="All Off")]
return cond_seq
def gen_xiaomi_button_automations(self):
self.automations += [{
"alias" : "ZLB-" + self.automation_room_name + "Remote Button - Single - Applies Different Scenes (State Machine)" + "-" + self.room_name,
"configured": self.cfg_remote_light and (self.num_of_xiaomi_button > 0),
"trigger": [
{
"platform": "state",
"entity_id": self.xiaomi_buttons,
"to": ["single", "1"]
}
],
"mode":"queued",
# Skip the starting Reset scene as this is used to trigger transition when calling a scene
"condition": [
{
"condition": "not",
"conditions": [
{
"condition": "state",
"entity_id": self.room_scene_ctl,
"state": "Reset"
}
]
}
],
"action": [
{
"choose": self.get_scene_state_machine(),
# In the case no condition is matching, turn off everything
"default": self.setNewScene("All White")
}
]
}]