diff --git a/devices/__init__.py b/devices/__init__.py index 64fbd54..aca6454 100644 --- a/devices/__init__.py +++ b/devices/__init__.py @@ -380,7 +380,7 @@ class tradfri_light(base): def unpack_filter(self, key): if key == self.KEY_BRIGHTNESS: - self[key] = self[key] * 100 / 256 + self[key] = (self[key] - 1) * 100 / 254 elif key == self.KEY_COLOR_TEMP: self[key] = (self[key] - 250) * 100 / 204 else: @@ -388,7 +388,7 @@ class tradfri_light(base): def pack_filter(self, key, data): if key == self.KEY_BRIGHTNESS: - return data * 256 / 100 + return data * 254 / 100 + 1 elif key == self.KEY_COLOR_TEMP: return data * 204 / 100 + 250 else: @@ -400,22 +400,22 @@ class tradfri_light(base): @property def output_0(self): """rv: [True, False]""" - return self.get(self.KEY_OUTPUT_0) + return self.get(self.KEY_OUTPUT_0, False) @property def linkquality(self): """rv: numeric value""" - return self.get(self.KEY_LINKQUALITY) + return self.get(self.KEY_LINKQUALITY, 0) @property def brightness(self): """rv: numeric value [0%, ..., 100%""" - return self.get(self.KEY_BRIGHTNESS) + return self.get(self.KEY_BRIGHTNESS, 0) @property def color_temp(self): """rv: numeric value [0%, ..., 100%""" - return self.get(self.KEY_COLOR_TEMP) + return self.get(self.KEY_COLOR_TEMP, 0) # # TX @@ -467,15 +467,18 @@ class tradfri_button(base): class nodered_gui(base): KEY_FEEDBACK = "feedback" + KEY_ENABLE = "enable" KEY_STATE = "state" + KEY_BRIGHTNESS = "brightness" + KEY_COLOR_TEMP = "color_temp" # TX_TOPIC = 'set' TX_TYPE = base.TX_VALUE TX_FILTER_DATA_KEYS = [] # RX_LOG_INFO_ALWAYS_KEYS = [] - RX_KEYS = [KEY_STATE] - RX_IGNORE_TOPICS = [KEY_FEEDBACK + '/' + TX_TOPIC] + RX_KEYS = [KEY_STATE, KEY_BRIGHTNESS, KEY_COLOR_TEMP] + RX_IGNORE_TOPICS = [KEY_FEEDBACK + '/' + TX_TOPIC, KEY_ENABLE + '/' + TX_TOPIC] RX_FILTER_DATA_KEYS = [] def __init__(self, mqtt_client, topic): @@ -489,8 +492,22 @@ class nodered_gui(base): """rv: [True, False]""" return self.get(self.KEY_STATE) + @property + def brightness(self): + """rv: [0, ..., 100]""" + return self.get(self.KEY_BRIGHTNESS) + + @property + def color_temp(self): + """rv: [0, ..., 100]""" + return self.get(self.KEY_COLOR_TEMP) + # # TX # def set_feedback(self, data): self.pack(self.KEY_FEEDBACK, data) + + def enable(self, data): + """data: [True, False]""" + self.pack(self.KEY_ENABLE, data) diff --git a/function/__init__.py b/function/__init__.py index f44fd53..34e1aa4 100644 --- a/function/__init__.py +++ b/function/__init__.py @@ -12,11 +12,6 @@ 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): @@ -43,11 +38,64 @@ class room_shelly(room): self.main_light_shelly.set_output_0(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) + + 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") + 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") + class first_floor_east_kitchen(room_shelly): # TODO: add circulation pump (switch, time) @@ -89,32 +137,89 @@ class first_floor_east_dining(room_shelly): self.gui_switch_floorlamp.set_feedback(data) -class first_floor_east_sleep_madi(room_shelly): +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") - self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/sleep_madi") + 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.button_tradfri = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east") + # + 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) + # + # Callback initialisation + # + 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 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 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") -class first_floor_east_living(room_shelly): +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") - self.main_light_tradfri = devices.tradfri_light(mqtt_client, topic="zigbee_og_e/light/livingroom") + 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)) # 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) # # 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_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) # self.main_light_shelly_last = None @@ -138,6 +243,30 @@ class first_floor_east_living(room_shelly): 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) class all_functions(object): @@ -152,13 +281,21 @@ class all_functions(object): 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() + # + # Input devices and Off Buttons + # + self.ffe_button_tradfri_sleep = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east") + # + # Callback initialisation + # + self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "toggle", + self.ffe_sleep_madi.toggle_main_light) + self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "brightness_up_click", + self.ffe_sleep_madi.toggle_bed_light_di) + self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "brightness_down_click", + self.ffe_sleep_madi.toggle_bed_light_di) + self.ffe_button_tradfri_sleep.add_callback(devices.tradfri_button.KEY_ACTION, "arrow_right_click", + self.ffe_floor.toggle_main_light) def devicelist(self): raise Exception