mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00: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.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import AvmWrapper, ConnectionInfo, FritzBoxBaseEntity
|
from .common import (
|
||||||
|
AvmWrapper,
|
||||||
|
ConnectionInfo,
|
||||||
|
FritzBoxBaseCoordinatorEntity,
|
||||||
|
FritzEntityDescription,
|
||||||
|
)
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class FritzBinarySensorEntityDescription(BinarySensorEntityDescription):
|
class FritzBinarySensorEntityDescription(
|
||||||
|
BinarySensorEntityDescription, FritzEntityDescription
|
||||||
|
):
|
||||||
"""Describes Fritz sensor entity."""
|
"""Describes Fritz sensor entity."""
|
||||||
|
|
||||||
is_suitable: Callable[[ConnectionInfo], bool] = lambda info: info.wan_enabled
|
is_suitable: Callable[[ConnectionInfo], bool] = lambda info: info.wan_enabled
|
||||||
@ -34,12 +41,14 @@ SENSOR_TYPES: tuple[FritzBinarySensorEntityDescription, ...] = (
|
|||||||
name="Connection",
|
name="Connection",
|
||||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
value_fn=lambda status, _: bool(status.is_connected),
|
||||||
),
|
),
|
||||||
FritzBinarySensorEntityDescription(
|
FritzBinarySensorEntityDescription(
|
||||||
key="is_linked",
|
key="is_linked",
|
||||||
name="Link",
|
name="Link",
|
||||||
device_class=BinarySensorDeviceClass.PLUG,
|
device_class=BinarySensorDeviceClass.PLUG,
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
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)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class FritzBoxBinarySensor(FritzBoxBaseEntity, BinarySensorEntity):
|
class FritzBoxBinarySensor(FritzBoxBaseCoordinatorEntity, BinarySensorEntity):
|
||||||
"""Define FRITZ!Box connectivity class."""
|
"""Define FRITZ!Box connectivity class."""
|
||||||
|
|
||||||
def __init__(
|
entity_description: FritzBinarySensorEntityDescription
|
||||||
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)
|
|
||||||
|
|
||||||
def update(self) -> None:
|
@property
|
||||||
"""Update data."""
|
def is_on(self) -> bool | None:
|
||||||
_LOGGER.debug("Updating FRITZ!Box binary sensors")
|
"""Return true if the binary sensor is on."""
|
||||||
if self.entity_description.key == "is_connected":
|
if isinstance(
|
||||||
self._attr_is_on = bool(self._avm_wrapper.fritz_status.is_connected)
|
state := self.coordinator.data.get(self.entity_description.key), bool
|
||||||
elif self.entity_description.key == "is_linked":
|
):
|
||||||
self._attr_is_on = bool(self._avm_wrapper.fritz_status.is_linked)
|
return state
|
||||||
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user