Optimized code a little
This commit is contained in:
parent
b47435cb56
commit
966f9aa005
1 changed files with 16 additions and 15 deletions
|
@ -1,5 +1,4 @@
|
||||||
from datetime import datetime, time, timedelta
|
from datetime import datetime, time, timedelta
|
||||||
import string
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from interface.philips_hue import PhilipsHue
|
from interface.philips_hue import PhilipsHue
|
||||||
from sensor.people_counter import PeopleCounter
|
from sensor.people_counter import PeopleCounter
|
||||||
|
@ -50,11 +49,11 @@ def time_minus_time(time_a: time, time_b: time) -> timedelta:
|
||||||
today = datetime.today()
|
today = datetime.today()
|
||||||
dt_a = datetime.combine(today, time_a)
|
dt_a = datetime.combine(today, time_a)
|
||||||
dt_b = datetime.combine(today, time_b)
|
dt_b = datetime.combine(today, time_b)
|
||||||
|
|
||||||
return dt_a - dt_b
|
return dt_a - dt_b
|
||||||
|
|
||||||
|
|
||||||
def get_scene_for_time(time: time) -> string:
|
def get_scene_for_time(time: time) -> str:
|
||||||
"""Determines the correct scene to activate for a given time.
|
"""Determines the correct scene to activate for a given time.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -71,7 +70,7 @@ def get_scene_for_time(time: time) -> string:
|
||||||
previous_scene = None
|
previous_scene = None
|
||||||
for start_time, scene in SCHEDULE.items():
|
for start_time, scene in SCHEDULE.items():
|
||||||
# If current time is still after schedule time, just keep going
|
# If current time is still after schedule time, just keep going
|
||||||
if time_minus_time(start_time, time) < timedelta(0):
|
if start_time <= time:
|
||||||
previous_scene = scene
|
previous_scene = scene
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -181,7 +180,7 @@ def trigger_change(triggerState: Dict):
|
||||||
motion_triggered_lights = target_light_state
|
motion_triggered_lights = target_light_state
|
||||||
|
|
||||||
|
|
||||||
def set_light_scene(target_scene: string) -> bool:
|
def set_light_scene(target_scene: str) -> bool:
|
||||||
"""Sets the lights to the given scene, but only, if lights are already on. Does not correct count if lights are in an unexpected state.
|
"""Sets the lights to the given scene, but only, if lights are already on. Does not correct count if lights are in an unexpected state.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -249,13 +248,14 @@ def update_scene():
|
||||||
"""Called by time trigger to update light scene if lights are on.
|
"""Called by time trigger to update light scene if lights are on.
|
||||||
"""
|
"""
|
||||||
scene = get_scene_for_time(datetime.now().time())
|
scene = get_scene_for_time(datetime.now().time())
|
||||||
|
|
||||||
if scene is None:
|
if scene is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
set_light_scene(scene)
|
set_light_scene(scene)
|
||||||
logging.debug(f'Updated scene at {datetime.now().time()} to {scene}.')
|
logging.debug(f'Updated scene at {datetime.now().time()} to {scene}.')
|
||||||
|
|
||||||
|
|
||||||
def register_time_triggers():
|
def register_time_triggers():
|
||||||
"""Registeres time triggered callbacks based on the schedule, to adjust the current scene, if lights are on.
|
"""Registeres time triggered callbacks based on the schedule, to adjust the current scene, if lights are on.
|
||||||
"""
|
"""
|
||||||
|
@ -267,19 +267,20 @@ def register_time_triggers():
|
||||||
delta = time_minus_time(time, datetime.now().time())
|
delta = time_minus_time(time, datetime.now().time())
|
||||||
if delta < timedelta(0):
|
if delta < timedelta(0):
|
||||||
delta += timedelta(1)
|
delta += timedelta(1)
|
||||||
|
|
||||||
timeloop._add_job(update_scene, interval=timedelta(1), offset=delta)
|
timeloop._add_job(update_scene, interval=timedelta(1), offset=delta)
|
||||||
|
|
||||||
timeloop.start(block=False)
|
timeloop.start(block=False)
|
||||||
|
|
||||||
logging.info("Registered time triggers.")
|
logging.info("Registered time triggers.")
|
||||||
|
|
||||||
|
|
||||||
register_time_triggers()
|
if __name__ == "__main__":
|
||||||
|
register_time_triggers()
|
||||||
|
|
||||||
# Represents callback trigger order
|
# Represents callback trigger order
|
||||||
counter.hookChange(change_cb)
|
counter.hookChange(change_cb)
|
||||||
counter.hookCounting(count_change)
|
counter.hookCounting(count_change)
|
||||||
counter.hookTrigger(trigger_change)
|
counter.hookTrigger(trigger_change)
|
||||||
|
|
||||||
counter.run()
|
counter.run()
|
||||||
|
|
Loading…
Reference in a new issue