From 6a7f17feffe1ce4ba8f475a002af009cd10588d5 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sun, 25 Dec 2022 22:59:55 +0100 Subject: [PATCH] room_shelly_motion_sensor adapted to fit to silvercrest functionality --- function/first_floor_east.py | 8 +++----- function/rooms.py | 32 ++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/function/first_floor_east.py b/function/first_floor_east.py index f93e84a..5204454 100644 --- a/function/first_floor_east.py +++ b/function/first_floor_east.py @@ -6,7 +6,7 @@ import config import devices import json import logging -from function.rooms import room_shelly, room_shelly_auto_off, room_shelly_tradfri_light +from function.rooms import room_shelly, room_shelly_motion_sensor, room_shelly_tradfri_light from function.helpers import changed_value_indicator try: from config import APP_NAME as ROOT_LOGGER_NAME @@ -15,12 +15,10 @@ except ImportError: logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) -class first_floor_east_floor(room_shelly_auto_off): +class first_floor_east_floor(room_shelly_motion_sensor): def __init__(self, mqtt_client): # http://shelly1l-3C6105E4E629 - super().__init__(mqtt_client, "shellies/ffe/floor/main_light", "gui/ffe/floor/main_light/switch") - self.motion_sensor_silvercrest = devices.silvercrest_motion_sensor(mqtt_client, "zigbee/ffe/floor/motion_sensor") - self.motion_sensor_silvercrest.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, True, self.main_light_shelly.set_output_0_mcb) + super().__init__(mqtt_client, "shellies/ffe/floor/main_light", "gui/ffe/floor/main_light/switch", "zigbee/ffe/floor/motion_sensor") class first_floor_east_kitchen(room_shelly): diff --git a/function/rooms.py b/function/rooms.py index 59bd08c..472f7ab 100644 --- a/function/rooms.py +++ b/function/rooms.py @@ -56,26 +56,46 @@ class room_shelly(room): self.last_flash_data = data -class room_shelly_auto_off(room_shelly): - def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, timer_value=30): +class room_shelly_motion_sensor(room_shelly): + def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_motion_sensor_1, topic_motion_sensor_2=None, timer_value=30): super().__init__(mqtt_client, topic_shelly, topic_gui_switch) self.timer_value = timer_value - self.main_light_timer = None # self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True) # + self.motion_sensor_silvercrest_1 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_1) + self.motion_sensor_silvercrest_1.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected) + # + if topic_motion_sensor_2 is not None: + self.motion_sensor_silvercrest_2 = devices.silvercrest_motion_sensor(mqtt_client, topic_motion_sensor_2) + self.motion_sensor_silvercrest_2.add_callback(devices.silvercrest_motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected) + # + self.motion_detected_1 = False + self.motion_detected_2 = False + self.main_light_timer = None + # cyclic_task = task.periodic(1, self.cyclic_task) cyclic_task.run() + def set_motion_detected(self, device, key, data): + if device == self.motion_sensor_silvercrest_1: + self.motion_detected_1 = data + elif device == self.motion_sensor_silvercrest_2: + self.motion_detected_2 = data + if data is True: + self.main_light_shelly.set_output_0(True) + def reload_timer(self, device, key, data): self.main_light_timer = self.timer_value def cyclic_task(self, cyclic_task): if self.main_light_timer is not None: - self.main_light_timer -= cyclic_task.cycle_time if self.main_light_timer <= 0: - self.main_light_shelly.set_output_0(False) - self.main_light_timer = None + if not self.motion_detected_1 and not self.motion_detected_2: + self.main_light_shelly.set_output_0(False) + self.main_light_timer = None + else: + self.main_light_timer -= cyclic_task.cycle_time class room_shelly_tradfri_light(room_shelly):