#!/usr/bin/env python # -*- coding: utf-8 -*- # import config import devices import json import logging from function.rooms import room_shelly, room_shelly_tradfri_light from function.helpers import changed_value_indicator 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 first_floor_east_floor(room_shelly): def __init__(self, mqtt_client): # http://shelly1l-3C6105E4E629 super().__init__(mqtt_client, "shellies/floor_madi", "gui/ffe_sw_floor") class first_floor_east_kitchen(room_shelly): # TODO: add circulation pump (switch, time) def __init__(self, mqtt_client): # http://shelly1l-8CAAB5616C01 super().__init__(mqtt_client, "shellies/kitchen", "gui/ffe_sw_kitchen") class first_floor_east_dining(room_shelly): def __init__(self, mqtt_client): # http://shelly1l-84CCA8ADD055 super().__init__(mqtt_client, "shellies/diningroom", "gui/ffe_sw_diningroom") self.floorlamp_powerplug = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/dining_floorlamp") if config.CHRISTMAS: self.garland_powerplug = devices.silvercrest_powerplug(mqtt_client, topic="zigbee_og_e/powerplug/aux") # self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_dining_floorlamp") # # Callback initialisation # self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation) self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp) self.floorlamp_powerplug.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp) # self.cvi = changed_value_indicator() def all_off(self, device=None, key=None, data=None): super().all_off(device, key, data) self.floorlamp_powerplug.set_output_0(False) def floorlamp_synchronisation(self, device, key, data): if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None: logger.info("Syncing \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) self.floorlamp_powerplug.set_output_0(data) def gui_switch_command_floorlamp(self, device, key, data): logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data)) self.floorlamp_powerplug.set_output_0(data) def gui_switch_feedback_floorlamp(self, device, key, data): self.gui_switch_floorlamp.set_feedback(data) class first_floor_east_sleep_madi(room_shelly_tradfri_light): def __init__(self, mqtt_client): # http://shelly1l-E8DB84A254C7 super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi", "zigbee_og_e/light/sleep_madi", "gui/ffe_br_sleep_madi", "gui/ffe_ct_sleep_madi") # self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di") # self.gui_switch_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_sw_bed_light_di") self.gui_brightness_bed_light_di = devices.nodered_gui(mqtt_client, "gui/ffe_br_bed_light_di") self.gui_brightness_bed_light_di.enable(False) self.gui_brightness_bed_light_di.set_feedback(0) # self.button_tradfri = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east") # # Callback initialisation # self.init_bed_light_functions() self.init_fade_function() # self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE, self.toggle_main_light) self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.toggle_bed_light_di) self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.toggle_bed_light_di) self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.fade_light) def all_off(self, device=None, key=None, data=None): super().all_off(device, key, data) self.bed_light_di_tradfri.set_output_0(False) # bed light def init_bed_light_functions(self): self.gui_switch_bed_light_di.add_callback( devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_bed_light_di) self.bed_light_di_tradfri.add_callback( devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_bed_light_di) self.bed_light_di_tradfri.add_callback( devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_bed_light_di) self.gui_brightness_bed_light_di.add_callback( devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_bed_light_di) def gui_switch_command_bed_light_di(self, device, key, data): logger.info("Switching \"%s\" bed light dirk: %s", type(self).__name__, str(data)) self.bed_light_di_tradfri.set_output_0(data) def gui_switch_feedback_bed_light_di(self, device, key, data): self.gui_switch_bed_light_di.set_feedback(data) self.gui_brightness_bed_light_di.enable(data) if data is False: self.gui_brightness_bed_light_di.set_feedback(0) else: self.gui_brightness_bed_light_di.set_feedback(self.bed_light_di_tradfri.brightness) def set_gui_brightness_bed_light_di(self, device, key, data): self.gui_brightness_bed_light_di.set_feedback(data) def set_brightness_bed_light_di(self, device, key, data): logger.info("Setting brightness \"%s\" bed light dirk: %.1f", type(self).__name__, data) self.bed_light_di_tradfri.set_brightness(data) def toggle_bed_light_di(self, device, key, data): logger.info("Toggeling \"%s\" bed light dirk", type(self).__name__) self.bed_light_di_tradfri.set_output_0("toggle") # fade def init_fade_function(self): self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.state_machine_last_activated_device) self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.state_machine_last_activated_device) # self.last_activated_device = None def state_machine_last_activated_device(self, device, topic, data): if data is True: self.last_activated_device = device.topic elif data is False: if device.topic == self.main_light_shelly.topic and self.bed_light_di_tradfri.output_0: self.last_activated_device = self.bed_light_di_tradfri.topic elif device.topic == self.bed_light_di_tradfri.topic and self.main_light_shelly.output_0: self.last_activated_device = self.main_light_shelly.topic else: self.last_activated_device = None self.mqtt_client.send("gui/ffe_led_main_light_sleep/set", json.dumps(self.last_activated_device == self.main_light_shelly.topic)) self.mqtt_client.send("gui/ffe_led_bed_light_sleep_di/set", json.dumps(self.last_activated_device == self.bed_light_di_tradfri.topic)) def fade_light(self, device, topic, data): if self.last_activated_device == self.main_light_shelly.topic: target = self.main_light_tradfri elif self.last_activated_device == self.bed_light_di_tradfri.topic: target = self.bed_light_di_tradfri else: return if (data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG): logger.info("Increasing brightness \"%s\" %s", type(self).__name__, target.topic) target.default_inc() elif (data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG): logger.info("Decreasing brightness \"%s\" %s", type(self).__name__, target.topic) target.default_dec() elif (data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]): logger.info("Stoping brightness change \"%s\" %s", type(self).__name__, target.topic) target.default_stop() class first_floor_east_living(room_shelly_tradfri_light): def __init__(self, mqtt_client): # http://shelly1l-3C6105E3F910 super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom", "zigbee_og_e/light/livingroom", "gui/ffe_br_livingroom", "gui/ffe_ct_livingroom") for i in range(1, 7): setattr(self, 'floorlamp_tradfri_%d' % i, devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/living_floorlamp_%d" % i)) if config.CHRISTMAS: self.powerplug_xmas_tree = devices.silvercrest_powerplug(mqtt_client, "zigbee_og_e/powerplug/xmas-tree") # self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_living_floorlamp") self.gui_brightness_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_br_livingroom_floorlamp") self.gui_brightness_floorlamp.enable(False) self.gui_brightness_floorlamp.set_feedback(0) self.gui_color_temp_floorlamp = devices.nodered_gui(mqtt_client, "gui/ffe_ct_livingroom_floorlamp") self.gui_color_temp_floorlamp.enable(False) self.gui_color_temp_floorlamp.set_feedback(0) # if config.CHRISTMAS: self.gui_switch_xmas_tree = devices.nodered_gui(mqtt_client, "gui/ffe_sw_livingroom_xmas_tree") # # Callback initialisation # self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.floorlamp_synchronisation) self.gui_switch_floorlamp.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_floorlamp) self.floorlamp_tradfri_1.add_callback( devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp) self.floorlamp_tradfri_1.add_callback( devices.tradfri_light.KEY_BRIGHTNESS, None, self.set_gui_brightness_floorlamp) self.floorlamp_tradfri_1.add_callback( devices.tradfri_light.KEY_COLOR_TEMP, None, self.set_gui_color_temp_floorlamp) self.gui_brightness_floorlamp.add_callback( devices.nodered_gui.KEY_BRIGHTNESS, None, self.set_brightness_floorlamp) self.gui_color_temp_floorlamp.add_callback( devices.nodered_gui.KEY_COLOR_TEMP, None, self.set_color_temp_floorlamp) # if config.CHRISTMAS: self.powerplug_xmas_tree.add_callback( devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_action) self.gui_switch_xmas_tree.add_callback(devices.nodered_gui.KEY_STATE, None, self.gui_switch_command_xmas) # self.cvi = changed_value_indicator() def all_off(self, device=None, key=None, data=None): super().all_off(device, key, data) for floorlamp in self.__floorlamp_devices__(): floorlamp.set_output_0(False) def __floorlamp_devices__(self): rv = [] for i in range(1, 7): rv.append(getattr(self, 'floorlamp_tradfri_%d' % i)) return rv def floorlamp_synchronisation(self, device, key, data): if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None: logger.info("Syncing \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) for device in self.__floorlamp_devices__(): device.set_output_0(data) def gui_switch_command_floorlamp(self, device, key, data): logger.info("Switching \"%s\" floorlamp: %s", type(self).__name__, str(data)) for device in self.__floorlamp_devices__(): device.set_output_0(data) def gui_switch_feedback_floorlamp(self, device, key, data): self.gui_switch_floorlamp.set_feedback(data) self.gui_brightness_floorlamp.enable(data) self.gui_color_temp_floorlamp.enable(data) if data is False: self.gui_brightness_floorlamp.set_feedback(0) self.gui_color_temp_floorlamp.set_feedback(0) else: self.gui_brightness_floorlamp.set_feedback(self.floorlamp_tradfri_1.brightness) self.gui_color_temp_floorlamp.set_feedback(self.floorlamp_tradfri_1.color_temp / 10) def set_gui_brightness_floorlamp(self, device, key, data): self.gui_brightness_floorlamp.set_feedback(data) def set_gui_color_temp_floorlamp(self, device, key, data): self.gui_color_temp_floorlamp.set_feedback(data / 10) def set_brightness_floorlamp(self, device, key, data): logger.info("Setting brightness \"%s\" floorlamp: %.1f", type(self).__name__, data) for device in self.__floorlamp_devices__(): device.set_brightness(data) def set_color_temp_floorlamp(self, device, key, data): logger.info("Setting color_temp \"%s\" floorlamp: %.1f", type(self).__name__, data) for device in self.__floorlamp_devices__(): device.set_color_temp(data * 10) def powerplug_xmas_action(self, device, key, data): self.gui_switch_xmas_tree.set_feedback(data) def gui_switch_command_xmas(self, device, key, data): logger.info("Switching \"%s\" xmas-tree: %s", type(self).__name__, data) self.powerplug_xmas_tree.set_output_0(data)