#!/usr/bin/env python # -*- coding: utf-8 -*- # import devices 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 def gui_switch_feedback(self, device, key, data): self.gui_switch_main_light.set_feedback(data) 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(mqtt_client, topic=topic_gui_switch) # # Callback initialisation # self.gui_switch_main_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command) self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.gui_switch_feedback) # self.block_all_off = False self.last_flash_data = None self.delayed_task = task.delayed(.25, self.toggle_main_light, 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 gui_switch_command(self, device, key, data): logger.info("Switching \"%s\" main light: %s", type(self).__name__, str(data)) self.main_light_shelly.set_output_0(data) def toggle_main_light(self, device, key, data): logger.info("Toggeling \"%s\" main light", type(self).__name__) self.main_light_shelly.set_output_0("toggle") 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.toggle_main_light(device, key, data) self.delayed_task.run() self.last_flash_data = data class room_shelly_tradfri_light(room_shelly): def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp): super().__init__(mqtt_client, topic_shelly, topic_gui_switch) self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic=topic_tradfri_light) # self.gui_brightness_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_brightness) self.gui_brightness_main_light.enable(False) self.gui_brightness_main_light.set_feedback(0) self.gui_color_temp_main_light = devices.nodered_gui(mqtt_client, topic=topic_gui_color_temp) self.gui_color_temp_main_light.enable(False) self.gui_color_temp_main_light.set_feedback(0) # # Callback initialisation # self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.enable_brightness_n_colortemp) self.main_light_tradfri.add_callback( devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_main_light) self.main_light_tradfri.add_callback( devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_main_light) self.gui_brightness_main_light.add_callback( devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_main_light) self.gui_color_temp_main_light.add_callback( devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_main_light) def enable_brightness_n_colortemp(self, devive, key, data): self.gui_brightness_main_light.enable(data) self.gui_color_temp_main_light.enable(data) if data is False: self.gui_brightness_main_light.set_feedback(0) self.gui_color_temp_main_light.set_feedback(0) else: self.gui_brightness_main_light.set_feedback(self.main_light_tradfri.brightness) self.gui_color_temp_main_light.set_feedback(self.main_light_tradfri.color_temp / 10) def set_gui_brightness_main_light(self, device, key, data): self.gui_brightness_main_light.set_feedback(data) def set_gui_color_temp_main_light(self, device, key, data): self.gui_color_temp_main_light.set_feedback(data / 10) def set_brightness_main_light(self, device, key, data): logger.info("Setting brightness \"%s\" main light: %.1f", type(self).__name__, data) self.main_light_tradfri.set_brightness(data) def set_color_temp_main_light(self, device, key, data): logger.info("Setting color_temp \"%s\" main light: %.1f", type(self).__name__, data) self.main_light_tradfri.set_color_temp(data * 10) def fade_light(self, device, topic, data): if (data == 'brightness_up_hold'): logger.info("Increasing brightness \"%s\" main light", type(self).__name__) self.main_light_tradfri.brightness_inc() elif (data == 'brightness_down_hold'): logger.info("Decreasing brightness \"%s\" main light", type(self).__name__) self.main_light_tradfri.brightness_dec() elif (data.startswith('brightness') and data.endswith('release')): logger.info("Stoping brightness change \"%s\" main light", type(self).__name__) self.main_light_tradfri.brightness_stop() class room_shelly_silvercrest_light(room_shelly_tradfri_light): def __init__(self, mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp): super().__init__(mqtt_client, topic_shelly, topic_gui_switch, topic_tradfri_light, topic_gui_brightness, topic_gui_color_temp) # # 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 is not True: 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": ""}')