-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomation.py
More file actions
474 lines (346 loc) · 13.5 KB
/
automation.py
File metadata and controls
474 lines (346 loc) · 13.5 KB
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
import datetime
import os
from typing import Callable, Any
from pyziggy.device_bases import LightWithColorTemp, LightWithColor, LightWithDimming
from pyziggy.message_loop import MessageLoopTimer
from pyziggy.parameters import Broadcaster, NumericParameter
from pyziggy.parameters import (
SettableBinaryParameter,
SettableToggleParameter,
SettableAndQueryableBinaryParameter,
SettableAndQueryableToggleParameter,
)
from pyziggy.util import LightWithDimmingScalable as L2S
from pyziggy.util import ScaleMapper
from astral_mired import MiredCalculator, TimeOfDay, EasyAstral
from device_helpers import (
IkeaN2CommandRepeater,
PhilipsTapDialRotaryHelper,
PlugScalable,
)
from pushover import send_push_notification_to_home_group
from pyziggy_autogenerate.available_devices import (
AvailableDevices,
Philips_RDM002,
)
from secrets import get_secret_or_else
devices = AvailableDevices()
kitchen = ScaleMapper(
[
(L2S(devices.hue_lightstrip), 0.0, 0.54),
(L2S(devices.dining_light_1), 0.56, 0.93),
(L2S(devices.dining_light_2), 0.56, 0.93),
(L2S(devices.kitchen_light), 0.95, 1.0),
],
[0.55, 0.94],
lambda: os.system("afplay /System/Library/Sounds/Tink.aiff &"),
)
living_room_with_couch = ScaleMapper(
[
(PlugScalable(devices.plug), 0.0, 0.05),
(L2S(devices.standing_lamp), 0.07, 0.7),
(L2S(devices.couch), 0.2, 0.7),
(L2S(devices.tallbyn), 0.7, 1.0),
],
[0.06],
lambda: os.system("afplay /System/Library/Sounds/Tink.aiff &"),
)
living_room_no_couch = ScaleMapper(
[
(PlugScalable(devices.plug), 0.0, 0.05),
(L2S(devices.tallbyn), 0.07, 0.7),
(L2S(devices.standing_lamp), 0.5, 1.0),
],
[0.06, 0.7],
lambda: os.system("afplay /System/Library/Sounds/Tink.aiff &"),
)
living_room = living_room_with_couch
def set_mired(mired):
for device in devices.get_devices():
if isinstance(device, LightWithColorTemp):
device.color_temp.set(mired)
def ikea_remote_action_handler():
action = devices.ikea_remote.action.get_enum_value()
types = devices.ikea_remote.action.enum_type
if action == types.brightness_move_up:
kitchen.add(0.075)
elif action == types.brightness_move_down:
kitchen.add(-0.075)
elif action == types.on:
devices.dining_light_1.state.set(1)
devices.dining_light_2.state.set(1)
elif action == types.off:
devices.dining_light_1.state.set(0)
devices.dining_light_2.state.set(0)
elif action == types.arrow_left_click:
set_mired(417)
elif action == types.arrow_right_click:
set_mired(370)
ikea_remote_action_broadcaster = IkeaN2CommandRepeater(devices.ikea_remote)
ikea_remote_action_broadcaster.repeating_action.add_listener(ikea_remote_action_handler)
def tradfri_remote_action_handler():
action = devices.tradfri_remote.action.get_enum_value()
types = devices.tradfri_remote.action.enum_type
bedroom_devices: list[LightWithDimming] = [devices.lampion, devices.fado]
if action == types.toggle:
state_to = 0 if bedroom_devices[0].state.get() else 1
for device in bedroom_devices:
device.state.set(state_to)
device.brightness.set_normalized(1)
elif action == types.toggle_hold:
turn_off_everything()
elif action == types.brightness_down_click:
for device in bedroom_devices:
device.brightness.add_normalized(-0.2)
elif action == types.brightness_up_click:
for device in bedroom_devices:
device.brightness.add_normalized(0.2)
devices.tradfri_remote.action.add_listener(tradfri_remote_action_handler)
def kitchen_dimmer(step: int):
kitchen.add(step / 8 * 0.022)
def living_room_dimmer(step: int):
living_room.add(step / 8 * 0.022)
def hue_changer(step: int):
current_hue: float | None = None
for device in devices.get_devices():
if isinstance(device, LightWithColor):
if current_hue is None:
current_hue = device.color_hs.hue.get()
assert current_hue is not None
device.color_hs.hue.set((current_hue + step) % 360)
def saturation_changer(step: int):
current_saturation: float | None = None
for device in devices.get_devices():
if isinstance(device, LightWithColor):
if current_saturation is None:
current_saturation = device.color_hs.saturation.get()
assert current_saturation is not None
device.color_hs.saturation.set(current_saturation + step)
device_params_turned_off: list | None = None
def turn_off_everything():
global device_params_turned_off
new_device_params_turned_off = []
for device in devices.get_devices():
for name, param in vars(device).items():
if name == "state":
if (
isinstance(param, SettableBinaryParameter)
or isinstance(param, SettableAndQueryableBinaryParameter)
or isinstance(param, SettableToggleParameter)
or isinstance(param, SettableAndQueryableToggleParameter)
):
if param.get() > 0:
new_device_params_turned_off.append(param)
param.set(0)
if new_device_params_turned_off:
device_params_turned_off = new_device_params_turned_off
def turn_things_back_on():
global device_params_turned_off
if device_params_turned_off is None:
return
for param in device_params_turned_off:
param.set(1)
device_params_turned_off = None
default_button_mapping = {
devices.philips_switch: living_room_dimmer,
devices.switch_poang: living_room_dimmer,
devices.switch_kitchen: kitchen_dimmer,
}
def switch_living_room_scene():
global living_room
if living_room is living_room_with_couch:
living_room = living_room_no_couch
devices.couch.state.set(0)
else:
living_room = living_room_with_couch
devices.couch.state.set(1)
class PhilipsButtonHandler:
def __init__(self, switch: Philips_RDM002):
self.switch = switch
self.button_1_released = True
self.button_2_released = True
self.philips_dial_handler = default_button_mapping[self.switch]
self.switch.action.add_listener(self.button_handler)
self._timer = MessageLoopTimer(self._timer_callback)
self.rotary_helper = PhilipsTapDialRotaryHelper(self.switch)
self.rotary_helper.on_rotate.add_listener(
lambda step: self.philips_dial_handler(step)
)
def button_handler(self):
t = self.switch.action.enum_type
action = self.switch.action.get_enum_value()
if action == t.button_1_press:
self.philips_dial_handler = living_room_dimmer
self.start()
if action == t.button_2_press:
self.philips_dial_handler = kitchen_dimmer
self.start()
if action == t.button_3_press or action == t.button_4_press:
switch_living_room_scene()
if action == t.button_1_hold and self.button_1_released:
self.button_1_released = False
turn_off_everything()
if action == t.button_2_hold and self.button_2_released:
self.button_2_released = False
turn_things_back_on()
if action == t.button_1_hold_release:
self.button_1_released = True
if action == t.button_2_hold_release:
self.button_2_released = True
def start(self):
self._timer.start(300)
def _timer_callback(self, timer: MessageLoopTimer):
self.philips_dial_handler = default_button_mapping[self.switch]
philips_switches = (
devices.philips_switch,
devices.switch_kitchen,
devices.switch_poang,
)
button_handlers = [PhilipsButtonHandler(s) for s in philips_switches]
devices.fado.brightness.add_listener(
lambda: devices.fado.brightness.set_normalized(
min(0.55, devices.fado.brightness.get_normalized())
)
)
office: list[LightWithDimming] = [devices.printer, devices.tokabo, devices.reading_lamp]
def toggle_office():
lights_are_off = any([light.state.get() == 0 for light in office])
for light in office:
if lights_are_off:
light.state.set(1)
light.brightness.set_normalized(1.0)
else:
light.state.set(0)
def toggle_couch():
devices.couch.state.set(0 if devices.couch.state.get() > 0 else 1)
class AutoColorTemp:
def __init__(self):
self._calculator = MiredCalculator(
get_secret_or_else("location", (47.402339, 19.251788, 0.0)),
[
(2.0, 417),
(TimeOfDay.SUNRISE - 0.5, 370),
(TimeOfDay.SUNRISE + 0.5, 179), # 5600 K daylight
(TimeOfDay.SUNSET - 1, 179),
(TimeOfDay.SUNSET, 370), # 2700 K in the evening
(23, 370),
(24, 417), # 2400 K late night
],
)
self._timer = MessageLoopTimer(self._timer_callback)
self._last_mired = self._calculator.get_current_mired()
self.on_change = Broadcaster()
def get_mired(self):
return self._last_mired
def start(self):
self._timer.start(10)
def stop(self):
self._timer.stop()
def _timer_callback(self, timer: MessageLoopTimer):
new_mired = self._calculator.get_current_mired()
if new_mired != self._last_mired:
self.on_change._call_listeners()
self._last_mired = new_mired
auto_color_temp = AutoColorTemp()
lights_with_color_temp: list[LightWithColorTemp] = [
l for l in devices.get_devices() if isinstance(l, LightWithColorTemp)
]
def change_mired_for_light(light: LightWithColorTemp):
if light.state.get() > 0:
light.color_temp.set(auto_color_temp.get_mired())
for light in lights_with_color_temp:
light.state.add_listener(lambda light=light: change_mired_for_light(light)) # type: ignore
def change_mired():
for light in lights_with_color_temp:
light.color_temp.set(auto_color_temp.get_mired())
auto_color_temp.on_change.add_listener(change_mired)
devices.on_connect.add_listener(lambda: auto_color_temp.start())
class OnceADay:
def __init__(self, time_hr_decimal: float, callback: Callable[[], Any]):
self._time_hr_decimal = time_hr_decimal
self._callback = callback
# Ensure that we only fire if the specified time is in the future
self._day_of_last_execution = (
OnceADay.get_day() - 1
if EasyAstral.get_now_decimal() < self._time_hr_decimal
else OnceADay.get_day()
)
self._timer = MessageLoopTimer(self._timer_callback)
def start(self):
self._timer.start(5)
@staticmethod
def get_day():
return datetime.datetime.now().day
def _timer_callback(self, timer: MessageLoopTimer):
current_day = OnceADay.get_day()
if (
current_day != self._day_of_last_execution
and EasyAstral.get_now_decimal() > self._time_hr_decimal
):
self._day_of_last_execution = current_day
self._callback()
morning_lights: list[LightWithDimming] = [
devices.couch,
devices.tallbyn,
devices.hue_lightstrip,
devices.standing_lamp,
devices.reading_lamp,
devices.tokabo,
devices.printer,
devices.dining_light_1,
devices.dining_light_2,
]
def turn_on_morning_lights():
for light in morning_lights:
light.state.set(1)
light.brightness.set_normalized(1)
devices.plug.state.set(1)
for light in [devices.dining_light_1, devices.dining_light_2]:
light.brightness.set_normalized(0.5)
turn_on_lights_in_the_morning = OnceADay(8.5, turn_on_morning_lights)
devices.on_connect.add_listener(lambda: turn_on_lights_in_the_morning.start())
class WaterSensorAlert:
def __init__(self):
self.timer = MessageLoopTimer(self.timer_callback)
self.callback_counter = 0
self.timer.start(2)
def timer_callback(self, timer: MessageLoopTimer):
if self.callback_counter % 1 == 0:
os.system("afplay /System/Library/Sounds/Submarine.aiff &")
if self.callback_counter % 10 == 0:
send_push_notification_to_home_group("Water sensor alert!")
self.callback_counter += 1
def deactivate(self):
self.timer.stop()
water_sensor_alert: WaterSensorAlert | None = None
def activate_water_sensor_alert():
global water_sensor_alert
value = devices.dishwasher_leak_sensor.water_leak.get()
if value == 1:
if water_sensor_alert is not None:
return
water_sensor_alert = WaterSensorAlert()
else:
if water_sensor_alert is not None:
water_sensor_alert.deactivate()
water_sensor_alert = None
devices.dishwasher_leak_sensor.water_leak.add_listener(activate_water_sensor_alert)
class Tv(Broadcaster):
def __init__(self, current: NumericParameter):
super().__init__()
self._is_on: bool | None = False
current.add_listener(self._current_listener)
def _current_listener(self):
new_is_on = devices.ikea_smart_plug.current.get() > 0.4
if self._is_on is None:
self._is_on = new_is_on
return
state_changed = self._is_on != new_is_on
self._is_on = new_is_on
if state_changed:
self._call_listeners()
def get(self) -> bool:
if self._is_on is None:
return False
return self._is_on
tv_state = Tv(devices.ikea_smart_plug.current)