123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
-
- import devices
- from function.helpers import now, sunset_time, sunrise_time
- import logging
- import task
-
- try:
- from config import APP_NAME as ROOT_LOGGER_NAME
- except ImportError:
- ROOT_LOGGER_NAME = 'root'
- logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
-
-
- class room(object):
- def __init__(self, mqtt_client):
- self.mqtt_client = mqtt_client
-
-
- class room_shelly(room):
- def __init__(self, mqtt_client, topic_shelly, topic_gui_switch):
- super().__init__(mqtt_client)
- self.main_light_shelly = devices.shelly(mqtt_client, topic=topic_shelly)
- #
- self.gui_switch_main_light = devices.nodered_gui_switch(mqtt_client, topic=topic_gui_switch)
- #
- # Callback initialisation
- #
- self.gui_switch_main_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.main_light_shelly.set_output_0_mcb)
- self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_main_light.set_state_mcb)
- #
- self.block_all_off = False
- self.last_flash_data = None
- self.delayed_task = task.delayed(.25, self.main_light_shelly.toggle_output_0_mcb, None, None, None)
-
- def all_off(self, device=None, key=None, data=None):
- if not self.block_all_off:
- logger.info("Switching all off \"%s\"", type(self).__name__)
- self.main_light_shelly.set_output_0(False)
- self.main_light_shelly.set_output_1(False)
- self.block_all_off = False
-
- def all_off_feedback(self, device=None, key=None, data=None):
- logger.info("Flashing \"%s\" main light", type(self).__name__)
- if self.main_light_shelly.output_0 is False:
- self.main_light_shelly.set_output_0(True)
- self.block_all_off = True
- self.delayed_task.run()
-
- def flash_main_light(self, device, key, data):
- if self.last_flash_data != data and data is True:
- logger.info("Flashing \"%s\" main light", type(self).__name__)
- self.main_light_shelly.toggle_output_0_mcb(device, key, data)
- self.delayed_task.run()
- self.last_flash_data = data
-
-
- 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_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
- self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_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.reset_timer()
- #
- cyclic_task = task.periodic(1, self.cyclic_task)
- cyclic_task.run()
-
- def set_motion_detected(self, device, key, data):
- if now() < sunrise_time(60) or now() > sunset_time(-60) or data is False:
- 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:
- logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.main_light_shelly.topic)
- self.main_light_shelly.set_output_0(True)
-
- def reload_timer(self, device, key, data):
- self.main_light_timer = self.timer_value
-
- def reset_timer(self, device=None, key=None, data=None):
- self.main_light_timer = None
- self.motion_detected_1 = False
- self.motion_detected_2 = False
-
- def cyclic_task(self, cyclic_task):
- if self.main_light_timer is not None:
- if self.main_light_timer <= 0:
- if not self.motion_detected_1 and not self.motion_detected_2:
- logger.info("No motion and time ran out - Switching off main light %s", self.main_light_shelly.topic)
- 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):
- def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
- super().__init__(mqtt_client, topic_shelly, topic_gui_switch)
- self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic_tradfri_light)
- #
- self.gui_br_ct_main_light = devices.nodered_gui_brightness_color_temp(mqtt_client, topic_gui_br_ct)
- #
- # Callback initialisation
- #
- self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_br_ct_main_light.set_enable_mcb)
- self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_main_light.set_brightness_mcb)
- self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_ct_main_light.set_color_temp_mcb)
- self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
- None, self.main_light_tradfri.set_brightness_mcb)
- self.gui_br_ct_main_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
- None, self.main_light_tradfri.set_color_temp_mcb)
-
-
- class room_shelly_silvercrest_light(room_shelly_tradfri_light):
- def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct):
- super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_br_ct)
- #
- # Callback initialisation
- #
- self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.get_initial_main_light_data)
- #
- self.main_light_shelly_last = None
-
- def get_initial_main_light_data(self, device, key, data):
- if data is True and self.main_light_shelly_last != data:
- self.send_init_message_main_light()
- self.main_light_shelly_last = data
-
- def send_init_message_main_light(self):
- self.main_light_tradfri.mqtt_client.send(self.main_light_tradfri.topic + "/get", '{"state": ""}')
|