From 67096d500287ef1773f1f4240e4bde9a9c529409 Mon Sep 17 00:00:00 2001 From: Krisjanis Lejejs Date: Tue, 15 Oct 2024 23:55:15 +0300 Subject: [PATCH] Return API values even if they are hidden, as they are usable --- ThermiaOnlineAPI/api/ThermiaAPI.py | 5 +- ThermiaOnlineAPI/model/HeatPump.py | 103 +++------------------- ThermiaOnlineAPI/tests/setup.py | 11 +++ ThermiaOnlineAPI/tests/test_heat_pumps.py | 23 ++++- 4 files changed, 48 insertions(+), 94 deletions(-) diff --git a/ThermiaOnlineAPI/api/ThermiaAPI.py b/ThermiaOnlineAPI/api/ThermiaAPI.py index 8874eff..d0f0d2a 100644 --- a/ThermiaOnlineAPI/api/ThermiaAPI.py +++ b/ThermiaOnlineAPI/api/ThermiaAPI.py @@ -45,6 +45,9 @@ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" } +# Fix for multiple operation modes with the same value +REG_OPERATIONMODE_SKIP_VALUES = ["REG_VALUE_OPERATION_MODE_SERVICE"] + class ThermiaAPI: def __init__(self, email, password): @@ -302,7 +305,7 @@ def __get_group_operational_operation_from_register_group( "REG_VALUE_OPERATION_MODE_" )[1], } - if values.get("visible") + if values.get("name") not in REG_OPERATIONMODE_SKIP_VALUES else {} ), operation_modes_data, diff --git a/ThermiaOnlineAPI/model/HeatPump.py b/ThermiaOnlineAPI/model/HeatPump.py index c630b56..c59dff3 100644 --- a/ThermiaOnlineAPI/model/HeatPump.py +++ b/ThermiaOnlineAPI/model/HeatPump.py @@ -86,12 +86,10 @@ def __init__(self, device_data: dict, api_interface: "ThermiaAPI"): self.__operational_statuses = None self.__all_operational_statuses_map = None - self.__visible_operational_statuses_map = None self.__running_operational_statuses = None self.__power_statuses = None self.__all_power_statuses_map = None - self.__visible_power_statuses_map = None self.__running_power_statuses = None self.update_data() @@ -131,18 +129,12 @@ def update_data(self): self.__all_operational_statuses_map = ( self.__get_all_operational_statuses_from_operational_status() ) - self.__visible_operational_statuses_map = ( - self.__get_all_visible_operational_statuses_from_operational_status() - ) self.__running_operational_statuses = self.__get_running_operational_statuses() self.__power_statuses = self.__get_power_statuses_from_operational_status() self.__all_power_statuses_map = ( self.__get_all_power_statuses_from_power_status() ) - self.__visible_power_statuses_map = ( - self.__get_all_visible_power_statuses_from_power_status() - ) self.__running_power_statuses = self.__get_running_power_statuses() def get_register_indexes(self): @@ -369,27 +361,6 @@ def __get_register_from_operational_status( return data[0] - def __get_value_by_key_and_register_name_from_operational_status( - self, register_name: str, value_key: str - ): - data_from_register = self.__get_register_from_operational_status(register_name) - - if data_from_register is None: - return None - - register_values_list = data_from_register.get("valueNames", []) - values_list: list = [d for d in register_values_list if d["name"] == value_key] - - if len(values_list) != 1: - return None - - value: dict = values_list[0] - - if value.get("visible"): - return value.get("value") - - return None - def __get_operational_statuses_from_operational_status(self) -> Optional[Dict]: if self.__device_config["operational_status_register"] is not None: data = self.__get_register_from_operational_status( @@ -449,12 +420,9 @@ def __get_all_operational_statuses_from_operational_status( operation_statuses_map = map( lambda values: { - values.get("value"): { - "name": values.get("name").split( - self.__device_config["operational_status_valueNamePrefix"] - )[1], - "visible": values.get("visible"), - } + values.get("value"): values.get("name").split( + self.__device_config["operational_status_valueNamePrefix"] + )[1], }, data, ) @@ -462,26 +430,6 @@ def __get_all_operational_statuses_from_operational_status( operation_statuses_list = list(operation_statuses_map) return ChainMap(*operation_statuses_list) - def __get_all_visible_operational_statuses_from_operational_status( - self, - ) -> Optional[ChainMap]: - data = self.__all_operational_statuses_map - - if data is None: - return ChainMap() - - operation_statuses_map = map( - lambda item: ( - {item[0]: item[1].get("name")} if item[1].get("visible") else {} - ), - data.items(), - ) - - operation_statuses_list = list( - filter(lambda x: x != {}, operation_statuses_map) - ) - return ChainMap(*operation_statuses_list) - def __get_running_operational_statuses( self, ) -> List[str]: @@ -505,9 +453,7 @@ def __get_running_operational_statuses( data_items_list = list(data.items()) current_operation_mode = [ - value.get("name") - for key, value in data_items_list - if key == current_register_value + value for key, value in data_items_list if key == current_register_value ] if len(current_operation_mode) == 1: @@ -530,8 +476,7 @@ def __get_running_operational_statuses( for key, value in data_items_list: if key <= current_register_value: current_register_value -= key - if value.get("visible"): - list_of_current_operation_statuses.append(value.get("name")) + list_of_current_operation_statuses.append(value) if current_register_value == 0: return list_of_current_operation_statuses @@ -556,10 +501,7 @@ def __get_all_power_statuses_from_power_status( power_statuses_map = map( lambda values: { - values.get("value"): { - "name": values.get("name").split("COMP_VALUE_STEP_")[1], - "visible": values.get("visible"), - } + values.get("value"): values.get("name").split("COMP_VALUE_STEP_")[1], }, data, ) @@ -567,24 +509,6 @@ def __get_all_power_statuses_from_power_status( power_statuses_list = list(power_statuses_map) return ChainMap(*power_statuses_list) - def __get_all_visible_power_statuses_from_power_status( - self, - ) -> Optional[ChainMap]: - data = self.__all_power_statuses_map - - if data is None: - return ChainMap() - - power_statuses_map = map( - lambda item: ( - {item[0]: item[1].get("name")} if item[1].get("visible") else {} - ), - data.items(), - ) - - power_statuses_list = list(filter(lambda x: x != {}, power_statuses_map)) - return ChainMap(*power_statuses_list) - def __get_running_power_statuses( self, ) -> List[str]: @@ -603,9 +527,7 @@ def __get_running_power_statuses( data_items_list = list(data.items()) current_power_status = [ - value.get("name") - for key, value in data_items_list - if key == current_register_value + value for key, value in data_items_list if key == current_register_value ] if len(current_power_status) == 1: @@ -623,8 +545,7 @@ def __get_running_power_statuses( for key, value in data_items_list: if key <= current_register_value: current_register_value -= key - if value.get("visible"): - list_of_current_power_statuses.append(value.get("name")) + list_of_current_power_statuses.append(value) if current_register_value == 0: return list_of_current_power_statuses @@ -801,7 +722,7 @@ def running_operational_statuses(self) -> List[str]: @property def available_operational_statuses(self) -> Optional[List[str]]: - data = self.__visible_operational_statuses_map + data = self.__all_operational_statuses_map if data is None: return [] @@ -810,7 +731,7 @@ def available_operational_statuses(self) -> Optional[List[str]]: @property def available_operational_statuses_map(self) -> Optional[ChainMap]: - return self.__visible_operational_statuses_map + return self.__all_operational_statuses_map @property def running_power_statuses(self) -> List[str]: @@ -823,7 +744,7 @@ def running_power_statuses(self) -> List[str]: @property def available_power_statuses(self) -> Optional[List[str]]: - data = self.__visible_power_statuses_map + data = self.__all_power_statuses_map if data is None: return [] @@ -832,7 +753,7 @@ def available_power_statuses(self) -> Optional[List[str]]: @property def available_power_statuses_map(self) -> Optional[ChainMap]: - return self.__visible_power_statuses_map + return self.__all_power_statuses_map @property def operational_status_integral(self): diff --git a/ThermiaOnlineAPI/tests/setup.py b/ThermiaOnlineAPI/tests/setup.py index ada1394..4b4d4c1 100644 --- a/ThermiaOnlineAPI/tests/setup.py +++ b/ThermiaOnlineAPI/tests/setup.py @@ -99,16 +99,27 @@ def setup_thermia_and_perform_basic_tests( heat_pump = thermia.heat_pumps[0] + print(heat_pump.model) assert heat_pump.model == expected_model + + print(heat_pump.model_id) assert heat_pump.model_id == expected_model_id + + print(heat_pump.available_operation_modes) assert match_lists_in_any_order( heat_pump.available_operation_modes, expected_available_operational_modes ) + + print(heat_pump.is_operation_mode_read_only) assert heat_pump.is_operation_mode_read_only == expected_is_operation_mode_read_only + + print(heat_pump.available_operational_statuses) assert match_lists_in_any_order( heat_pump.available_operational_statuses, expected_available_operational_statuses, ) + + print(heat_pump.available_power_statuses) assert match_lists_in_any_order( heat_pump.available_power_statuses, expected_available_power_statuses ) diff --git a/ThermiaOnlineAPI/tests/test_heat_pumps.py b/ThermiaOnlineAPI/tests/test_heat_pumps.py index 26eb083..170a7b7 100644 --- a/ThermiaOnlineAPI/tests/test_heat_pumps.py +++ b/ThermiaOnlineAPI/tests/test_heat_pumps.py @@ -17,10 +17,18 @@ def test_diplomat_duo_921(requests_mock): expected_available_operational_statuses=[ "COMPR", "BRINEPUMP", + "RADIATORPUMP", + "COOLING", "HOT_WATER", "HEATING", ], - expected_available_power_statuses=["3KW", "6KW"], + expected_available_power_statuses=[ + "3KW", + "6KW", + "9KW", + "12KW", + "15KW", + ], ) @@ -30,11 +38,20 @@ def test_itec_iq(requests_mock): "iTec_IQ.txt", expected_model="iTec", expected_model_id="IQ", - expected_available_operational_modes=["OFF", "AUTO", "COMPRESSOR", "AUXILIARY"], + expected_available_operational_modes=[ + "OFF", + "AUTO", + "COMPRESSOR", + "AUXILIARY", + "HOT_WATER", + ], expected_available_operational_statuses=[ "COMPR", + "RADIATORPUMP", + "HOT_WATER", "HEATING", "DEFROST", + "POOL", "COOLING", ], expected_available_power_statuses=[], @@ -51,6 +68,7 @@ def test_ncp_1024(requests_mock): expected_is_operation_mode_read_only=True, expected_available_operational_statuses=[ "STATUS_MANUAL", + "STATUS_DEFROST", "STATUS_HOTWATER", "STATUS_HEAT", "STATUS_COOL", @@ -75,6 +93,7 @@ def test_ncp_1028(requests_mock): expected_is_operation_mode_read_only=True, expected_available_operational_statuses=[ "STATUS_MANUAL", + "STATUS_DEFROST", "STATUS_HOTWATER", "STATUS_HEAT", "STATUS_COOL",