mirror of
https://github.com/home-assistant/core
synced 2024-09-12 15:16:21 +02:00
Turn AVM FRITZ!Box Tools binary sensors into coordinator entities (#89955)
make binary sensors coordinator entities
This commit is contained in:
parent
33fef5592f
commit
c0387a655c
@ -15,14 +15,21 @@ from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .common import AvmWrapper, ConnectionInfo, FritzBoxBaseEntity
|
||||
from .common import (
|
||||
AvmWrapper,
|
||||
ConnectionInfo,
|
||||
FritzBoxBaseCoordinatorEntity,
|
||||
FritzEntityDescription,
|
||||
)
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FritzBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
class FritzBinarySensorEntityDescription(
|
||||
BinarySensorEntityDescription, FritzEntityDescription
|
||||
):
|
||||
"""Describes Fritz sensor entity."""
|
||||
|
||||
is_suitable: Callable[[ConnectionInfo], bool] = lambda info: info.wan_enabled
|
||||
@ -34,12 +41,14 @@ SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = (
|
||||
name="Connection",
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
value_fn=lambda status, _: bool(status.is_connected),
|
||||
),
|
||||
FritzBinarySensorEntityDescription(
|
||||
key="is_linked",
|
||||
name="Link",
|
||||
device_class=BinarySensorDeviceClass.PLUG,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
value_fn=lambda status, _: bool(status.is_linked),
|
||||
),
|
||||
)
|
||||
|
||||
@ -62,25 +71,16 @@ async def async_setup_entry(
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class FritzBoxBinarySensor(FritzBoxBaseEntity, BinarySensorEntity):
|
||||
class FritzBoxBinarySensor(FritzBoxBaseCoordinatorEntity, BinarySensorEntity):
|
||||
"""Define FRITZ!Box connectivity class."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
avm_wrapper: AvmWrapper,
|
||||
device_friendly_name: str,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Init FRITZ!Box connectivity class."""
|
||||
self.entity_description = description
|
||||
self._attr_name = f"{device_friendly_name} {description.name}"
|
||||
self._attr_unique_id = f"{avm_wrapper.unique_id}-{description.key}"
|
||||
super().__init__(avm_wrapper, device_friendly_name)
|
||||
entity_description: FritzBinarySensorEntityDescription
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update data."""
|
||||
_LOGGER.debug("Updating FRITZ!Box binary sensors")
|
||||
if self.entity_description.key == "is_connected":
|
||||
self._attr_is_on = bool(self._avm_wrapper.fritz_status.is_connected)
|
||||
elif self.entity_description.key == "is_linked":
|
||||
self._attr_is_on = bool(self._avm_wrapper.fritz_status.is_linked)
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if the binary sensor is on."""
|
||||
if isinstance(
|
||||
state := self.coordinator.data.get(self.entity_description.key), bool
|
||||
):
|
||||
return state
|
||||
return None
|
||||
|
Loading…
Reference in New Issue
Block a user