From 36bd835412a343d0788014b21191643767fff872 Mon Sep 17 00:00:00 2001 From: mgfcf Date: Fri, 3 Dec 2021 17:23:13 +0100 Subject: [PATCH] Implemented early light --- src/philips_hue_counter.py | 51 ++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/philips_hue_counter.py b/src/philips_hue_counter.py index 40cf0de..d5b352e 100644 --- a/src/philips_hue_counter.py +++ b/src/philips_hue_counter.py @@ -1,5 +1,7 @@ +from typing import Dict from interface.philips_hue import PhilipsHue from sensor.people_counter import PeopleCounter +from sensor.tof_sensor import Directions from sensor.vl53l1x_sensor import VL53L1XSensor import logging @@ -15,18 +17,22 @@ hue_conf = { hue = PhilipsHue(hue_conf) counter = PeopleCounter(VL53L1XSensor()) peopleCount = 0 +early_light_state = False logging.getLogger().setLevel(logging.INFO) def count_change(change: int) -> None: + global hue global peopleCount - + global early_light_state + # Are lights on at the moment? - previous_lights_state = hue.get_group(hue_conf['light_group'])['state']['any_on'] - + previous_lights_state = hue.get_group(hue_conf['light_group'])[ + 'state']['any_on'] + # Apply correction - if peopleCount <= 0 and previous_lights_state: + if peopleCount <= 0 and previous_lights_state and not early_light_state: # User indicates, that people count was not actually 0 peopleCount = 1 logging.debug(f'People count corrected to {peopleCount}') @@ -34,22 +40,51 @@ def count_change(change: int) -> None: # User indicates, that people count was actually 0 peopleCount = 0 logging.debug(f'People count corrected to {peopleCount}') - + peopleCount += change if peopleCount < 0: peopleCount = 0 logging.debug(f'People count changed by {change}') - + # Handle light target_light_state = peopleCount > 0 - + # Return, if there is no change if previous_lights_state == target_light_state: + if previous_lights_state: + early_light_state = False return - + hue.set_group(hue_conf['light_group'], {'on': target_light_state}) logging.debug(f'Light state changed to {target_light_state}') +def trigger_change(triggerState: Dict): + global hue + global early_light_state + + target_light_state = None + + # Is someone walking close to the door? + target_light_state = triggerState[Directions.INSIDE] or triggerState[Directions.OUTSIDE] + + # Only look at changing situations + if target_light_state == early_light_state: + return + + # Are lights on at the moment? + previous_lights_state = hue.get_group(hue_conf['light_group'])['state']['any_on'] + if target_light_state == previous_lights_state: + return + + # Adjust light as necessary + hue.set_group(hue_conf['light_group'], {'on': target_light_state}) + logging.debug(f'Light state changed to {target_light_state}') + + early_light_state = target_light_state + + + counter.hookCounting(count_change) +counter.hookTrigger(trigger_change) counter.run()