Skip to content

Commit

Permalink
Return API values even if they are hidden, as they are usable
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Oct 15, 2024
1 parent b3b629a commit 67096d5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 94 deletions.
5 changes: 4 additions & 1 deletion ThermiaOnlineAPI/api/ThermiaAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
103 changes: 12 additions & 91 deletions ThermiaOnlineAPI/model/HeatPump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -449,39 +420,16 @@ 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,
)

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]:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -556,35 +501,14 @@ 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,
)

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]:
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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 []
Expand All @@ -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]:
Expand All @@ -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 []
Expand All @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions ThermiaOnlineAPI/tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
23 changes: 21 additions & 2 deletions ThermiaOnlineAPI/tests/test_heat_pumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)


Expand All @@ -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=[],
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 67096d5

Please sign in to comment.