From 239984f87d7a563ce809dae3022fb276efced129 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 5 Jun 2024 12:51:39 +0200 Subject: [PATCH] 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 17448444664f6b84c5e5e2a18899444eafe75785. * yes --- .../components/incomfort/binary_sensor.py | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/incomfort/binary_sensor.py b/homeassistant/components/incomfort/binary_sensor.py index 238f1812aa2..aecbd96f472 100644 --- a/homeassistant/components/incomfort/binary_sensor.py +++ b/homeassistant/components/incomfort/binary_sensor.py @@ -2,11 +2,16 @@ from __future__ import annotations +from collections.abc import Callable +from dataclasses import dataclass from typing import Any 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.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -17,6 +22,24 @@ from .coordinator import InComfortDataCoordinator 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( hass: HomeAssistant, entry: InComfortConfigEntry, @@ -25,23 +48,31 @@ async def async_setup_entry( """Set up an InComfort/InTouch binary_sensor entity.""" incomfort_coordinator = entry.runtime_data 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): - """Representation of an InComfort Failed sensor.""" +class IncomfortBinarySensor(IncomfortEntity, BinarySensorEntity): + """Representation of an InComfort binary sensor.""" - _attr_name = "Fault" + entity_description: IncomfortBinarySensorEntityDescription def __init__( - self, coordinator: InComfortDataCoordinator, heater: InComfortHeater + self, + coordinator: InComfortDataCoordinator, + heater: InComfortHeater, + description: IncomfortBinarySensorEntityDescription, ) -> None: """Initialize the binary sensor.""" super().__init__(coordinator) + self.entity_description = description 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( identifiers={(DOMAIN, heater.serial_no)}, manufacturer="Intergas", @@ -51,9 +82,9 @@ class IncomfortFailed(IncomfortEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return the status of the sensor.""" - return self._heater.status["is_failed"] + return self._heater.status[self.entity_description.value_key] @property - def extra_state_attributes(self) -> dict[str, Any] | None: + def extra_state_attributes(self) -> dict[str, Any]: """Return the device state attributes.""" - return {"fault_code": self._heater.status["fault_code"]} + return self.entity_description.extra_state_attributes_fn(self._heater.status)