123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- import config
- import devices
- import inspect
- import logging
-
- # TODO: implement bed light dirk fading by input device
- # TODO: implement heating function sleep_madi
- # TODO: implement circulation pump
- # TODO: implement switch off functionality (except of switch off button transportation)
- # TODO: implement garland (incl. day events like sunset, sunrise, ...)
- # TODO: implement existing nodered functionality "dirk" (ground floor west)
- # TODO: implement warning message (incl. fixing all_functions.devicelist
-
- 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 all_functions(object):
- def __init__(self, mqtt_client):
- self.rooms = {}
- self.__devices__ = None
- #
- # ground floor west
- #
- self.gfw_floor = ground_floor_west_floor(mqtt_client)
- self.gfw_marion = ground_floor_west_marion(mqtt_client)
- self.gfw_dirk = ground_floor_west_dirk(mqtt_client)
- #
- # first floor west
- #
- self.ffw_julian = first_floor_west_julian(mqtt_client)
- self.ffw_living = first_floor_west_living(mqtt_client)
- #
- # 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)
- #
- # Input devices sleep_madi
- #
- self.ffe_button_tradfri_sleep = devices.tradfri_button(mqtt_client, topic="zigbee_og_e/input_device/og_east")
- 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
- # 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__
-
-
- 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 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 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": ""}')
-
-
- class ground_floor_west_floor(room_shelly_silvercrest_light):
- # https://shelly1l-84CCA8AD1148
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/floor_eg_w", "gui/gfw_sw_floor",
- "zigbee_eg_w/light/floor_eg_w/a", "gui/gfw_br_floor", "gui/gfw_ct_floor")
- #
- # Callback initialisation
- #
- self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/floor_eg_w/b")
- self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
- None, self.sync_brightness_main_light)
- self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
- None, self.sync_color_temp_main_light)
-
- def send_init_message_main_light(self):
- return super().send_init_message_main_light()
- self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
-
- def sync_brightness_main_light(self, device, key, data):
- self.main_light_tradfri_2.set_brightness(data)
-
- def sync_color_temp_main_light(self, device, key, data):
- self.main_light_tradfri_2.set_color_temp(data)
-
-
- class ground_floor_west_marion(room_shelly):
- # https://shelly1l-E8DB84A1E067
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/marion", "gui/gfw_sw_marion")
-
-
- class ground_floor_west_dirk(room_shelly_tradfri_light):
- # https://shelly1l-3C6105E44F27
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/dirk", "gui/gfw_sw_dirk",
- "zigbee_eg_w/light/dirk", "gui/gfw_br_dirk", "gui/gfw_ct_dirk")
-
-
- class first_floor_west_julian(room_shelly_tradfri_light):
- # http://shelly1l-3C6105E43452
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/julian", "gui/ffw_sw_julian",
- "zigbee_og_e/light/julian", "gui/ffw_br_julian", "gui/ffw_ct_julian")
-
-
- class first_floor_west_living(room_shelly):
- # http://shelly1l-84CCA8ACE6A1
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/living_mika", "gui/ffw_sw_living")
-
-
- 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)
- 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_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)
- #
- # 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_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))
- #
- 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_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
-
- 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)
- 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)
|