Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor operational and power statuses #37

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,9 @@ To execute the example file, first run `pip install -r requirements.txt` to inst
| `running_operational_statuses` | List of running operational statuses of the Heat Pump |
| `available_operational_statuses` | List of available operational statuses |
| `available_operational_statuses_map` | Dictionary mapping operational status names to their values |
| `operational_status_auxiliary_heater_3kw` | Auxiliary heater status for 3kw (returns `None` if unavailable) |
| `operational_status_auxiliary_heater_6kw` | Auxiliary heater status for 6kw (returns `None` if unavailable) |
| `operational_status_auxiliary_heater_9kw` | Auxiliary heater status for 9kw (returns `None` if unavailable) |
| `operational_status_auxiliary_heater_12kw` | Auxiliary heater status for 12kw (returns `None` if unavailable) |
| `operational_status_auxiliary_heater_15kw` | Auxiliary heater status for 15kw (returns `None` if unavailable) |
| `operational_status_compressor_status` | Compressor status |
| `operational_status_brine_pump_status` | Brine pump status |
| `operational_status_radiator_pump_status` | Radiator pump status |
| `operational_status_cooling_status` | Cooling status |
| `operational_status_hot_water_status` | Hot water status |
| `operational_status_heating_status` | Heating status |
| `running_power_statuses` | List of running power statuses of the Heat Pump |
| `available_power_statuses` | List of available power statuses |
| `available_power_statuses_map` | Dictionary mapping power status names to their values |
| `operational_status_integral` | Integral |
| `operational_status_pid` | PID |
| --- | --- |
Expand Down
2 changes: 2 additions & 0 deletions ThermiaOnlineAPI/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
REG_INTEGRAL_LSD = "REG_INTEGRAL_LSD"
REG_PID = "REG_PID"

COMP_POWER_STATUS = "COMP_POWER_STATUS"

###############################################################################
# Hot water registers
###############################################################################
Expand Down
200 changes: 120 additions & 80 deletions ThermiaOnlineAPI/model/HeatPump.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
REG_OPER_TIME_IMM3,
REG_PID,
REG_RETURN_LINE,
COMP_POWER_STATUS,
COMP_STATUS,
COMP_STATUS_ITEC,
REG_SUPPLY_LINE,
Expand Down Expand Up @@ -87,6 +88,11 @@ def __init__(self, device_data: dict, api_interface: "ThermiaAPI"):
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()

def update_data(self):
Expand Down Expand Up @@ -126,6 +132,15 @@ def update_data(self):
)
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):
return self.__register_indexes

Expand Down Expand Up @@ -517,6 +532,99 @@ def __get_running_operational_statuses(

return []

def __get_power_statuses_from_operational_status(self) -> Optional[Dict]:
data = self.__get_register_from_operational_status(COMP_POWER_STATUS)

if data is None:
return None

return data.get("valueNames", [])

def __get_all_power_statuses_from_power_status(
self,
) -> Optional[ChainMap]:
data = self.__power_statuses

if data is None:
return ChainMap()

power_modes_map = map(
lambda values: {
values.get("value"): {
"name": values.get("name").split("COMP_VALUE_STEP_")[1],
"visible": values.get("visible"),
}
},
data,
)

power_modes_list = list(power_modes_map)
return ChainMap(*power_modes_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_modes_map = map(
lambda item: (
{item[0]: item[1].get("name")} if item[1].get("visible") else {}
),
data.items(),
)

power_modes_list = list(filter(lambda x: x != {}, power_modes_map))
return ChainMap(*power_modes_list)

def __get_running_power_statuses(
self,
) -> List[str]:
data = self.__get_register_from_operational_status(COMP_POWER_STATUS)

if data is None:
return []

current_register_value = get_dict_value_or_none(data, "registerValue")

data = self.__all_power_statuses_map

if data is None:
return []

data_items_list = list(data.items())

current_power_status = [
value.get("name")
for key, value in data_items_list
if key == current_register_value
]

if len(current_power_status) == 1:
return current_power_status

if (
len(current_power_status) != 1
and current_register_value > 0
and len(data_items_list) > 1
):
# Attempt to get multiple statuses by binary sum of the values
data_items_list.sort(key=lambda x: x[0], reverse=True)
list_of_current_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"))

if current_register_value == 0:
return list_of_current_power_statuses

return []

@property
def name(self):
return get_dict_value_or_none(self.__info, "name")
Expand Down Expand Up @@ -699,94 +807,26 @@ def available_operational_statuses_map(self) -> Optional[ChainMap]:
return self.__visible_operational_statuses_map

@property
def operational_status_auxiliary_heater_3kw(self):
return self.__get_value_by_key_and_register_name_from_operational_status(
"COMP_POWER_STATUS", "COMP_VALUE_STEP_3KW"
)

@property
def operational_status_auxiliary_heater_6kw(self):
return self.__get_value_by_key_and_register_name_from_operational_status(
"COMP_POWER_STATUS", "COMP_VALUE_STEP_6KW"
)

@property
def operational_status_auxiliary_heater_9kw(self):
return self.__get_value_by_key_and_register_name_from_operational_status(
"COMP_POWER_STATUS", "COMP_VALUE_STEP_9KW"
)

@property
def operational_status_auxiliary_heater_12kw(self):
return self.__get_value_by_key_and_register_name_from_operational_status(
"COMP_POWER_STATUS", "COMP_VALUE_STEP_12KW"
)

@property
def operational_status_auxiliary_heater_15kw(self):
return self.__get_value_by_key_and_register_name_from_operational_status(
"COMP_POWER_STATUS", "COMP_VALUE_STEP_15KW"
)

@property
def operational_status_compressor_status(self) -> Optional[bool]:
if (
self.__visible_operational_statuses_map is None
or "COMPR" not in self.__visible_operational_statuses_map.values()
):
return None

return "COMPR" in self.running_operational_statuses

@property
def operational_status_brine_pump_status(self) -> Optional[bool]:
if (
self.__visible_operational_statuses_map is None
or "BRINEPUMP" not in self.__visible_operational_statuses_map.values()
):
return None

return "BRINEPUMP" in self.running_operational_statuses
def running_power_statuses(self) -> List[str]:
data = self.__running_power_statuses

@property
def operational_status_radiator_pump_status(self) -> Optional[bool]:
if (
self.__visible_operational_statuses_map is None
or "RADIATORPUMP" not in self.__visible_operational_statuses_map.values()
):
return None
if data is None:
return []

return "RADIATORPUMP" in self.running_operational_statuses
return data

@property
def operational_status_cooling_status(self) -> Optional[bool]:
if (
self.__visible_operational_statuses_map is None
or "COOLING" not in self.__visible_operational_statuses_map.values()
):
return None

return "COOLING" in self.running_operational_statuses
def available_power_statuses(self) -> Optional[List[str]]:
data = self.__visible_power_statuses_map

@property
def operational_status_hot_water_status(self) -> Optional[bool]:
if (
self.__visible_operational_statuses_map is None
or "HOT_WATER" not in self.__visible_operational_statuses_map.values()
):
return None
if data is None:
return []

return "HOT_WATER" in self.running_operational_statuses
return list(data.values())

@property
def operational_status_heating_status(self) -> Optional[bool]:
if (
self.__visible_operational_statuses_map is None
or "HEATING" not in self.__visible_operational_statuses_map.values()
):
return None

return "HEATING" in self.running_operational_statuses
def available_power_statuses_map(self) -> Optional[ChainMap]:
return self.__visible_power_statuses_map

@property
def operational_status_integral(self):
Expand Down
24 changes: 9 additions & 15 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,16 @@
"Available operational statuses map: "
+ str(heat_pump.available_operational_statuses_map)
)
print("Auxiliary heater 3KW: " + str(heat_pump.operational_status_auxiliary_heater_3kw))
print("Auxiliary heater 6KW: " + str(heat_pump.operational_status_auxiliary_heater_6kw))
print("Auxiliary heater 9KW: " + str(heat_pump.operational_status_auxiliary_heater_9kw))
print(
"Auxiliary heater 12KW: " + str(heat_pump.operational_status_auxiliary_heater_12kw)
)
print(
"Auxiliary heater 15KW: " + str(heat_pump.operational_status_auxiliary_heater_15kw)
)

print("Compressor status: " + str(heat_pump.operational_status_compressor_status))
print("Brine pump status: " + str(heat_pump.operational_status_brine_pump_status))
print("Radiator pump status: " + str(heat_pump.operational_status_radiator_pump_status))
print("Cooling status: " + str(heat_pump.operational_status_cooling_status))
print("Hot water status: " + str(heat_pump.operational_status_hot_water_status))
print("Heating status: " + str(heat_pump.operational_status_heating_status))
print("\n")

print("Power status")
print("Running power statuses: " + str(heat_pump.running_power_statuses))
print("Available power statuses: " + str(heat_pump.available_power_statuses))
print("Available power statuses map: " + str(heat_pump.available_power_statuses_map))

print("\n")

print("Integral: " + str(heat_pump.operational_status_integral))
print("Pid: " + str(heat_pump.operational_status_pid))

Expand Down