mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Add entity descriptions to Smarty Binary sensor (#129110)
This commit is contained in:
parent
5b2113c43d
commit
929ba70ef8
@ -2,11 +2,16 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from pysmarty2 import Smarty
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -17,6 +22,34 @@ from .coordinator import SmartyConfigEntry, SmartyCoordinator
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class SmartyBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||||
|
"""Class describing Smarty binary sensor entities."""
|
||||||
|
|
||||||
|
value_fn: Callable[[Smarty], bool]
|
||||||
|
|
||||||
|
|
||||||
|
ENTITIES: tuple[SmartyBinarySensorEntityDescription, ...] = (
|
||||||
|
SmartyBinarySensorEntityDescription(
|
||||||
|
key="alarm",
|
||||||
|
name="Alarm",
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
value_fn=lambda smarty: smarty.alarm,
|
||||||
|
),
|
||||||
|
SmartyBinarySensorEntityDescription(
|
||||||
|
key="warning",
|
||||||
|
name="Warning",
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
value_fn=lambda smarty: smarty.warning,
|
||||||
|
),
|
||||||
|
SmartyBinarySensorEntityDescription(
|
||||||
|
key="boost",
|
||||||
|
name="Boost State",
|
||||||
|
value_fn=lambda smarty: smarty.boost,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: SmartyConfigEntry,
|
entry: SmartyConfigEntry,
|
||||||
@ -25,75 +58,31 @@ async def async_setup_entry(
|
|||||||
"""Set up the Smarty Binary Sensor Platform."""
|
"""Set up the Smarty Binary Sensor Platform."""
|
||||||
|
|
||||||
coordinator = entry.runtime_data
|
coordinator = entry.runtime_data
|
||||||
sensors = [
|
|
||||||
AlarmSensor(coordinator),
|
|
||||||
WarningSensor(coordinator),
|
|
||||||
BoostSensor(coordinator),
|
|
||||||
]
|
|
||||||
|
|
||||||
async_add_entities(sensors)
|
async_add_entities(
|
||||||
|
SmartyBinarySensor(coordinator, description) for description in ENTITIES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class SmartyBinarySensor(CoordinatorEntity[SmartyCoordinator], BinarySensorEntity):
|
class SmartyBinarySensor(CoordinatorEntity[SmartyCoordinator], BinarySensorEntity):
|
||||||
"""Representation of a Smarty Binary Sensor."""
|
"""Representation of a Smarty Binary Sensor."""
|
||||||
|
|
||||||
|
entity_description: SmartyBinarySensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: SmartyCoordinator,
|
coordinator: SmartyCoordinator,
|
||||||
name: str,
|
entity_description: SmartyBinarySensorEntityDescription,
|
||||||
device_class: BinarySensorDeviceClass | None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attr_name = f"{coordinator.config_entry.title} {name}"
|
self.entity_description = entity_description
|
||||||
self._attr_device_class = device_class
|
self._attr_name = f"{coordinator.config_entry.title} {entity_description.name}"
|
||||||
|
self._attr_unique_id = (
|
||||||
|
f"{coordinator.config_entry.entry_id}_{entity_description.key}"
|
||||||
class BoostSensor(SmartyBinarySensor):
|
|
||||||
"""Boost State Binary Sensor."""
|
|
||||||
|
|
||||||
def __init__(self, coordinator: SmartyCoordinator) -> None:
|
|
||||||
"""Alarm Sensor Init."""
|
|
||||||
super().__init__(coordinator, name="Boost State", device_class=None)
|
|
||||||
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_boost"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool | None:
|
|
||||||
"""Return true if the binary sensor is on."""
|
|
||||||
return self.coordinator.client.boost
|
|
||||||
|
|
||||||
|
|
||||||
class AlarmSensor(SmartyBinarySensor):
|
|
||||||
"""Alarm Binary Sensor."""
|
|
||||||
|
|
||||||
def __init__(self, coordinator: SmartyCoordinator) -> None:
|
|
||||||
"""Alarm Sensor Init."""
|
|
||||||
super().__init__(
|
|
||||||
coordinator,
|
|
||||||
name="Alarm",
|
|
||||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
||||||
)
|
)
|
||||||
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_alarm"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool | None:
|
def is_on(self) -> bool:
|
||||||
"""Return true if the binary sensor is on."""
|
"""Return the state of the binary sensor."""
|
||||||
return self.coordinator.client.alarm
|
return self.entity_description.value_fn(self.coordinator.client)
|
||||||
|
|
||||||
|
|
||||||
class WarningSensor(SmartyBinarySensor):
|
|
||||||
"""Warning Sensor."""
|
|
||||||
|
|
||||||
def __init__(self, coordinator: SmartyCoordinator) -> None:
|
|
||||||
"""Warning Sensor Init."""
|
|
||||||
super().__init__(
|
|
||||||
coordinator,
|
|
||||||
name="Warning",
|
|
||||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
|
||||||
)
|
|
||||||
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_warning"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool | None:
|
|
||||||
"""Return true if the binary sensor is on."""
|
|
||||||
return self.coordinator.client.warning
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user