Basic schedule with custom scenes for different times
This commit is contained in:
parent
63c2264eae
commit
043f405a97
1 changed files with 84 additions and 5 deletions
|
@ -1,5 +1,5 @@
|
||||||
from datetime import datetime
|
from datetime import datetime, time
|
||||||
from pathlib import Path
|
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
|
||||||
|
@ -12,6 +12,10 @@ import json
|
||||||
# Should lights already turn on where there is any kind of motion in the sensor
|
# Should lights already turn on where there is any kind of motion in the sensor
|
||||||
ENABLE_MOTION_TRIGGERED_LIGHT = True
|
ENABLE_MOTION_TRIGGERED_LIGHT = True
|
||||||
|
|
||||||
|
# Schedule (Key is time after scene should be used. Value is scene name to be used.)
|
||||||
|
# Needs to be sorted chronologically
|
||||||
|
SCHEDULE = {}
|
||||||
|
|
||||||
|
|
||||||
LOG_FILE_PATH = "log.txt" # Path for logs
|
LOG_FILE_PATH = "log.txt" # Path for logs
|
||||||
hue_conf = {
|
hue_conf = {
|
||||||
|
@ -31,6 +35,45 @@ motion_triggered_lights = False # Is light on because of any detected motion
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
|
def get_scene_for_time(time: time) -> string:
|
||||||
|
"""Determines the correct scene to activate for a given time.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
time (time): Time to find scene for.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
string: Scene name that should be active. None, if schedule is empty.
|
||||||
|
"""
|
||||||
|
global SCHEDULE
|
||||||
|
|
||||||
|
if SCHEDULE is None or len(SCHEDULE) <= 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
previous_scene = None
|
||||||
|
for start_time, scene in SCHEDULE.items():
|
||||||
|
# If current time is still after schedule time, just keep going
|
||||||
|
if start_time - time < 0:
|
||||||
|
previous_scene = scene
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Schedule timef is now after current time, which is too late
|
||||||
|
# So if exists, take previous scene, since it was the last before the current time
|
||||||
|
if previous_scene:
|
||||||
|
return previous_scene
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Only breaks if it could not find a valid scene, so use lates scene as fallback
|
||||||
|
return SCHEDULE.values()[-1]
|
||||||
|
|
||||||
|
|
||||||
|
def register_time_triggers():
|
||||||
|
"""Registeres time triggered callbacks based on the schedule, to adjust the current scene, if lights are on.
|
||||||
|
"""
|
||||||
|
#! TODO
|
||||||
|
logging.info("Registered time triggers.")
|
||||||
|
|
||||||
|
|
||||||
def change_cb(countChange: int, directionState: Dict):
|
def change_cb(countChange: int, directionState: Dict):
|
||||||
"""Handles basic logging of event data for later analysis.
|
"""Handles basic logging of event data for later analysis.
|
||||||
|
|
||||||
|
@ -126,6 +169,34 @@ 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:
|
||||||
|
"""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:
|
||||||
|
target_scene (string): Name of the scene to activate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True, if lights are on after calling this function.
|
||||||
|
"""
|
||||||
|
# Are lights on at the moment?
|
||||||
|
light_state = get_light_state()
|
||||||
|
if not light_state:
|
||||||
|
# Lights are off, not doing anything
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Is valid scene?
|
||||||
|
if target_scene is None:
|
||||||
|
return True # Light still on
|
||||||
|
|
||||||
|
# Set lights to scene
|
||||||
|
hue.set_group_scene(hue_conf['light_group'], target_scene)
|
||||||
|
logging.debug(
|
||||||
|
f'Light scene set to {target_scene}')
|
||||||
|
|
||||||
|
# Lights should be on now
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def set_light_state(target_light_state: bool) -> bool:
|
def set_light_state(target_light_state: bool) -> bool:
|
||||||
"""Sets the lights to the given state.
|
"""Sets the lights to the given state.
|
||||||
|
|
||||||
|
@ -141,9 +212,15 @@ def set_light_state(target_light_state: bool) -> bool:
|
||||||
return previous_lights_state
|
return previous_lights_state
|
||||||
|
|
||||||
# Adjust light as necessary
|
# Adjust light as necessary
|
||||||
hue.set_group(hue_conf['light_group'], {'on': target_light_state})
|
target_scene = get_scene_for_time(datetime.now())
|
||||||
|
# Set to specific scene if exists
|
||||||
|
if target_scene:
|
||||||
|
hue.set_group_scene(hue_conf['light_group'], target_scene)
|
||||||
logging.debug(
|
logging.debug(
|
||||||
f'Light state changed to {target_light_state} for early light')
|
f'Light state changed to {target_light_state} with scene {target_scene}')
|
||||||
|
else:
|
||||||
|
hue.set_group(hue_conf['light_group'], {'on': target_light_state})
|
||||||
|
logging.debug(f'Light state changed to {target_light_state}')
|
||||||
|
|
||||||
return previous_lights_state
|
return previous_lights_state
|
||||||
|
|
||||||
|
@ -156,6 +233,8 @@ def get_light_state() -> bool:
|
||||||
return hue.get_group(hue_conf['light_group'])['state']['any_on']
|
return hue.get_group(hue_conf['light_group'])['state']['any_on']
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in a new issue