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
pull/118861/head^2
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
1 changed files with 41 additions and 10 deletions

View File

@ -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)