Add entity descriptions to incomfort binary sensor (#118863)

* Detach name from unique id in incomfort

* Add entity descriptions to incomfort

* Revert "Detach name from unique id in incomfort"

This reverts commit 1744844466.

* yes
This commit is contained in:
Joost Lekkerkerker 2024-06-05 12:51:39 +02:00 committed by GitHub
parent 68a537a05a
commit 239984f87d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,11 +2,16 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any from typing import Any
from incomfortclient import Heater as InComfortHeater from incomfortclient import Heater as InComfortHeater
from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -17,6 +22,24 @@ from .coordinator import InComfortDataCoordinator
from .entity import IncomfortEntity from .entity import IncomfortEntity
@dataclass(frozen=True, kw_only=True)
class IncomfortBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Incomfort binary sensor entity."""
value_key: str
extra_state_attributes_fn: Callable[[dict[str, Any]], dict[str, Any]]
SENSOR_TYPES: tuple[IncomfortBinarySensorEntityDescription, ...] = (
IncomfortBinarySensorEntityDescription(
key="failed",
name="Fault",
value_key="is_failed",
extra_state_attributes_fn=lambda status: {"fault_code": status["fault_code"]},
),
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: InComfortConfigEntry, entry: InComfortConfigEntry,
@ -25,23 +48,31 @@ async def async_setup_entry(
"""Set up an InComfort/InTouch binary_sensor entity.""" """Set up an InComfort/InTouch binary_sensor entity."""
incomfort_coordinator = entry.runtime_data incomfort_coordinator = entry.runtime_data
heaters = incomfort_coordinator.data.heaters heaters = incomfort_coordinator.data.heaters
async_add_entities(IncomfortFailed(incomfort_coordinator, h) for h in heaters) async_add_entities(
IncomfortBinarySensor(incomfort_coordinator, h, description)
for h in heaters
for description in SENSOR_TYPES
)
class IncomfortFailed(IncomfortEntity, BinarySensorEntity): class IncomfortBinarySensor(IncomfortEntity, BinarySensorEntity):
"""Representation of an InComfort Failed sensor.""" """Representation of an InComfort binary sensor."""
_attr_name = "Fault" entity_description: IncomfortBinarySensorEntityDescription
def __init__( def __init__(
self, coordinator: InComfortDataCoordinator, heater: InComfortHeater self,
coordinator: InComfortDataCoordinator,
heater: InComfortHeater,
description: IncomfortBinarySensorEntityDescription,
) -> None: ) -> None:
"""Initialize the binary sensor.""" """Initialize the binary sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.entity_description = description
self._heater = heater self._heater = heater
self._attr_unique_id = f"{heater.serial_no}_failed" self._attr_unique_id = f"{heater.serial_no}_{description.key}"
self._attr_device_info = DeviceInfo( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, heater.serial_no)}, identifiers={(DOMAIN, heater.serial_no)},
manufacturer="Intergas", manufacturer="Intergas",
@ -51,9 +82,9 @@ class IncomfortFailed(IncomfortEntity, BinarySensorEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return the status of the sensor.""" """Return the status of the sensor."""
return self._heater.status["is_failed"] return self._heater.status[self.entity_description.value_key]
@property @property
def extra_state_attributes(self) -> dict[str, Any] | None: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes.""" """Return the device state attributes."""
return {"fault_code": self._heater.status["fault_code"]} return self.entity_description.extra_state_attributes_fn(self._heater.status)