From 3ebfe2f3a532f014c6f1a9f701633b854a3aa78e Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Fri, 23 Dec 2022 11:45:00 +0100 Subject: [PATCH] minor changes - esthetic --- devices/__init__.py | 13 ++++++++++ function/first_floor_east.py | 8 +++---- function/ground_floor_west.py | 45 +++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/devices/__init__.py b/devices/__init__.py index faafc17..2aef3b5 100644 --- a/devices/__init__.py +++ b/devices/__init__.py @@ -78,6 +78,8 @@ class base(dict): # self.callback_list = [] self.warning_callback = None + # + self.__previous__ = {} def receive_callback(self, client, userdata, message): self.unpack(message) @@ -93,6 +95,7 @@ class base(dict): prev_value = self.get(key) if key in self.RX_KEYS: self[key] = data + self.__previous__[key] = prev_value # Filter, if needed self.unpack_filter(key) if prev_value != self.get(key): @@ -186,6 +189,9 @@ class base(dict): def warning_text(self, data): return "default warning text - replace parent warning_text function" + def previous_value(self, key): + self.__previous__.get(key) + class shelly(base): KEY_OUTPUT_0 = "relay/0" @@ -317,6 +323,7 @@ class my_powerplug(base): KEY_OUTPUT_2 = "output/3" KEY_OUTPUT_3 = "output/4" KEY_OUTPUT_ALL = "output/all" + KEY_OUTPUT_LIST = [KEY_OUTPUT_0, KEY_OUTPUT_1, KEY_OUTPUT_2, KEY_OUTPUT_3] # TX_TOPIC = 'set' TX_TYPE = base.TX_VALUE @@ -352,6 +359,12 @@ class my_powerplug(base): # # TX # + def set_output(self, key, state): + if key in self.KEY_OUTPUT_LIST: + self.pack(key, state) + else: + logging.error("Unknown key to set the output!") + def set_output_0(self, state): """state: [True, False, 'toggle']""" self.pack(self.KEY_OUTPUT_0, state) diff --git a/function/first_floor_east.py b/function/first_floor_east.py index 149b6e7..81c2233 100644 --- a/function/first_floor_east.py +++ b/function/first_floor_east.py @@ -52,8 +52,8 @@ class first_floor_east_dining(room_shelly): self.floorlamp_powerplug.set_output_0(False) def floorlamp_synchronisation(self, device, key, data): - if self.cvi.changed_here(device.topic, key, data): - logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) + if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None: + logger.info("Syncing \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) self.floorlamp_powerplug.set_output_0(data) def gui_switch_command_floorlamp(self, device, key, data): @@ -218,8 +218,8 @@ class first_floor_east_living(room_shelly_tradfri_light): return rv def floorlamp_synchronisation(self, device, key, data): - if self.cvi.changed_here(device.topic, key, data): - logger.info("Synching \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) + if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None: + logger.info("Syncing \"%s\" floorlamp with main light (%s)", type(self).__name__, str(data)) for device in self.__floorlamp_devices__(): device.set_output_0(data) diff --git a/function/ground_floor_west.py b/function/ground_floor_west.py index aca6c2d..ad87f82 100644 --- a/function/ground_floor_west.py +++ b/function/ground_floor_west.py @@ -161,23 +161,23 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): self.gui_switch_pc_dock.set_feedback(data) def cd_amplifier_synchronisation(self, device, key, data): - if device == self.powerplug_common: - if self.cvi.changed_here(device.topic, key, data): - logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data) - self.powerplug_common.set_output_0(data) + if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None: + logger.info("Syncing \"%s\" amplifier with cd player: %s", type(self).__name__, data) + self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data) def raspi_amplifier_synchronisation(self, device, key, data): - if self.cvi.changed_here(device.topic, key, data): - logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data) - self.powerplug_common.set_output_0(data) + if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None: + logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data) + self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data) def desk_light_switch_action(self, device, key, data): if device == self.button_tradfri: - logger.info("Toggeling \"%s\" desk light to %s", type(self).__name__, not self.powerplug_common.output_1) - self.powerplug_common.set_output_1("toggle") + logger.info("Toggeling \"%s\" desk light to %s", type( + self).__name__, not self.get(self.KEY_POWERPLUG_AMPLIFIER)) + self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, "toggle") else: logger.info("Setting \"%s\" desk light: %s", type(self).__name__, data) - self.powerplug_common.set_output_1(data) + self.powerplug_common.set_output(self.KEY_POWERPLUG_DESK_LIGHT, data) def desk_light_set_gui_params_action(self, device, key, data): if key == devices.nodered_gui.KEY_BRIGHTNESS: @@ -195,27 +195,30 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): def amplifier_switch_action(self, device, key, data): if device == self.button_tradfri: - logger.info("Toggeling \"%s\" amplifier to %s", type(self).__name__, not self.powerplug_common.output_0) - self.powerplug_common.set_output_0("toggle") + logger.info("Toggeling \"%s\" amplifier to %s", type(self).__name__, + not self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER)) + self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, "toggle") else: logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data) - self.powerplug_common.set_output_0(data) + self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data) def cd_player_switch_action(self, device, key, data): if device == self.button_tradfri: - logger.info("Toggeling \"%s\" cd_player to %s", type(self).__name__, not self.powerplug_common.output_2) - self.powerplug_common.set_output_2("toggle") + logger.info("Toggeling \"%s\" cd_player to %s", type(self).__name__, + not self.powerplug_common.get(self.KEY_POWERPLUG_CD_PLAYER)) + self.powerplug_common.set_output(self.KEY_POWERPLUG_CD_PLAYER, "toggle") else: logger.info("Setting \"%s\" cd_player: %s", type(self).__name__, data) - self.powerplug_common.set_output_2(data) + self.powerplug_common.set_output(self.KEY_POWERPLUG_CD_PLAYER, data) def pc_dock_switch_action(self, device, key, data): if device == self.button_tradfri: - logger.info("Toggeling \"%s\" pc_dock to %s", type(self).__name__, not self.powerplug_common.output_3) - self.powerplug_common.set_output_3("toggle") + logger.info("Toggeling \"%s\" pc_dock to %s", type(self).__name__, + not self.powerplug_common.get(self.KEY_POWERPLUG_PC_DOCK)) + self.powerplug_common.set_output(self.KEY_POWERPLUG_PC_DOCK, "toggle") else: logger.info("Setting \"%s\" pc_dock: %s", type(self).__name__, data) - self.powerplug_common.set_output_3(data) + self.powerplug_common.set_output(self.KEY_POWERPLUG_PC_DOCK, data) def device_chooser_action(self, device, key, data): if device == self.main_light_shelly: @@ -243,9 +246,9 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): 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.output_1 + return self.powerplug_common.get(self.KEY_POWERPLUG_DESK_LIGHT) elif state == self.STATE_ACTIVE_DEVICE_AMPLIFIER: - return self.powerplug_common.output_0 + return self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER) def choose_next_device(self, device=None, key=None, data=None): if self.active_device_state is not None: