-
Notifications
You must be signed in to change notification settings - Fork 0
/
seasons.py
697 lines (677 loc) · 22 KB
/
seasons.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
# Seasons.py -- Hass helper python_script to turn a Climate entity into a smart
# thermostat, with support for multiple thermostats each having their own
# schedule.
#
# INPUTS:
# * climate_unit (required): The Climate entity to be controlled
# * global_mode (required): an entity whose state is the desired global climate
# mode (usually an input_select)
# * state_entity (required): an input_text entity, unique to this climate_unit,
# where the script can store some information between runs.
# * at_home_sensor (optional): an entity that represents whether anyone is home
# (usually a binary_sensor)
# * from_timer (optional): whether the script was triggered by timer. If true,
# only changes that modify the last-set operation mode or setpoint take
# effect. This is done so that manual changes are left alone until the next
# schedule switch.
#
# The last input is the 'seasons' dictionary as defined below, which defines the
# scheduled behavior for each global mode / climate unit combination. It's a
# dictionary keyed by a tuple of global mode and climate unit. Each entry is a
# list of schedules, where each schedule has the following fields:
# * title: Used only for logging and to help you find the right entry for edits
# * time_on / time_off (optional): Start and stop of this schedule, 24-hour
# hours:minutes. If not given, this schedule is always active (though see
# window and if_away/if_home below)
# * days: (optional): String defining days of week this schedule is active, matched
# on the schedule's start time. Seven characters, dash or dot if not active, any
# other character if active. You can use 0123456 or MTWTFSS or whatever you like.
# Monday is first, following Python datetime convention.
# * operation (required): The operating mode for this schedule, one of the modes
# supported by your climate entity.
# * setpoint (optional): The desired temperature for this schedule. Some modes
# (e.g. 'dry' dehumidifaction) don't require a setpoint so it's optional
# * window (optional): If given, if this entity's state is 'on' (i.e. the given
# window is open), the schedule will act as if its operation mode is 'off'.
# This is so you don't attempt to heat/cool the great outdoors if you left the
# window open for some fresh air.
# * if_away / if_home (optional): If present, this schedule will only apply if
# the at_home_sensor state matches (true meaning someone is home). If no
# at_home_sensor is given, these are both always false.
# * humidity_sensor (optional): See if_humid. Note: could also be some other
# type of sensor, like dewpoint.
# * if_humid (optional): Percentage. If present, this schedule will only apply
# if the humidity reported by the humidity_sensor is above this value at the
# beginning of the period.
#
# Put this script in <config>/python_scripts (create the directory if needed)
# and activate it as described at
# https://www.home-assistant.io/components/python_script/ .
# You should set up automations to call service python_script.seasons for each
# relevant climate unit for the each of the following events:
# * your global_mode entity changes (all climate units)
# * your at_home_sensor changes (all climate units)
# * your window sensor(s) change(s) (relevant climate units)
# * on a time_interval, suggested every 15 minutes. (all climate units). This
# interval is the resolution of your scheduled changes, so make it more or
# less frequent as required.
SEASONS = {
('Cold Winter', 'climate.first_floor_heat'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Cold Winter', 'climate.second_floor'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Cold Winter', 'climate.loft_heat'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Winter', 'climate.first_floor_heat'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Winter', 'climate.second_floor'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Winter', 'climate.master_br'): [
{
'title': 'Winter Sleeping',
'time_on': '21:29',
'time_off': '07:59',
'operation': 'heat',
'setpoint': 64
}
],
('Winter', 'climate.loft_heat'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Cold Shoulder', 'climate.first_floor_heat'): [
{
'title': 'Ecobee schedule',
'operation': 'heat'
}
],
('Cold Shoulder', 'climate.master_br'): [
{
'title': 'Morning (weekday)',
'days': 'MTWTF..',
'time_on': '05:44',
'time_off': '07:59',
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'setpoint': 67
},
{
'title': 'Morning (weekend)',
'days': '.....SS',
'time_on': '07:29',
'time_off': '08:59',
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'setpoint': 68
},
{
'title': 'Sleeping',
'time_on': '21:59',
'time_off': '08:59',
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'setpoint': 64
},
{
'title': 'Day (Away)',
'time_on': '07:59',
'time_off': '16:29',
'if_away': True,
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'setpoint': 62
},
{
'title': 'Day (Home)',
'time_on': '07:59',
'time_off': '17:59',
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'setpoint': 68
},
{
'title': 'Evening (Away)',
'time_on': '17:59',
'time_off': '21:44',
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'if_away': True,
'setpoint': 62
},
{
'title': 'Evening (Home)',
'time_on': '15:59',
'time_off': '21:44',
'operation': 'heat',
'window': 'binary_sensor.bedroom_window',
'setpoint': 68
}
],
('Cold Shoulder', 'climate.loft_heat'): [
{
'title': 'Morning Boost',
'operation': 'heat',
'time_on': '06:59',
'time_off': '07:44',
'window': 'binary_sensor.skylight',
'setpoint': 68
}
],
('Cold Shoulder', 'climate.loft'): [
{
'title': 'Night',
'operation': 'heat',
'time_on': '00:04',
'time_off': '07:14',
'window': 'binary_sensor.skylight',
'setpoint': 61
},
{
'title': 'Day (Weekday)',
'days': 'MTWTF..',
'operation': 'heat',
'time_on': '07:14',
'time_off': '16:59',
'window': 'binary_sensor.skylight',
'setpoint': 68
},
{
'title': 'Day (Weekend)',
'days': '.....SS',
'operation': 'heat',
'time_on': '08:59',
'time_off': '16:59',
'window': 'binary_sensor.skylight',
'setpoint': 62
},
{
'title': 'Evening',
'operation': 'heat',
'time_on': '16:59',
'time_off': '00:04',
'window': 'binary_sensor.skylight',
'setpoint': 63
}
],
('Warm Shoulder', 'climate.first_floor'): [
{
'title': 'Morning (weekday)',
'days': 'MTWTF..',
'time_on': '05:44',
'time_off': '07:59',
'operation': 'heat',
'setpoint': 68
},
{
'title': 'Morning (weekend)',
'days': '.....SS',
'time_on': '07:29',
'time_off': '08:59',
'operation': 'heat',
'setpoint': 68
},
{
'title': 'Pre-Sleeping',
'time_on': '21:44',
'time_off': '21:59',
'operation': 'heat',
'setpoint': 62
},
{
'title': 'Sleeping',
'time_on': '21:59',
'time_off': '08:59',
'operation': 'heat',
'setpoint': 62
},
{
'title': 'Day (Away)',
'time_on': '08:59',
'time_off': '16:29',
'if_away': True,
'operation': 'heat',
'setpoint': 62
},
{
'title': 'Day (Home)',
'time_on': '08:59',
'time_off': '17:59',
'operation': 'heat',
'setpoint': 68
},
{
'title': 'Evening (Away)',
'time_on': '17:59',
'time_off': '21:44',
'operation': 'heat',
'if_away': True,
'setpoint': 62
},
{
'title': 'Evening (Home)',
'time_on': '15:59',
'time_off': '21:44',
'operation': 'heat',
'setpoint': 69
}
],
('Warm Shoulder', 'climate.master_br'): [
{
'title': 'Morning (weekday)',
'days': 'MTWTF..',
'time_on': '05:44',
'time_off': '07:59',
'operation': 'heat',
'setpoint': 67
},
{
'title': 'Morning (weekend)',
'days': '.....SS',
'time_on': '07:29',
'time_off': '08:59',
'operation': 'heat',
'setpoint': 68
},
{
'title': 'Pre-Sleeping',
'time_on': '21:44',
'time_off': '21:59',
'operation': 'heat',
'setpoint': 64
},
{
'title': 'Sleeping',
'time_on': '21:59',
'time_off': '08:59',
'operation': 'heat',
'setpoint': 64
},
{
'title': 'Day (Away)',
'time_on': '08:59',
'time_off': '16:29',
'if_away': True,
'operation': 'heat',
'setpoint': 62
},
{
'title': 'Day (Home)',
'time_on': '08:59',
'time_off': '17:59',
'operation': 'heat',
'setpoint': 68
},
{
'title': 'Evening (Away)',
'time_on': '17:59',
'time_off': '21:44',
'operation': 'heat',
'if_away': True,
'setpoint': 62
},
{
'title': 'Evening (Home)',
'time_on': '15:59',
'time_off': '21:44',
'operation': 'heat',
'setpoint': 68
}
],
('Warm Shoulder', 'climate.loft'): [
{
'title': 'Night',
'operation': 'heat',
'time_on': '00:04',
'time_off': '07:29',
'window': 'binary_sensor.skylight',
'setpoint': 61
},
{
'title': 'Day (Weekday)',
'days': 'MTWTF..',
'operation': 'heat',
'time_on': '07:29',
'time_off': '17:59',
'window': 'binary_sensor.skylight',
'setpoint': 68
},
{
'title': 'Day (Weekend)',
'days': '.....SS',
'operation': 'heat',
'time_on': '08:59',
'time_off': '17:59',
'window': 'binary_sensor.skylight',
'setpoint': 62
},
{
'title': 'Evening',
'operation': 'heat',
'time_on': '17:59',
'time_off': '00:04',
'window': 'binary_sensor.skylight',
'setpoint': 63
}
],
('Normal Summer', 'climate.master_br'): [
{
'title': 'Dehumidify',
'time_on': '19:59',
'time_off': '20:59',
'operation': 'dry',
'humidity_sensor': 'sensor.dewpoint_mbr',
'if_humid': 63,
'window': 'binary_sensor.bedroom_window',
},
{
'title': 'Sleeping-early',
'time_on': '20:59',
'time_off': '02:59',
'operation': 'cool',
'window': 'binary_sensor.bedroom_window',
'setpoint': 73
},
{
'title': 'Sleeping-late',
'time_on': '02:59',
'time_off': '07:59',
'operation': 'cool',
'window': 'binary_sensor.bedroom_window',
'setpoint': 74
},
],
('Hot Summer', 'climate.master_br'): [
{
'title': 'Dehumidify',
'time_on': '19:59',
'time_off': '20:59',
'operation': 'dry',
'humidity_sensor': 'sensor.dewpoint_mbr',
'if_humid': 63,
'window': 'binary_sensor.bedroom_window',
},
{
'title': 'Sleeping-early',
'time_on': '20:59',
'time_off': '02:59',
'operation': 'cool',
'window': 'binary_sensor.bedroom_window',
'setpoint': 73
},
{
'title': 'Sleeping-late',
'time_on': '02:59',
'time_off': '07:59',
'operation': 'cool',
'window': 'binary_sensor.bedroom_window',
'setpoint': 74
},
{
'title': 'Day (Away)',
'time_on': '08:29',
'time_off': '19:44',
'operation': 'cool',
'window': 'binary_sensor.bedroom_window',
'if_away': True,
'setpoint': 78
},
{
'title': 'Day (Home)',
'time_on': '08:29',
'time_off': '19:44',
'operation': 'cool',
'window': 'binary_sensor.bedroom_window',
'setpoint': 76
},
],
('Hot Summer', 'climate.loft'): [
{
'title': 'Night',
'operation': 'cool',
'time_on': '00:04',
'time_off': '08:59',
'window': 'binary_sensor.skylight',
'setpoint': 83
},
{
'title': 'Day (away)',
'operation': 'cool',
'time_on': '08:59',
'time_off': '17:59',
'if_away': True,
'window': 'binary_sensor.skylight',
'setpoint': 85
},
{
'title': 'Day',
'operation': 'cool',
'time_on': '08:59',
'time_off': '17:59',
'window': 'binary_sensor.skylight',
'setpoint': 80
},
{
'title': 'Evening',
'operation': 'cool',
'time_on': '17:59',
'time_off': '00:04',
'window': 'binary_sensor.skylight',
'setpoint': 81
}
],
('Hot Summer', 'climate.first_floor'): [
{
'title': 'Sleeping',
'time_on': '21:59',
'time_off': '05:59',
'window': 'binary_sensor.first_floor_windows',
'operation': 'cool',
'setpoint': 78
},
{
'title': 'Day (Away)',
'time_on': '07:59',
'time_off': '15:59',
'operation': 'cool',
'window': 'binary_sensor.first_floor_windows',
'if_away': True,
'setpoint': 78
},
{
'title': 'Day (Home)',
'time_on': '05:59',
'time_off': '15:59',
'window': 'binary_sensor.first_floor_windows',
'operation': 'cool',
'setpoint': 75
},
{
'title': 'Evening (Away)',
'time_on': '17:59',
'time_off': '21:44',
'operation': 'cool',
'window': 'binary_sensor.first_floor_windows',
'if_away': True,
'setpoint': 78
},
{
'title': 'Evening (Home)',
'time_on': '15:59',
'time_off': '21:44',
'window': 'binary_sensor.first_floor_windows',
'operation': 'cool',
'setpoint': 75
}
]
}
def is_time_between(begin_time, end_time, check_time):
if begin_time < end_time:
return begin_time <= check_time <= end_time
# crosses midnight
return check_time >= begin_time or check_time <= end_time
def time_offset(orig_time, offset):
hour = orig_time.hour
minute = orig_time.minute
minute = minute + offset
if minute < 0:
hour = hour - 1
minute = minute + 60
if minute > 60:
hour = hour + 1
minute = minute - 60
if hour < 0:
hour = hour + 24
if hour > 24:
hour = hour - 24
return datetime.time(hour=hour, minute=minute)
def day_of_start(start_time, end_time, check_time):
today = datetime.datetime.now().weekday()
# today if doesn't cross midnight or no actual times given
if (not start_time) or (not end_time) or start_time <= end_time:
return today
# today if we're between start and midnight
if start_time <= check_time:
return today
# Otherwise, yesterday
return 6 if today == 0 else today - 1
saved_state = hass.states.get(data.get('state_entity')).state
climate_unit = data.get('climate_unit', 'climate.master_br')
current_mode = hass.states.get(data.get('global_mode')).state
from_timer = data.get('from_timer', False)
at_home_sensor = data.get('at_home_sensor')
is_home = False
is_away = False
if at_home_sensor:
is_home = hass.states.get(at_home_sensor).state == 'on'
is_away = not is_home
now = datetime.datetime.now().time()
key = (current_mode, climate_unit)
schedules = SEASONS.get(key)
matched = False
setpoint = None
turn_on = False
turn_off = False
desired_operation = None
title = None
next_state = None
if not schedules:
logger.info("No schedules for {}".format(key))
else:
for schedule in schedules:
time_on_str = schedule.get('time_on')
time_off_str = schedule.get('time_off')
time_on = None
time_off = None
if time_on_str:
time_on = datetime.datetime.strptime(time_on_str, '%H:%M').time()
if time_off_str:
time_off = datetime.datetime.strptime(time_off_str, '%H:%M').time()
start_day = day_of_start(time_on, time_off, now)
day_match = True
days = schedule.get('days')
if days and len(days) == 7:
day_match = days[start_day] != '-' and days[start_day] != '.'
in_interval = day_match and (((not time_on) or (not time_off) or
is_time_between(time_on, time_off, now)))
home_away_match = True
if schedule.get('if_home'):
home_away_match = is_home
if schedule.get('if_away'):
home_away_match = not is_home
dry_exclude = False
hs = schedule.get('humidity_sensor')
if hs and schedule.get('if_humid'):
dry_exclude = float(hass.states.get(hs).state) < float(schedule['if_humid'])
if in_interval and home_away_match and not dry_exclude:
# When we get here, we have schedules for this unit and
# global mode and we're in this schedule's interval.
# We will obey this schedule and ignore subsequent matches
if not time_on:
time_on = datetime.datetime.strptime('00:00', '%H:%M').time()
window_open = False
if schedule.get('window'):
window_open = hass.states.get(schedule['window']).state == 'on'
else:
window_open = False
decided = False
matched = True
next_state = "%s-%s" % (str(schedule.get('operation')),
str(schedule.get('setpoint')))
same_next_state = (next_state == saved_state)
if window_open:
# Off if window is open
turn_off = True
title = schedule.get('title') + ' (Window open)'
decided = True
if (not decided) and from_timer and (not same_next_state):
desired_operation = schedule.get('operation')
if desired_operation == 'off':
turn_off = True
else:
turn_on = True
setpoint = schedule.get('setpoint')
title = schedule.get('title')
decided = True
if not decided and (not from_timer):
desired_operation = schedule.get('operation')
if desired_operation == 'off':
turn_off = True
else:
turn_on = True
setpoint = schedule.get('setpoint')
title = schedule.get('title')
decided = True
break
if not matched and current_mode != "Manual":
# If no schedules matched, turn off except in Manual
next_state = "off-None"
same_next_state = (next_state == saved_state)
if (not from_timer) or (not same_next_state):
turn_off = True
title = 'Default (Off)'
if turn_off:
desired_operation = 'off'
if desired_operation:
logger.info("Setting {} to mode {} target {} from schedule {}".format(
climate_unit, desired_operation, setpoint, title))
service_data = {
"entity_id": climate_unit,
"hvac_mode": desired_operation
}
hass.services.call('climate', 'set_hvac_mode', service_data, False)
if setpoint:
time.sleep(2.0)
if '.' in str(setpoint):
setpoint_num = float(setpoint)
else:
setpoint_num = int(setpoint)
service_data = {
"entity_id": climate_unit,
"temperature": setpoint_num,
"hvac_mode": desired_operation
}
hass.services.call('climate', 'set_temperature', service_data, False)
if next_state:
hass.states.set(data.get('state_entity'), next_state)