2022-03-27 20:39:46 +00:00
|
|
|
"""Support for the Airzone sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any, Final
|
|
|
|
|
|
|
|
from aioairzone.const import (
|
|
|
|
AZD_AIR_DEMAND,
|
2022-04-23 05:27:23 +00:00
|
|
|
AZD_BATTERY_LOW,
|
2022-03-27 20:39:46 +00:00
|
|
|
AZD_ERRORS,
|
|
|
|
AZD_FLOOR_DEMAND,
|
|
|
|
AZD_NAME,
|
|
|
|
AZD_PROBLEMS,
|
2022-05-09 14:07:11 +00:00
|
|
|
AZD_SYSTEMS,
|
2022-03-27 20:39:46 +00:00
|
|
|
AZD_ZONES,
|
|
|
|
)
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
2022-04-01 18:51:20 +00:00
|
|
|
BinarySensorDeviceClass,
|
2022-03-27 20:39:46 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-05-09 17:56:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-03-27 20:39:46 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import AirzoneUpdateCoordinator
|
2022-05-09 14:07:11 +00:00
|
|
|
from .entity import AirzoneEntity, AirzoneSystemEntity, AirzoneZoneEntity
|
2022-03-27 20:39:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class AirzoneBinarySensorEntityDescription(BinarySensorEntityDescription):
|
|
|
|
"""A class that describes airzone binary sensor entities."""
|
|
|
|
|
|
|
|
attributes: dict[str, str] | None = None
|
|
|
|
|
|
|
|
|
2022-05-09 14:07:11 +00:00
|
|
|
SYSTEM_BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
|
|
|
|
AirzoneBinarySensorEntityDescription(
|
|
|
|
attributes={
|
|
|
|
"errors": AZD_ERRORS,
|
|
|
|
},
|
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
key=AZD_PROBLEMS,
|
|
|
|
name="Problem",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-04-23 05:20:14 +00:00
|
|
|
ZONE_BINARY_SENSOR_TYPES: Final[tuple[AirzoneBinarySensorEntityDescription, ...]] = (
|
2022-03-27 20:39:46 +00:00
|
|
|
AirzoneBinarySensorEntityDescription(
|
2022-04-01 18:51:20 +00:00
|
|
|
device_class=BinarySensorDeviceClass.RUNNING,
|
2022-03-27 20:39:46 +00:00
|
|
|
key=AZD_AIR_DEMAND,
|
|
|
|
name="Air Demand",
|
|
|
|
),
|
2022-04-23 05:27:23 +00:00
|
|
|
AirzoneBinarySensorEntityDescription(
|
|
|
|
device_class=BinarySensorDeviceClass.BATTERY,
|
|
|
|
key=AZD_BATTERY_LOW,
|
|
|
|
name="Battery Low",
|
|
|
|
),
|
2022-03-27 20:39:46 +00:00
|
|
|
AirzoneBinarySensorEntityDescription(
|
2022-04-01 18:51:20 +00:00
|
|
|
device_class=BinarySensorDeviceClass.RUNNING,
|
2022-03-27 20:39:46 +00:00
|
|
|
key=AZD_FLOOR_DEMAND,
|
|
|
|
name="Floor Demand",
|
|
|
|
),
|
|
|
|
AirzoneBinarySensorEntityDescription(
|
|
|
|
attributes={
|
|
|
|
"errors": AZD_ERRORS,
|
|
|
|
},
|
2022-04-01 18:51:20 +00:00
|
|
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
2022-03-27 20:39:46 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
key=AZD_PROBLEMS,
|
|
|
|
name="Problem",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Add Airzone binary sensors from a config_entry."""
|
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
2022-04-23 05:20:14 +00:00
|
|
|
binary_sensors: list[AirzoneBinarySensor] = []
|
2022-05-09 14:07:11 +00:00
|
|
|
|
|
|
|
for system_id, system_data in coordinator.data[AZD_SYSTEMS].items():
|
|
|
|
for description in SYSTEM_BINARY_SENSOR_TYPES:
|
|
|
|
if description.key in system_data:
|
|
|
|
binary_sensors.append(
|
|
|
|
AirzoneSystemBinarySensor(
|
|
|
|
coordinator,
|
|
|
|
description,
|
|
|
|
entry,
|
|
|
|
system_id,
|
|
|
|
system_data,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-03-27 20:39:46 +00:00
|
|
|
for system_zone_id, zone_data in coordinator.data[AZD_ZONES].items():
|
2022-04-23 05:20:14 +00:00
|
|
|
for description in ZONE_BINARY_SENSOR_TYPES:
|
2022-03-27 20:39:46 +00:00
|
|
|
if description.key in zone_data:
|
|
|
|
binary_sensors.append(
|
2022-04-23 05:20:14 +00:00
|
|
|
AirzoneZoneBinarySensor(
|
2022-03-27 20:39:46 +00:00
|
|
|
coordinator,
|
|
|
|
description,
|
|
|
|
entry,
|
|
|
|
system_zone_id,
|
|
|
|
zone_data,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
async_add_entities(binary_sensors)
|
|
|
|
|
|
|
|
|
|
|
|
class AirzoneBinarySensor(AirzoneEntity, BinarySensorEntity):
|
2022-04-23 05:20:14 +00:00
|
|
|
"""Define an Airzone binary sensor."""
|
|
|
|
|
|
|
|
entity_description: AirzoneBinarySensorEntityDescription
|
|
|
|
|
2022-05-17 19:47:28 +00:00
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Update attributes when the coordinator updates."""
|
|
|
|
self._async_update_attrs()
|
|
|
|
super()._handle_coordinator_update()
|
|
|
|
|
2022-05-09 17:56:59 +00:00
|
|
|
@callback
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update binary sensor attributes."""
|
|
|
|
self._attr_is_on = self.get_airzone_value(self.entity_description.key)
|
|
|
|
if self.entity_description.attributes:
|
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
key: self.get_airzone_value(val)
|
|
|
|
for key, val in self.entity_description.attributes.items()
|
|
|
|
}
|
2022-04-23 05:20:14 +00:00
|
|
|
|
|
|
|
|
2022-05-09 14:07:11 +00:00
|
|
|
class AirzoneSystemBinarySensor(AirzoneSystemEntity, AirzoneBinarySensor):
|
|
|
|
"""Define an Airzone System binary sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: AirzoneUpdateCoordinator,
|
|
|
|
description: AirzoneBinarySensorEntityDescription,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
system_id: str,
|
|
|
|
system_data: dict[str, Any],
|
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(coordinator, entry, system_data)
|
|
|
|
self._attr_name = f"System {system_id} {description.name}"
|
|
|
|
self._attr_unique_id = f"{self._attr_unique_id}_{system_id}_{description.key}"
|
|
|
|
self.entity_description = description
|
2022-05-09 17:56:59 +00:00
|
|
|
self._async_update_attrs()
|
2022-05-09 14:07:11 +00:00
|
|
|
|
|
|
|
|
2022-04-23 05:20:14 +00:00
|
|
|
class AirzoneZoneBinarySensor(AirzoneZoneEntity, AirzoneBinarySensor):
|
|
|
|
"""Define an Airzone Zone binary sensor."""
|
2022-03-27 20:39:46 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: AirzoneUpdateCoordinator,
|
|
|
|
description: AirzoneBinarySensorEntityDescription,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
system_zone_id: str,
|
|
|
|
zone_data: dict[str, Any],
|
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(coordinator, entry, system_zone_id, zone_data)
|
2022-04-26 08:52:55 +00:00
|
|
|
|
2022-03-27 20:39:46 +00:00
|
|
|
self._attr_name = f"{zone_data[AZD_NAME]} {description.name}"
|
2022-04-26 08:52:55 +00:00
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{self._attr_unique_id}_{system_zone_id}_{description.key}"
|
|
|
|
)
|
2022-03-27 20:39:46 +00:00
|
|
|
self.entity_description = description
|
2022-05-09 17:56:59 +00:00
|
|
|
self._async_update_attrs()
|