From 2f673aa2838d05d5236dc765ee5a4fa7aa5bdc8a Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Mon, 26 Dec 2022 16:54:26 +0100 Subject: [PATCH] clean up --- function/common.py | 8 ++-- function/first_floor_east.py | 3 -- function/first_floor_west.py | 2 +- function/ground_floor_west.py | 79 +++++++++++++++-------------------- function/helpers.py | 13 ------ 5 files changed, 38 insertions(+), 67 deletions(-) diff --git a/function/common.py b/function/common.py index 851eb06..a37acfe 100644 --- a/function/common.py +++ b/function/common.py @@ -5,7 +5,6 @@ import devices from function.rooms import room_shelly from function.modules import heating_function_brennenstuhl -from function.helpers import changed_value_indicator import logging import task @@ -21,22 +20,21 @@ class common_circulation_pump(room_shelly): # http://shelly1-E89F6D85A466 super().__init__(mqtt_client, "shellies/ffe/kitchen/circulation_pump", "gui/none/common/circulation_pump/switch") # - self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.circ_pump_actions) + self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.circ_pump_actions, True) # self.gui_timer_view = devices.nodered_gui_heatvalve(mqtt_client, "gui/ffe_circ_pump_timer") self.gui_timer_view.set_feedback('-') # - self.cvi = changed_value_indicator() self.ct = task.periodic(6, self.cyclic_task) self.pump_timer = None # self.ct.run() def circ_pump_actions(self, device, key, data): - if self.cvi.changed_here(device.topic, key, data) and data is True: + if data is True: self.pump_timer = 10 self.gui_timer_view.set_feedback(self.pump_timer) - elif data is False: + else: self.pump_timer = None self.gui_timer_view.set_feedback('-') diff --git a/function/first_floor_east.py b/function/first_floor_east.py index 5204454..4770d3c 100644 --- a/function/first_floor_east.py +++ b/function/first_floor_east.py @@ -7,7 +7,6 @@ import devices import json import logging from function.rooms import room_shelly, room_shelly_motion_sensor, room_shelly_tradfri_light -from function.helpers import changed_value_indicator try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: @@ -163,8 +162,6 @@ class first_floor_east_living(room_shelly_tradfri_light): self.gui_switch_xmas_tree.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_xmas_tree.set_output_0_mcb) # self.powerplug_xmas_tree.add_callback(devices.silvercrest_powerplug.KEY_OUTPUT_0, None, self.powerplug_xmas_star.set_output_0_mcb) - # - self.cvi = changed_value_indicator() def all_off(self, device=None, key=None, data=None): super().all_off(device, key, data) diff --git a/function/first_floor_west.py b/function/first_floor_west.py index 21fb2c9..02b1dbf 100644 --- a/function/first_floor_west.py +++ b/function/first_floor_west.py @@ -3,7 +3,7 @@ # import logging -from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light +from function.rooms import room_shelly, room_shelly_tradfri_light try: from config import APP_NAME as ROOT_LOGGER_NAME diff --git a/function/ground_floor_west.py b/function/ground_floor_west.py index 9fbc0ef..b2e404e 100644 --- a/function/ground_floor_west.py +++ b/function/ground_floor_west.py @@ -5,7 +5,6 @@ import devices import logging from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light -from function.helpers import changed_value_indicator import task try: @@ -80,8 +79,6 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): # self.delayed_task = task.delayed(1.0, self.send_audio_source) # - self.cvi = changed_value_indicator() - # # Callback initialisation # @@ -110,14 +107,14 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): self.gui_switch_amplifier.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_0_mcb) self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.gui_switch_amplifier.set_state_mcb) # amplifier auto on - self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation) - self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation) - self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation) + self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation, True) + self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation, True) + self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation, True) # audio source - self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.audio_source_selector) - self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.audio_source_selector) - self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector) - self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector) + self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, True, self.audio_source_selector, True) + self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, True, self.audio_source_selector, True) + self.spotify_state.add_callback(devices.status.KEY_STATE, True, self.audio_source_selector, True) + self.mpd_state.add_callback(devices.status.KEY_STATE, True, self.audio_source_selector, True) self.audio_source = self.AUDIO_SOURCE_PC # cd player @@ -134,8 +131,8 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): # 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) - self.powerplug_common.add_callback(None, None, self.device_chooser_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 @@ -146,37 +143,32 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): self.powerplug_common.set_output_all(False) def cd_amplifier_synchronisation(self, device, key, 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) + 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) 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) + 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 self.cvi.changed_here(device.topic, key, data): - if data is True: - self.active_device_state = self.STATE_ACTIVE_DEVICE_MAIN_LIGHT - self.update_active_device_led() - else: - self.choose_next_device() + 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 self.cvi.changed_here(device.topic, key, data): - if data is True: - self.active_device_state = self.STATE_ACTIVE_DEVICE_DESK_LIGHT - self.update_active_device_led() - else: - self.choose_next_device() + 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 self.cvi.changed_here(device.topic, key, data): - if data is True: - self.active_device_state = self.STATE_ACTIVE_DEVICE_AMPLIFIER - self.update_active_device_led() - else: - self.choose_next_device() + 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: @@ -237,17 +229,14 @@ class ground_floor_west_dirk(room_shelly_tradfri_light): def audio_source_selector(self, device, key, data): if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER: - if self.cvi.changed_here(device.topic, key, data) and data is True: - # switch on of cd player - self.audio_source = self.AUDIO_SOURCE_CD + # switch on of cd player + self.audio_source = self.AUDIO_SOURCE_CD elif device in [self.spotify_state, self.mpd_state]: - if self.cvi.changed_here(device.topic, key, data) and data is True: - # switch on raspi-source - self.audio_source = self.AUDIO_SOURCE_RASPI + # switch on raspi-source + self.audio_source = self.AUDIO_SOURCE_RASPI elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER: - if self.cvi.changed_here(device.topic, key, data) and data is True: - # switch on of amplifier -> select source and reset stored source value - self.delayed_task.run() + # switch on of amplifier -> select source and reset stored source value + self.delayed_task.run() def send_audio_source(self): if self.audio_source == self.AUDIO_SOURCE_PC: diff --git a/function/helpers.py b/function/helpers.py index 088c9d0..046f15c 100644 --- a/function/helpers.py +++ b/function/helpers.py @@ -7,19 +7,6 @@ import inspect import time -class changed_value_indicator(dict): - def __init__(self): - super().__init__(self) - - def changed_here(self, topic, key, value): - caller_name = inspect.getmodule(inspect.stack()[1][0]).__name__ + '.' + inspect.stack()[1][3] - key = '::'.join([caller_name, topic, key]) - # - rv = self.get(key) != value - self[key] = value - return rv - - def now(): return time.mktime(time.localtime())