2022-04-27 22:39:27 +02:00
|
|
|
from datetime import datetime, time, timedelta
|
2022-09-14 00:15:37 +02:00
|
|
|
from services.philips_hue import PhilipsHue
|
|
|
|
from sensors import PeopleCounter, Directions, VL53L1XSensor
|
2021-11-03 00:26:29 +01:00
|
|
|
import logging
|
2021-12-03 17:30:54 +01:00
|
|
|
import json
|
2022-04-27 22:39:27 +02:00
|
|
|
from timeloop import Timeloop
|
2021-11-03 00:26:29 +01:00
|
|
|
|
2022-03-14 20:49:47 +01:00
|
|
|
|
|
|
|
# Should lights already turn on where there is any kind of motion in the sensor
|
|
|
|
ENABLE_MOTION_TRIGGERED_LIGHT = True
|
|
|
|
|
2022-05-18 08:52:42 +02:00
|
|
|
# Should lights change when a certain time in the schedule is reached
|
2022-09-14 00:15:37 +02:00
|
|
|
ENABLE_SCHEDULE_TRIGGERS = (
|
|
|
|
False # Not working correctly at the moment, so turned off by default
|
|
|
|
)
|
2022-05-18 08:52:42 +02:00
|
|
|
|
2022-04-27 22:04:06 +02:00
|
|
|
# Schedule (Key is time after scene should be used. Value is scene name to be used.)
|
|
|
|
# Needs to be sorted chronologically
|
2022-09-14 00:15:37 +02:00
|
|
|
SCHEDULE: dict[time, str] = {
|
|
|
|
time(8, 0): "Good Morning",
|
|
|
|
time(18, 0): "Evening",
|
|
|
|
time(22, 0): "Nightlight",
|
|
|
|
}
|
2022-04-27 22:04:06 +02:00
|
|
|
|
2022-03-14 20:49:47 +01:00
|
|
|
|
2022-09-14 00:15:37 +02:00
|
|
|
LOG_FILE_PATH = "log.txt" # Path for logs
|
2021-11-03 00:26:29 +01:00
|
|
|
hue_conf = {
|
2022-09-14 00:15:37 +02:00
|
|
|
"bridge_ip": "",
|
|
|
|
"transition_time": 10, # seconds
|
|
|
|
"light_group": "",
|
2021-11-03 00:26:29 +01:00
|
|
|
# If file exists, application is considered 'registered' at the bridge
|
2022-09-14 00:15:37 +02:00
|
|
|
"registered_file": "smart_light_registered.bridge",
|
|
|
|
} # Custom configuration for philips hue
|
2021-11-03 00:26:29 +01:00
|
|
|
|
|
|
|
|
2022-03-14 20:49:47 +01:00
|
|
|
hue: PhilipsHue = PhilipsHue(hue_conf) # Light interface
|
|
|
|
counter: PeopleCounter = PeopleCounter(VL53L1XSensor()) # Sensor object
|
2022-09-14 00:15:37 +02:00
|
|
|
peopleCount: int = 0 # Global count of people on the inside
|
|
|
|
motion_triggered_lights = False # Is light on because of any detected motion
|
2022-04-27 22:39:27 +02:00
|
|
|
timeloop: Timeloop = Timeloop() # Used for time triggered schedule
|
2021-11-03 00:26:29 +01:00
|
|
|
|
|
|
|
logging.getLogger().setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
2022-04-27 23:06:17 +02:00
|
|
|
def time_minus_time(time_a: time, time_b: time) -> timedelta:
|
|
|
|
"""Implementes a basic timedelta function for time objects.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
time_a (time): Time to subtract from.
|
|
|
|
time_b (time): Time to be subtracted.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
timedelta: Delta between the two time objects.
|
|
|
|
"""
|
|
|
|
today = datetime.today()
|
|
|
|
dt_a = datetime.combine(today, time_a)
|
|
|
|
dt_b = datetime.combine(today, time_b)
|
2022-05-08 22:51:48 +02:00
|
|
|
|
2022-04-27 23:06:17 +02:00
|
|
|
return dt_a - dt_b
|
|
|
|
|
|
|
|
|
2022-05-08 22:51:48 +02:00
|
|
|
def get_scene_for_time(time: time) -> str:
|
2022-04-27 22:04:06 +02:00
|
|
|
"""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
|
2022-05-08 22:51:48 +02:00
|
|
|
if start_time <= time:
|
2022-04-27 22:04:06 +02:00
|
|
|
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
|
2022-04-27 23:13:19 +02:00
|
|
|
return list(SCHEDULE.values())[-1]
|
2022-04-27 22:04:06 +02:00
|
|
|
|
|
|
|
|
2022-09-14 00:15:37 +02:00
|
|
|
def change_cb(countChange: int, directionState: dict):
|
2022-03-14 20:49:47 +01:00
|
|
|
"""Handles basic logging of event data for later analysis.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
countChange (int): The change in the number of people. Usually on of [-1, 0, 1].
|
2022-09-14 00:15:37 +02:00
|
|
|
directionState (dict): Object describing the internal state of the sensor.
|
2022-03-14 20:49:47 +01:00
|
|
|
"""
|
|
|
|
data = {
|
2022-09-14 00:15:37 +02:00
|
|
|
"version": "v0.0",
|
|
|
|
"previousPeopleCount": peopleCount,
|
|
|
|
"countChange": countChange,
|
|
|
|
"directionState": directionState,
|
|
|
|
"dateTime": datetime.now(),
|
|
|
|
"motionTriggeredLights": motion_triggered_lights,
|
2022-03-14 20:49:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2022-09-14 00:15:37 +02:00
|
|
|
with open(LOG_FILE_PATH, "a") as f:
|
2022-03-14 20:49:47 +01:00
|
|
|
f.write(json.dumps(data, default=str) + "\n")
|
|
|
|
except Exception as ex:
|
2022-09-14 00:15:37 +02:00
|
|
|
logging.exception(f"Unable to write log file. {ex}")
|
2022-03-14 20:49:47 +01:00
|
|
|
|
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
def count_change(change: int) -> None:
|
2022-03-14 20:49:47 +01:00
|
|
|
"""Handles light state when people count changes
|
|
|
|
|
|
|
|
Args:
|
|
|
|
change (int): The change in the number of people. Usually on of [-1, 0, 1].
|
|
|
|
"""
|
2021-12-03 17:23:13 +01:00
|
|
|
global hue
|
2021-11-03 00:26:29 +01:00
|
|
|
global peopleCount
|
2022-03-14 20:49:47 +01:00
|
|
|
global motion_triggered_lights
|
2021-12-03 17:23:13 +01:00
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
# Are lights on at the moment?
|
2022-03-15 21:54:01 +01:00
|
|
|
previous_lights_state = get_light_state()
|
2021-12-03 17:23:13 +01:00
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
# Apply correction
|
2022-03-14 20:49:47 +01:00
|
|
|
if peopleCount <= 0 and previous_lights_state and not motion_triggered_lights:
|
|
|
|
# Count was 0, but lights were on (not because of motion triggers) => people count was not actually 0
|
2021-11-03 00:26:29 +01:00
|
|
|
peopleCount = 1
|
2022-09-14 00:15:37 +02:00
|
|
|
logging.debug(f"People count corrected to {peopleCount}")
|
2021-11-03 00:26:29 +01:00
|
|
|
elif peopleCount > 0 and not previous_lights_state:
|
2022-03-14 20:49:47 +01:00
|
|
|
# Count was >0, but lights were off => people count was actually 0
|
2021-11-03 00:26:29 +01:00
|
|
|
peopleCount = 0
|
2022-09-14 00:15:37 +02:00
|
|
|
logging.debug(f"People count corrected to {peopleCount}")
|
2021-12-03 17:23:13 +01:00
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
peopleCount += change
|
|
|
|
if peopleCount < 0:
|
|
|
|
peopleCount = 0
|
2022-09-14 00:15:37 +02:00
|
|
|
logging.debug(f"People count changed by {change}")
|
2021-12-03 17:23:13 +01:00
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
# Handle light
|
|
|
|
target_light_state = peopleCount > 0
|
2021-12-03 17:23:13 +01:00
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
# Return, if there is no change
|
|
|
|
if previous_lights_state == target_light_state:
|
2021-12-03 17:23:13 +01:00
|
|
|
if previous_lights_state:
|
2022-03-14 20:49:47 +01:00
|
|
|
# Signaling that the people count is taking control over the light now
|
2022-03-15 21:54:01 +01:00
|
|
|
motion_triggered_lights = False
|
2021-12-03 17:23:13 +01:00
|
|
|
return
|
|
|
|
|
2022-03-15 21:54:01 +01:00
|
|
|
set_light_state(target_light_state)
|
2021-12-03 17:23:13 +01:00
|
|
|
|
|
|
|
|
2022-09-14 00:15:37 +02:00
|
|
|
def trigger_change(triggerState: dict):
|
2022-03-14 20:49:47 +01:00
|
|
|
"""Handles motion triggered light state.
|
|
|
|
|
|
|
|
Args:
|
2022-09-14 00:15:37 +02:00
|
|
|
triggerState (dict): Describing in what directions the sensor is triggerd.
|
2022-03-14 20:49:47 +01:00
|
|
|
"""
|
2021-12-03 17:23:13 +01:00
|
|
|
global hue
|
2022-03-14 20:49:47 +01:00
|
|
|
global motion_triggered_lights
|
2021-12-03 17:23:13 +01:00
|
|
|
|
|
|
|
target_light_state = None
|
|
|
|
|
|
|
|
# Is someone walking close to the door?
|
2022-09-14 00:15:37 +02:00
|
|
|
motion_detected = (
|
|
|
|
triggerState[Directions.INSIDE] or triggerState[Directions.OUTSIDE]
|
|
|
|
)
|
2022-03-14 20:49:47 +01:00
|
|
|
target_light_state = motion_detected
|
2022-03-15 21:54:01 +01:00
|
|
|
|
2022-03-14 20:49:47 +01:00
|
|
|
# Does motion triggered light need to do anything?
|
|
|
|
if peopleCount > 0:
|
|
|
|
# State is successfully handled by the count
|
|
|
|
motion_triggered_lights = False
|
|
|
|
return
|
2021-12-03 17:23:13 +01:00
|
|
|
|
|
|
|
# Only look at changing situations
|
2022-03-14 20:49:47 +01:00
|
|
|
if target_light_state == motion_triggered_lights:
|
2021-11-03 00:26:29 +01:00
|
|
|
return
|
2022-03-14 20:49:47 +01:00
|
|
|
|
2022-03-15 21:54:01 +01:00
|
|
|
set_light_state(target_light_state)
|
|
|
|
|
|
|
|
# Save state
|
|
|
|
motion_triggered_lights = target_light_state
|
|
|
|
|
|
|
|
|
2022-05-08 22:51:48 +02:00
|
|
|
def set_light_scene(target_scene: str) -> bool:
|
2022-04-27 22:04:06 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
# Is valid scene?
|
|
|
|
if target_scene is None:
|
2022-05-08 22:54:48 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# Are lights on at the moment? Only based on people count for simplicity
|
|
|
|
if peopleCount <= 0:
|
|
|
|
# Lights are probably off, not doing anything
|
|
|
|
return
|
2022-04-27 22:04:06 +02:00
|
|
|
|
|
|
|
# Set lights to scene
|
2022-09-14 00:15:37 +02:00
|
|
|
hue.set_group_scene(hue_conf["light_group"], target_scene)
|
|
|
|
logging.debug(f"Light scene set to {target_scene}")
|
2022-04-27 22:04:06 +02:00
|
|
|
|
|
|
|
|
2022-03-15 21:54:01 +01:00
|
|
|
def set_light_state(target_light_state: bool) -> bool:
|
|
|
|
"""Sets the lights to the given state.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_light_state (bool): Should lights on the inside be on or off.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: Previous light state.
|
|
|
|
"""
|
2021-12-03 17:23:13 +01:00
|
|
|
# Are lights on at the moment?
|
2022-03-15 21:54:01 +01:00
|
|
|
previous_lights_state = get_light_state()
|
2021-12-03 17:23:13 +01:00
|
|
|
if target_light_state == previous_lights_state:
|
2022-03-15 21:54:01 +01:00
|
|
|
return previous_lights_state
|
2022-03-14 20:49:47 +01:00
|
|
|
|
2022-09-14 00:15:37 +02:00
|
|
|
# Adjust light if necessary
|
2022-04-27 22:39:27 +02:00
|
|
|
target_scene = get_scene_for_time(datetime.now().time())
|
2022-04-28 07:54:49 +02:00
|
|
|
if target_light_state and target_scene:
|
|
|
|
# Set to specific scene if exists
|
2022-09-14 00:15:37 +02:00
|
|
|
hue.set_group_scene(hue_conf["light_group"], target_scene)
|
2022-04-27 22:04:06 +02:00
|
|
|
logging.debug(
|
2022-09-14 00:15:37 +02:00
|
|
|
f"Light state changed to {target_light_state} with scene {target_scene}"
|
|
|
|
)
|
2022-04-27 22:04:06 +02:00
|
|
|
else:
|
2022-09-14 00:15:37 +02:00
|
|
|
hue.set_group(hue_conf["light_group"], {"on": target_light_state})
|
|
|
|
logging.debug(f"Light state changed to {target_light_state}")
|
2021-12-03 17:23:13 +01:00
|
|
|
|
2022-03-15 21:54:01 +01:00
|
|
|
return previous_lights_state
|
|
|
|
|
|
|
|
|
|
|
|
def get_light_state() -> bool:
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
bool: Current light state.
|
|
|
|
"""
|
2022-09-14 00:15:37 +02:00
|
|
|
return hue.get_group(hue_conf["light_group"])["state"]["any_on"]
|
2021-12-03 17:30:54 +01:00
|
|
|
|
2021-11-03 00:26:29 +01:00
|
|
|
|
2022-04-27 22:39:27 +02:00
|
|
|
def update_scene():
|
2022-09-14 00:15:37 +02:00
|
|
|
"""Called by time trigger to update light scene if lights are on."""
|
2022-04-27 22:39:27 +02:00
|
|
|
scene = get_scene_for_time(datetime.now().time())
|
2022-05-08 22:51:48 +02:00
|
|
|
|
2022-04-27 22:39:27 +02:00
|
|
|
if scene is None:
|
|
|
|
return
|
2022-05-08 22:51:48 +02:00
|
|
|
|
2022-04-27 22:39:27 +02:00
|
|
|
set_light_scene(scene)
|
2022-09-14 00:15:37 +02:00
|
|
|
logging.debug(f"Updated scene at {datetime.now().time()} to {scene}.")
|
2022-04-27 22:39:27 +02:00
|
|
|
|
2022-05-08 22:51:48 +02:00
|
|
|
|
2022-04-27 22:39:27 +02:00
|
|
|
def register_time_triggers():
|
2022-09-14 00:15:37 +02:00
|
|
|
"""Registeres time triggered callbacks based on the schedule, to adjust the current scene, if lights are on."""
|
2022-04-27 22:39:27 +02:00
|
|
|
global SCHEDULE
|
|
|
|
if SCHEDULE is None or len(SCHEDULE) <= 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
for time in SCHEDULE.keys():
|
2022-04-27 23:06:17 +02:00
|
|
|
delta = time_minus_time(time, datetime.now().time())
|
2022-04-27 23:08:44 +02:00
|
|
|
if delta < timedelta(0):
|
2022-04-27 22:39:27 +02:00
|
|
|
delta += timedelta(1)
|
2022-05-08 22:51:48 +02:00
|
|
|
|
2022-04-27 22:39:27 +02:00
|
|
|
timeloop._add_job(update_scene, interval=timedelta(1), offset=delta)
|
2022-05-08 22:51:48 +02:00
|
|
|
|
2022-04-27 22:39:27 +02:00
|
|
|
timeloop.start(block=False)
|
|
|
|
|
|
|
|
logging.info("Registered time triggers.")
|
|
|
|
|
|
|
|
|
2022-05-08 22:51:48 +02:00
|
|
|
if __name__ == "__main__":
|
2022-05-18 08:52:42 +02:00
|
|
|
if ENABLE_SCHEDULE_TRIGGERS:
|
|
|
|
register_time_triggers()
|
2022-04-27 22:04:06 +02:00
|
|
|
|
2022-05-08 22:51:48 +02:00
|
|
|
# Represents callback trigger order
|
|
|
|
counter.hookChange(change_cb)
|
|
|
|
counter.hookCounting(count_change)
|
|
|
|
counter.hookTrigger(trigger_change)
|
2022-03-14 20:49:47 +01:00
|
|
|
|
2022-05-08 22:51:48 +02:00
|
|
|
counter.run()
|