#!/usr/bin/env python # -*- coding: utf-8 -*- # import config import devices import inspect import logging try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) # TODO: Add brightness and color temp including disable of gui elements. # TODO: Add lamp sleep_di # TODO: Add input_device functions # TODO: Clean-Up Node-Red class room(object): 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): 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) def all_off(self): self.main_light_shelly.set_output_0(False) self.main_light_shelly.set_output_1(False) 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) 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.main_light_shelly_last = None def floorlamp_synchronisation(self, device, key, data): if data != self.main_light_shelly_last: logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) self.floorlamp_powerplug.set_output_0(data) self.main_light_shelly_last = 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): def __init__(self, mqtt_client): # http://shelly1l-E8DB84A254C7 super().__init__(mqtt_client, "shellies/sleep_madi", "gui/ffe_sw_sleep_madi") self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_madi") self.bed_light_di_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_bed_di") self.button_tradfri = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east") class first_floor_east_living(room_shelly): def __init__(self, mqtt_client): # http://shelly1l-3C6105E3F910 super().__init__(mqtt_client, "shellies/livingroom", "gui/ffe_sw_livingroom") self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/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)) # self.gui_switch_floorlamp = devices.nodered_gui(mqtt_client, topic="gui/ffe_sw_living_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_tradfri_1.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_switch_feedback_floorlamp) # self.main_light_shelly_last = None 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 data != self.main_light_shelly_last: logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) for device in self.__floorlamp_devices__(): device.set_output_0(data) self.main_light_shelly_last = 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) class all_functions(object): def __init__(self, mqtt_client): self.rooms = {} self.__devices__ = None # # first floor east # self.ffe_floor = first_floor_east_floor(mqtt_client) self.ffe_kitchen = first_floor_east_kitchen(mqtt_client) self.ffe_dining = first_floor_east_dining(mqtt_client) self.ffe_sleep_madi = first_floor_east_sleep_madi(mqtt_client) self.ffe_living = first_floor_east_living(mqtt_client) def ffe_off(self): self.ffe_floor.off() self.ffe_kitchen.off() self.ffe_dining.off() self.ffe_sleep_madi.off() self.ffe_living.off() def devicelist(self): raise Exception # TODO: generate list by using getattr if self.__devices__ is None: self.__devices__ = [] for room in self.rooms: for name, obj in inspect.getmembers(room): if type(obj) in devices.DEVICE_TYPE_LIST(): self.__devices__.append(obj) return self.__devices__