Config update
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
# ---------------------------------- Music ---------------------------------------------
|
||||
|
||||
URLS = {
|
||||
@@ -129,3 +130,39 @@ class CyclicCounter:
|
||||
def prev(self):
|
||||
self.idx = (self.idx - 1) % len(self.values)
|
||||
self.func(self.values[self.idx])
|
||||
|
||||
|
||||
|
||||
TIMEOUT_REPEAT_PRESS = 5 # time to switch states when pressed shortly in sequence, in seconds
|
||||
|
||||
# --------------------------------- Helper functions ----------------------------------------------------
|
||||
|
||||
light_scene_helper_dict = {}
|
||||
def scene_helper(scene_function):
|
||||
"""Function to manage the repeated press of the same action
|
||||
When pressed shortly in sequence, states are cycled
|
||||
Otherwise light is toggled"""
|
||||
unique_name_for_globals = id(scene_function)
|
||||
is_on_func, scenes = scene_function()
|
||||
last_called, call_idx = light_scene_helper_dict.get(unique_name_for_globals, (datetime.now(), -1))
|
||||
now = datetime.now()
|
||||
time_since_last_call = now - last_called
|
||||
recently_called = (time_since_last_call < timedelta(seconds=TIMEOUT_REPEAT_PRESS))
|
||||
light_already_on = is_on_func()
|
||||
log.warning(f"{light_already_on=}")
|
||||
if recently_called:
|
||||
call_idx = (call_idx + 1)
|
||||
else:
|
||||
call_idx = -1
|
||||
|
||||
if light_already_on and call_idx == -1:
|
||||
scenes[-1]()
|
||||
call_idx = -1
|
||||
elif not light_already_on:
|
||||
scenes[0]()
|
||||
call_idx = 0
|
||||
else:
|
||||
idx = call_idx % len(scenes)
|
||||
scenes[idx]()
|
||||
|
||||
light_scene_helper_dict[unique_name_for_globals] = (now, call_idx)
|
||||
Reference in New Issue
Block a user