Skip to content

Commit

Permalink
Refactor operational statuses and add power statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Oct 6, 2024
1 parent 83876a1 commit e08e784
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 162 deletions.
110 changes: 34 additions & 76 deletions custom_components/thermia/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Thermia binary sensor integration."""

from __future__ import annotations
from typing import List

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from .binary_sensors.operational_status_binary_sensor import (
ThermiaOperationalStatusBinarySensor,
from .binary_sensors.operational_or_power_status_binary_sensor import (
ThermiaOperationalOrPowerStatusBinarySensor,
)
from ThermiaOnlineAPI import ThermiaHeatPump

from .const import (
DOMAIN,
Expand All @@ -27,83 +29,39 @@ async def async_setup_entry(

hass_thermia_binary_sensors = []

for idx, heat_pump in enumerate(coordinator.data.heat_pumps):
if heat_pump.operational_status_compressor_status is not None:
hass_thermia_binary_sensors.append(
ThermiaOperationalStatusBinarySensor(
coordinator,
idx,
"is_online",
"Compressor Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
"operational_status_compressor_status",
)
)

if heat_pump.operational_status_brine_pump_status is not None:
hass_thermia_binary_sensors.append(
ThermiaOperationalStatusBinarySensor(
coordinator,
idx,
"is_online",
"Brine Pump Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
"operational_status_brine_pump_status",
)
)

if heat_pump.operational_status_radiator_pump_status is not None:
hass_thermia_binary_sensors.append(
ThermiaOperationalStatusBinarySensor(
coordinator,
idx,
"is_online",
"Radiator Pump Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
"operational_status_radiator_pump_status",
)
)

if heat_pump.operational_status_cooling_status is not None:
hass_thermia_binary_sensors.append(
ThermiaOperationalStatusBinarySensor(
coordinator,
idx,
"is_online",
"Cooling Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
"operational_status_cooling_status",
)
)
heat_pumps: List[ThermiaHeatPump] = coordinator.data.heat_pumps

if heat_pump.operational_status_hot_water_status is not None:
hass_thermia_binary_sensors.append(
ThermiaOperationalStatusBinarySensor(
coordinator,
idx,
"is_online",
"Hot Water Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
"operational_status_hot_water_status",
for idx, heat_pump in enumerate(heat_pumps):
if heat_pump.available_operational_statuses is not None and heat_pump.running_operational_statuses is not None:
for operational_status in heat_pump.available_operational_statuses:
name = operational_status.replace("_", " ").title()
hass_thermia_binary_sensors.append(
ThermiaOperationalOrPowerStatusBinarySensor(
coordinator,
idx,
"is_online",
f"{name} Operational Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
operational_status,
"running_operational_statuses"
)
)
)

if heat_pump.operational_status_heating_status is not None:
hass_thermia_binary_sensors.append(
ThermiaOperationalStatusBinarySensor(
coordinator,
idx,
"is_online",
"Heating Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
"operational_status_heating_status",
if heat_pump.available_power_statuses is not None and heat_pump.running_power_statuses is not None:
for power_status in heat_pump.available_power_statuses:
name = power_status.replace("_", " ").title()
hass_thermia_binary_sensors.append(
ThermiaOperationalOrPowerStatusBinarySensor(
coordinator,
idx,
"is_online",
f"{name} Power Status",
MDI_INFORMATION_OUTLINE_ICON,
BinarySensorDeviceClass.RUNNING,
power_status,
"running_power_statuses"
)
)
)

async_add_entities(hass_thermia_binary_sensors)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from ..const import DOMAIN


class ThermiaOperationalStatusBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of an Thermia Operational Status binary sensor."""
class ThermiaOperationalOrPowerStatusBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of an Thermia Operational or Power Status binary sensor."""

def __init__(
self,
Expand All @@ -19,7 +19,8 @@ def __init__(
binary_sensor_name,
mdi_icon,
device_class,
value_prop,
status_value,
running_status_list,
):
super().__init__(coordinator)
self.idx = idx
Expand All @@ -28,7 +29,8 @@ def __init__(
self._binary_sensor_name = binary_sensor_name
self._mdi_icon = mdi_icon
self._device_class = device_class
self._value_prop = value_prop
self._status_value = status_value
self._running_status_list = running_status_list

@property
def available(self):
Expand Down Expand Up @@ -71,4 +73,4 @@ def is_on(self):
"""Return the state of the sensor."""
heat_pump = self.coordinator.data.heat_pumps[self.idx]

return getattr(heat_pump, self._value_prop)
return self._status_value in getattr(heat_pump, self._running_status_list)
85 changes: 4 additions & 81 deletions custom_components/thermia/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,86 +256,6 @@ async def async_setup_entry(
# Operational status data
###########################################################################

if heat_pump.operational_status_auxiliary_heater_3kw is not None:
hass_thermia_sensors.append(
ThermiaGenericSensor(
coordinator,
idx,
"is_online",
"Auxiliary Heater 3KW",
MDI_INFORMATION_OUTLINE_ICON,
EntityCategory.DIAGNOSTIC,
None,
"measurement",
"operational_status_auxiliary_heater_3kw",
None,
)
)

if heat_pump.operational_status_auxiliary_heater_6kw is not None:
hass_thermia_sensors.append(
ThermiaGenericSensor(
coordinator,
idx,
"is_online",
"Auxiliary Heater 6KW",
MDI_INFORMATION_OUTLINE_ICON,
EntityCategory.DIAGNOSTIC,
None,
"measurement",
"operational_status_auxiliary_heater_6kw",
None,
)
)

if heat_pump.operational_status_auxiliary_heater_9kw is not None:
hass_thermia_sensors.append(
ThermiaGenericSensor(
coordinator,
idx,
"is_online",
"Auxiliary Heater 9KW",
MDI_INFORMATION_OUTLINE_ICON,
EntityCategory.DIAGNOSTIC,
None,
"measurement",
"operational_status_auxiliary_heater_9kw",
None,
)
)

if heat_pump.operational_status_auxiliary_heater_12kw is not None:
hass_thermia_sensors.append(
ThermiaGenericSensor(
coordinator,
idx,
"is_online",
"Auxiliary Heater 12KW",
MDI_INFORMATION_OUTLINE_ICON,
EntityCategory.DIAGNOSTIC,
None,
"measurement",
"operational_status_auxiliary_heater_12kw",
None,
)
)

if heat_pump.operational_status_auxiliary_heater_15kw is not None:
hass_thermia_sensors.append(
ThermiaGenericSensor(
coordinator,
idx,
"is_online",
"Auxiliary Heater 15KW",
MDI_INFORMATION_OUTLINE_ICON,
EntityCategory.DIAGNOSTIC,
None,
"measurement",
"operational_status_auxiliary_heater_15kw",
None,
)
)

if heat_pump.operational_status_integral is not None:
hass_thermia_sensors.append(
ThermiaGenericSensor(
Expand Down Expand Up @@ -457,4 +377,7 @@ async def async_setup_entry(
for idx, _ in enumerate(coordinator.data.heat_pumps)
]

async_add_entities([*hass_thermia_active_alarms_sensors, *hass_thermia_sensors])
async_add_entities([
*hass_thermia_active_alarms_sensors,
*hass_thermia_sensors
])

0 comments on commit e08e784

Please sign in to comment.