Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Platform for binary sensor integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from devolo_plc_api.plcnet_api import LogicalNetwork
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from . import DevoloHomeNetworkConfigEntry
20 from .const import CONNECTED_PLC_DEVICES, CONNECTED_TO_ROUTER
21 from .coordinator import DevoloDataUpdateCoordinator
22 from .entity import DevoloCoordinatorEntity
23 
24 PARALLEL_UPDATES = 0
25 
26 
27 def _is_connected_to_router(entity: DevoloBinarySensorEntity) -> bool:
28  """Check, if device is attached to the router."""
29  return all(
30  device.attached_to_router
31  for device in entity.coordinator.data.devices
32  if device.mac_address == entity.device.mac
33  )
34 
35 
36 @dataclass(frozen=True, kw_only=True)
38  """Describes devolo sensor entity."""
39 
40  value_func: Callable[[DevoloBinarySensorEntity], bool]
41 
42 
43 SENSOR_TYPES: dict[str, DevoloBinarySensorEntityDescription] = {
44  CONNECTED_TO_ROUTER: DevoloBinarySensorEntityDescription(
45  key=CONNECTED_TO_ROUTER,
46  device_class=BinarySensorDeviceClass.PLUG,
47  entity_category=EntityCategory.DIAGNOSTIC,
48  entity_registry_enabled_default=False,
49  value_func=_is_connected_to_router,
50  ),
51 }
52 
53 
55  hass: HomeAssistant,
56  entry: DevoloHomeNetworkConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Get all devices and sensors and setup them via config entry."""
60  coordinators = entry.runtime_data.coordinators
61 
62  entities: list[BinarySensorEntity] = []
63  entities.append(
65  entry,
66  coordinators[CONNECTED_PLC_DEVICES],
67  SENSOR_TYPES[CONNECTED_TO_ROUTER],
68  )
69  )
70  async_add_entities(entities)
71 
72 
74  DevoloCoordinatorEntity[LogicalNetwork], BinarySensorEntity
75 ):
76  """Representation of a devolo binary sensor."""
77 
78  def __init__(
79  self,
80  entry: DevoloHomeNetworkConfigEntry,
81  coordinator: DevoloDataUpdateCoordinator[LogicalNetwork],
82  description: DevoloBinarySensorEntityDescription,
83  ) -> None:
84  """Initialize entity."""
85  self.entity_description: DevoloBinarySensorEntityDescription = description
86  super().__init__(entry, coordinator)
87 
88  @property
89  def is_on(self) -> bool:
90  """State of the binary sensor."""
91  return self.entity_description.value_func(self)
None __init__(self, DevoloHomeNetworkConfigEntry entry, DevoloDataUpdateCoordinator[LogicalNetwork] coordinator, DevoloBinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, DevoloHomeNetworkConfigEntry entry, AddEntitiesCallback async_add_entities)
bool _is_connected_to_router(DevoloBinarySensorEntity entity)