Skip to content

Commit

Permalink
Split and refactor AirzoneEntity (#70421)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noltari committed Apr 23, 2022
1 parent 5ffaa70 commit 6dc4486
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 47 deletions.
14 changes: 11 additions & 3 deletions homeassistant/components/airzone/__init__.py
Expand Up @@ -30,6 +30,14 @@
class AirzoneEntity(CoordinatorEntity[AirzoneUpdateCoordinator]):
"""Define an Airzone entity."""

def get_airzone_value(self, key) -> Any:
"""Return Airzone entity value by key."""
raise NotImplementedError()


class AirzoneZoneEntity(AirzoneEntity):
"""Define an Airzone Zone entity."""

def __init__(
self,
coordinator: AirzoneUpdateCoordinator,
Expand All @@ -47,12 +55,12 @@ def __init__(
self._attr_device_info: DeviceInfo = {
"identifiers": {(DOMAIN, f"{entry.entry_id}_{system_zone_id}")},
"manufacturer": MANUFACTURER,
"model": self.get_zone_value(AZD_THERMOSTAT_MODEL),
"model": self.get_airzone_value(AZD_THERMOSTAT_MODEL),
"name": f"Airzone [{system_zone_id}] {zone_data[AZD_NAME]}",
"sw_version": self.get_zone_value(AZD_THERMOSTAT_FW),
"sw_version": self.get_airzone_value(AZD_THERMOSTAT_FW),
}

def get_zone_value(self, key):
def get_airzone_value(self, key) -> Any:
"""Return zone value by key."""
value = None
if self.system_zone_id in self.coordinator.data[AZD_ZONES]:
Expand Down
46 changes: 27 additions & 19 deletions homeassistant/components/airzone/binary_sensor.py
Expand Up @@ -24,7 +24,7 @@
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import AirzoneEntity
from . import AirzoneEntity, AirzoneZoneEntity
from .const import DOMAIN
from .coordinator import AirzoneUpdateCoordinator

Expand All @@ -36,7 +36,7 @@ class AirzoneBinarySensorEntityDescription(BinarySensorEntityDescription):
attributes: dict[str, str] | None = None


BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
ZONE_BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
AirzoneBinarySensorEntityDescription(
device_class=BinarySensorDeviceClass.RUNNING,
key=AZD_AIR_DEMAND,
Expand Down Expand Up @@ -65,12 +65,12 @@ async def async_setup_entry(
"""Add Airzone binary sensors from a config_entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]

binary_sensors = []
binary_sensors: list[AirzoneBinarySensor] = []
for system_zone_id, zone_data in coordinator.data[AZD_ZONES].items():
for description in BINARY_SENSOR_TYPES:
for description in ZONE_BINARY_SENSOR_TYPES:
if description.key in zone_data:
binary_sensors.append(
AirzoneBinarySensor(
AirzoneZoneBinarySensor(
coordinator,
description,
entry,
Expand All @@ -83,7 +83,28 @@ async def async_setup_entry(


class AirzoneBinarySensor(AirzoneEntity, BinarySensorEntity):
"""Define an Airzone sensor."""
"""Define an Airzone binary sensor."""

entity_description: AirzoneBinarySensorEntityDescription

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return state attributes."""
if not self.entity_description.attributes:
return None
return {
key: self.get_airzone_value(val)
for key, val in self.entity_description.attributes.items()
}

@property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
return self.get_airzone_value(self.entity_description.key)


class AirzoneZoneBinarySensor(AirzoneZoneEntity, AirzoneBinarySensor):
"""Define an Airzone Zone binary sensor."""

def __init__(
self,
Expand All @@ -97,17 +118,4 @@ def __init__(
super().__init__(coordinator, entry, system_zone_id, zone_data)
self._attr_name = f"{zone_data[AZD_NAME]} {description.name}"
self._attr_unique_id = f"{entry.entry_id}_{system_zone_id}_{description.key}"
self.attributes = description.attributes
self.entity_description = description

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return state attributes."""
if not self.attributes:
return None
return {key: self.get_zone_value(val) for key, val in self.attributes.items()}

@property
def is_on(self) -> bool | None:
"""Return true if the binary sensor is on."""
return self.get_zone_value(self.entity_description.key)
28 changes: 14 additions & 14 deletions homeassistant/components/airzone/climate.py
Expand Up @@ -49,7 +49,7 @@
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import AirzoneEntity
from . import AirzoneZoneEntity
from .const import API_TEMPERATURE_STEP, DOMAIN, TEMP_UNIT_LIB_TO_HASS
from .coordinator import AirzoneUpdateCoordinator

Expand Down Expand Up @@ -97,7 +97,7 @@ async def async_setup_entry(
)


class AirzoneClimate(AirzoneEntity, ClimateEntity):
class AirzoneClimate(AirzoneZoneEntity, ClimateEntity):
"""Define an Airzone sensor."""

def __init__(
Expand All @@ -113,13 +113,13 @@ def __init__(
self._attr_unique_id = f"{entry.entry_id}_{system_zone_id}"
self._attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
self._attr_target_temperature_step = API_TEMPERATURE_STEP
self._attr_max_temp = self.get_zone_value(AZD_TEMP_MAX)
self._attr_min_temp = self.get_zone_value(AZD_TEMP_MIN)
self._attr_max_temp = self.get_airzone_value(AZD_TEMP_MAX)
self._attr_min_temp = self.get_airzone_value(AZD_TEMP_MIN)
self._attr_temperature_unit = TEMP_UNIT_LIB_TO_HASS[
self.get_zone_value(AZD_TEMP_UNIT)
self.get_airzone_value(AZD_TEMP_UNIT)
]
self._attr_hvac_modes = [
HVAC_MODE_LIB_TO_HASS[mode] for mode in self.get_zone_value(AZD_MODES)
HVAC_MODE_LIB_TO_HASS[mode] for mode in self.get_airzone_value(AZD_MODES)
]
self._async_update_attrs()

Expand Down Expand Up @@ -161,8 +161,8 @@ async def async_set_hvac_mode(self, hvac_mode: str) -> None:
params[API_ON] = 0
else:
mode = HVAC_MODE_HASS_TO_LIB[hvac_mode]
if mode != self.get_zone_value(AZD_MODE):
if self.get_zone_value(AZD_MASTER):
if mode != self.get_airzone_value(AZD_MODE):
if self.get_airzone_value(AZD_MASTER):
params[API_MODE] = mode
else:
raise HomeAssistantError(
Expand All @@ -187,16 +187,16 @@ def _handle_coordinator_update(self) -> None:
@callback
def _async_update_attrs(self) -> None:
"""Update climate attributes."""
self._attr_current_temperature = self.get_zone_value(AZD_TEMP)
self._attr_current_humidity = self.get_zone_value(AZD_HUMIDITY)
if self.get_zone_value(AZD_ON):
mode = self.get_zone_value(AZD_MODE)
self._attr_current_temperature = self.get_airzone_value(AZD_TEMP)
self._attr_current_humidity = self.get_airzone_value(AZD_HUMIDITY)
if self.get_airzone_value(AZD_ON):
mode = self.get_airzone_value(AZD_MODE)
self._attr_hvac_mode = HVAC_MODE_LIB_TO_HASS[mode]
if self.get_zone_value(AZD_DEMAND):
if self.get_airzone_value(AZD_DEMAND):
self._attr_hvac_action = HVAC_ACTION_LIB_TO_HASS[mode]
else:
self._attr_hvac_action = CURRENT_HVAC_IDLE
else:
self._attr_hvac_action = CURRENT_HVAC_OFF
self._attr_hvac_mode = HVAC_MODE_OFF
self._attr_target_temperature = self.get_zone_value(AZD_TEMP_SET)
self._attr_target_temperature = self.get_airzone_value(AZD_TEMP_SET)
26 changes: 15 additions & 11 deletions homeassistant/components/airzone/sensor.py
Expand Up @@ -16,11 +16,11 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import AirzoneEntity
from . import AirzoneEntity, AirzoneZoneEntity
from .const import DOMAIN, TEMP_UNIT_LIB_TO_HASS
from .coordinator import AirzoneUpdateCoordinator

SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
ZONE_SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
device_class=SensorDeviceClass.TEMPERATURE,
key=AZD_TEMP,
Expand All @@ -44,12 +44,12 @@ async def async_setup_entry(
"""Add Airzone sensors from a config_entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]

sensors = []
sensors: list[AirzoneSensor] = []
for system_zone_id, zone_data in coordinator.data[AZD_ZONES].items():
for description in SENSOR_TYPES:
for description in ZONE_SENSOR_TYPES:
if description.key in zone_data:
sensors.append(
AirzoneSensor(
AirzoneZoneSensor(
coordinator,
description,
entry,
Expand All @@ -64,6 +64,15 @@ async def async_setup_entry(
class AirzoneSensor(AirzoneEntity, SensorEntity):
"""Define an Airzone sensor."""

@property
def native_value(self):
"""Return the state."""
return self.get_airzone_value(self.entity_description.key)


class AirzoneZoneSensor(AirzoneZoneEntity, AirzoneSensor):
"""Define an Airzone Zone sensor."""

def __init__(
self,
coordinator: AirzoneUpdateCoordinator,
Expand All @@ -80,10 +89,5 @@ def __init__(

if description.key == AZD_TEMP:
self._attr_native_unit_of_measurement = TEMP_UNIT_LIB_TO_HASS.get(
self.get_zone_value(AZD_TEMP_UNIT)
self.get_airzone_value(AZD_TEMP_UNIT)
)

@property
def native_value(self):
"""Return the state."""
return self.get_zone_value(self.entity_description.key)

0 comments on commit 6dc4486

Please sign in to comment.