diff --git a/devices/__init__.py b/devices/__init__.py index ec7d3c5..8e1e04e 100644 --- a/devices/__init__.py +++ b/devices/__init__.py @@ -215,6 +215,12 @@ class shelly(base): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic) + def add_on_off_callback_0(self, callback): + self.add_callback(self.KEY_OUTPUT_0, None, callback, True) + + def add_on_off_callback_1(self, callback): + self.add_callback(self.KEY_OUTPUT_1, None, callback, True) + # # WARNING CALL # @@ -307,6 +313,9 @@ class silvercrest_powerplug(base): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic) + def add_default_on_off_callback(self, callback): + self.add_callback(self.KEY_OUTPUT_0, None, callback, True) + # # RX # @@ -380,6 +389,18 @@ class my_powerplug(base): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic) + def add_on_off_callback_0(self, callback): + self.add_callback(self.KEY_OUTPUT_0, None, callback, True) + + def add_on_off_callback_1(self, callback): + self.add_callback(self.KEY_OUTPUT_1, None, callback, True) + + def add_on_off_callback_2(self, callback): + self.add_callback(self.KEY_OUTPUT_2, None, callback, True) + + def add_on_off_callback_3(self, callback): + self.add_callback(self.KEY_OUTPUT_3, None, callback, True) + # # RX # @@ -490,6 +511,9 @@ class tradfri_light(base): def __init__(self, mqtt_client, topic): super().__init__(mqtt_client, topic) + def add_on_off_callback_0(self, callback): + self.add_callback(self.KEY_OUTPUT_0, None, callback, True) + def unpack_filter(self, key): if key == self.KEY_BRIGHTNESS: self[key] = (self[key] - 1) * 100 / 254 diff --git a/function/__init__.py b/function/__init__.py index 08c54ba..877e62a 100644 --- a/function/__init__.py +++ b/function/__init__.py @@ -15,7 +15,7 @@ except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) -# TODO: usage of implementation strategy from gfw_dirk for ffe_sleep +# TODO: topics and parameters -> config.py # TODO: implement devices.nodered_gui_timer (for circulation pump) # implement devices.nodered_gui_heatvalve incl. setpoint, boost, ... and functions from funtion.module # improve devices.brennenstuhl_heatvalve diff --git a/function/first_floor_east.py b/function/first_floor_east.py index 4770d3c..5e500a4 100644 --- a/function/first_floor_east.py +++ b/function/first_floor_east.py @@ -4,7 +4,7 @@ import config import devices -import json +from function.modules import brightness_choose_n_action import logging from function.rooms import room_shelly, room_shelly_motion_sensor, room_shelly_tradfri_light try: @@ -59,20 +59,20 @@ class first_floor_east_sleep(room_shelly_tradfri_light): # self.gui_switch_bed_light_di = devices.nodered_gui_switch(mqtt_client, "gui/ffe/sleep/bed_light_di/switch") self.gui_br_ct_bed_light_di = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/ffe/sleep/bed_light_di/br_ct") - self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, "gui/ffe/sleep/active_device_state/led") # self.button_tradfri = devices.tradfri_button(mqtt_client, "zigbee/ffe/sleep/input_device") # + self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, "gui/ffe/sleep/active_device_state/led") + self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly.add_on_off_callback_0) + self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri.add_on_off_callback_0) + # # Callback initialisation # - # button + # on/off with button self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE, self.main_light_shelly.toggle_output_0_mcb) - self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, + self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT, self.bed_light_di_tradfri.toggle_output_0_mcb) - self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, - self.bed_light_di_tradfri.toggle_output_0_mcb) - self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.fade_light) # bed light # switch @@ -85,45 +85,10 @@ class first_floor_east_sleep(room_shelly_tradfri_light): self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_OUTPUT_0, None, self.gui_br_ct_bed_light_di.set_enable_mcb) self.bed_light_di_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_ct_bed_light_di.set_brightness_mcb) - # fade function - 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 all_off(self, device=None, key=None, data=None): super().all_off(device, key, data) self.bed_light_di_tradfri.set_output_0(False) - 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.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_0, self.last_activated_device == self.main_light_shelly.topic) - self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_1, 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): diff --git a/function/ground_floor_west.py b/function/ground_floor_west.py index b2e404e..f206875 100644 --- a/function/ground_floor_west.py +++ b/function/ground_floor_west.py @@ -3,6 +3,7 @@ # import devices +from function.modules import brightness_choose_n_action import logging from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light import task @@ -72,7 +73,11 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): self.gui_switch_pc_dock = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/pc_dock/switch") # self.remote_amplifier = devices.remote(mqtt_client, "my_apps/gfw/dirk/remote/RAS5") - self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, "gui/gfw/dirk/active_device_state/led") + # + self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, "gui/gfw/dirk/active_device_state/led") + self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly.add_on_off_callback_0) + self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common.add_on_off_callback_1) + self.brightness_functions.add(self.remote_amplifier, self.powerplug_common.add_on_off_callback_0) # self.spotify_state = devices.audio_status(mqtt_client, "my_apps/gfw/dirk/hifi/spotify") self.mpd_state = devices.audio_status(mqtt_client, "my_apps/gfw/dirk/hifi/mpd") @@ -129,15 +134,6 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): self.gui_switch_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb) self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_switch_pc_dock.set_state_mcb) - # brightness - self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.brightness_action) - self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.device_chooser_action, True) - self.powerplug_common.add_callback(None, None, self.device_chooser_action, True) - self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device) - self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device) - self.active_device_state = None - self.update_active_device_led() - def all_off(self, device=None, key=None, data=None): super().all_off(device, key, data) self.powerplug_common.set_output_all(False) @@ -150,83 +146,6 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data) self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data) - def device_chooser_action(self, device, key, data): - if device == self.main_light_shelly: - if data is True: - self.active_device_state = self.STATE_ACTIVE_DEVICE_MAIN_LIGHT - self.update_active_device_led() - else: - self.choose_next_device() - elif device == self.powerplug_common and key == self.KEY_POWERPLUG_DESK_LIGHT: - if data is True: - self.active_device_state = self.STATE_ACTIVE_DEVICE_DESK_LIGHT - self.update_active_device_led() - else: - self.choose_next_device() - elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER: - if data is True: - self.active_device_state = self.STATE_ACTIVE_DEVICE_AMPLIFIER - self.update_active_device_led() - else: - self.choose_next_device() - - def get_activity_state(self, state): - if state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT: - return self.main_light_shelly.output_0 - elif state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT: - return self.powerplug_common.get(self.KEY_POWERPLUG_DESK_LIGHT) - elif state == self.STATE_ACTIVE_DEVICE_AMPLIFIER: - return self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER) - - def update_active_device_led(self): - self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_AMPLIFIER, self.active_device_state == self.STATE_ACTIVE_DEVICE_AMPLIFIER) - self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_MAIN_LIGHT, self.active_device_state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT) - self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_DESK_LIGHT, self.active_device_state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT) - - def choose_prev_device(self, device=None, key=None, data=None): - if self.active_device_state is not None: - start_value = self.active_device_state - for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1): - target_state = (start_value + i + 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1) - if self.get_activity_state(target_state): - self.active_device_state = target_state - self.update_active_device_led() - return - self.active_device_state = None - self.update_active_device_led() - - def choose_next_device(self, device=None, key=None, data=None): - if self.active_device_state is not None: - start_value = self.active_device_state - for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1): - target_state = (start_value - i - 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1) - if self.get_activity_state(target_state): - self.active_device_state = target_state - self.update_active_device_led() - return - self.active_device_state = None - self.update_active_device_led() - - def brightness_action(self, device, key, data): - if self.active_device_state is not None: - brightness_func = { - self.STATE_ACTIVE_DEVICE_MAIN_LIGHT: self.main_light_tradfri, - self.STATE_ACTIVE_DEVICE_DESK_LIGHT: self.desk_light_tradfri, - self.STATE_ACTIVE_DEVICE_AMPLIFIER: self.remote_amplifier - } - target = brightness_func[self.active_device_state] - if target is None: - logger.error("Not yet implemented") - return - if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG: - logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic) - target.default_inc() - elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG: - logger.info("Decreasing \"%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]: - target.default_stop() - def audio_source_selector(self, device, key, data): if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER: # switch on of cd player diff --git a/function/modules.py b/function/modules.py index 09cc25b..07fda41 100644 --- a/function/modules.py +++ b/function/modules.py @@ -13,6 +13,93 @@ except ImportError: logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) +class brightness_choose_n_action(object): + def __init__(self, mqtt_client, button_tradfri, topic_led): + self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, topic_led) + # brightness change + button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, + devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action) + button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action) + button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, + devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action) + button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, + devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action) + # device change + button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device) + button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device) + # + self.device_list = [] + self.callback_device_list = [] + self.device_states = [] + self.active_device_state = None + self.update_active_device_led() + + def add(self, device, add_callback): + """ + device: A device for brightness function needs to have the following methods: + * .default_inc() + * .default_dec() + * .default_stop() + add_callback: A on/off callback which tages the as argument and + - which calls the callback only on changes + - which calls with the following arguments: device, key, data + """ + if len(self.device_list) >= len(devices.nodered_gui_leds.RX_KEYS): + raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.") + self.device_list.append(device) + self.callback_device_list.append(add_callback.__self__) + self.device_states.append(False) + add_callback(self.device_state_action) + + def device_state_action(self, device, key, data): + self.device_states[self.callback_device_list.index(device)] = data + if data is True: + self.active_device_state = self.callback_device_list.index(device) + self.update_active_device_led() + else: + self.choose_next_device() + + def update_active_device_led(self): + for i in range(0, len(self.device_list)): + self.gui_led_active_device.set_led(devices.nodered_gui_leds.RX_KEYS[i], self.active_device_state == i) + + def choose_prev_device(self, device=None, key=None, data=None): + if self.active_device_state is not None: + start_value = self.active_device_state + for i in range(0, len(self.device_list)): + target_state = (start_value - i - 1) % (len(self.device_list)) + if self.device_states[target_state]: + self.active_device_state = target_state + self.update_active_device_led() + return + self.active_device_state = None + self.update_active_device_led() + + def choose_next_device(self, device=None, key=None, data=None): + if self.active_device_state is not None: + start_value = self.active_device_state + for i in range(0, len(self.device_list)): + target_state = (start_value + i + 1) % (len(self.device_list)) + if self.device_states[target_state]: + self.active_device_state = target_state + self.update_active_device_led() + return + self.active_device_state = None + self.update_active_device_led() + + def brightness_action(self, device, key, data): + if self.active_device_state is not None: + target = self.device_list[self.active_device_state] + if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG: + logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic) + target.default_inc() + elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG: + logger.info("Decreasing \"%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]: + target.default_stop() + + class heating_function_brennenstuhl(object): RETURN_TO_DEFAULT_TIME = 45 * 60 BOOST_TEMP_OFFSET = 5