From 8ba63349fb1baea81ac228d1f90405995b5fc50e Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Wed, 28 Dec 2022 14:52:24 +0100 Subject: [PATCH] bug-fix for brightness_choose_n_action --- devices/__init__.py | 24 ----------------------- function/__init__.py | 8 ++++---- function/first_floor_east.py | 4 ++-- function/ground_floor_west.py | 6 +++--- function/modules.py | 36 +++++++++++++++++------------------ 5 files changed, 27 insertions(+), 51 deletions(-) diff --git a/devices/__init__.py b/devices/__init__.py index 8e1e04e..ec7d3c5 100644 --- a/devices/__init__.py +++ b/devices/__init__.py @@ -215,12 +215,6 @@ 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 # @@ -313,9 +307,6 @@ 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 # @@ -389,18 +380,6 @@ 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 # @@ -511,9 +490,6 @@ 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 54eac65..081019a 100644 --- a/function/__init__.py +++ b/function/__init__.py @@ -59,10 +59,10 @@ class all_functions(object): def init_off_functionality(self): # Off Buttons - self.gui_button_all_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/all/common/off/button") - self.gui_button_gfw_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/gfw/common/off/button") - self.gui_button_ffw_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/ffw/common/off/button") - self.gui_button_ffe_off = devices.nodered_gui_button(self.mqtt_client, topic="gui/ffe/common/off/button") + self.gui_button_all_off = devices.nodered_gui_button(self.mqtt_client, "gui/all/common/off/button") + self.gui_button_gfw_off = devices.nodered_gui_button(self.mqtt_client, "gui/gfw/common/off/button") + self.gui_button_ffw_off = devices.nodered_gui_button(self.mqtt_client, "gui/ffw/common/off/button") + self.gui_button_ffe_off = devices.nodered_gui_button(self.mqtt_client, "gui/ffe/common/off/button") # self.gui_button_all_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.all_off) self.gui_button_gfw_off.add_callback(devices.nodered_gui_button.KEY_STATE, True, self.gfw_off) diff --git a/function/first_floor_east.py b/function/first_floor_east.py index 8a03f9b..154dc8d 100644 --- a/function/first_floor_east.py +++ b/function/first_floor_east.py @@ -63,8 +63,8 @@ class first_floor_east_sleep(room_shelly_tradfri_light): self.button_tradfri = devices.tradfri_button(mqtt_client, config.TOPIC_FFE_SLEEP_INPUT_DEVICE) # self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_FFE_SLEEP_DEVICE_CHOOSER_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) + self.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0) + self.brightness_functions.add(self.bed_light_di_tradfri, self.bed_light_di_tradfri, devices.tradfri_light.KEY_OUTPUT_0) # # Callback initialisation # diff --git a/function/ground_floor_west.py b/function/ground_floor_west.py index 6133079..754f83c 100644 --- a/function/ground_floor_west.py +++ b/function/ground_floor_west.py @@ -77,9 +77,9 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): self.remote_amplifier = devices.remote(mqtt_client, config.TOPIC_GFW_DIRK_AMPLIFIER_REMOTE) # self.brightness_functions = brightness_choose_n_action(mqtt_client, self.button_tradfri, config.TOPIC_GFW_DIRK_DEVICE_CHOOSER_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.brightness_functions.add(self.main_light_tradfri, self.main_light_shelly, devices.shelly.KEY_OUTPUT_0) + self.brightness_functions.add(self.desk_light_tradfri, self.powerplug_common, self.KEY_POWERPLUG_DESK_LIGHT) + self.brightness_functions.add(self.remote_amplifier, self.powerplug_common, self.KEY_POWERPLUG_AMPLIFIER) # self.spotify_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_SPOTIFY) self.mpd_state = devices.audio_status(mqtt_client, config.TOPIC_GFW_DIRK_MPD) diff --git a/function/modules.py b/function/modules.py index 07fda41..388ebbb 100644 --- a/function/modules.py +++ b/function/modules.py @@ -28,46 +28,46 @@ class brightness_choose_n_action(object): 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.brightness_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): + def add(self, brightness_device, callback_device, callback_key): """ - device: A device for brightness function needs to have the following methods: + brightness_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 + callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method: + * .add_callback(key, data or None, callback, on_changes_only) """ - if len(self.device_list) >= len(devices.nodered_gui_leds.RX_KEYS): + if len(self.brightness_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.brightness_device_list.append(brightness_device) + self.callback_device_list.append((callback_device, callback_key)) self.device_states.append(False) - add_callback(self.device_state_action) + callback_device.add_callback(callback_key, True, self.device_state_action, True) + callback_device.add_callback(callback_key, False, self.device_state_action, True) def device_state_action(self, device, key, data): - self.device_states[self.callback_device_list.index(device)] = data + self.device_states[self.callback_device_list.index((device, key))] = data if data is True: - self.active_device_state = self.callback_device_list.index(device) + self.active_device_state = self.callback_device_list.index((device, key)) 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)): + for i in range(0, len(self.brightness_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)) + for i in range(0, len(self.brightness_device_list)): + target_state = (start_value - i - 1) % (len(self.brightness_device_list)) if self.device_states[target_state]: self.active_device_state = target_state self.update_active_device_led() @@ -78,8 +78,8 @@ class brightness_choose_n_action(object): 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)) + for i in range(0, len(self.brightness_device_list)): + target_state = (start_value + i + 1) % (len(self.brightness_device_list)) if self.device_states[target_state]: self.active_device_state = target_state self.update_active_device_led() @@ -89,7 +89,7 @@ class brightness_choose_n_action(object): def brightness_action(self, device, key, data): if self.active_device_state is not None: - target = self.device_list[self.active_device_state] + target = self.brightness_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()