Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary Sensor platform for Garages Amsterdam."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from odp_amsterdam import Garage
9 
11  BinarySensorDeviceClass,
12  BinarySensorEntity,
13  BinarySensorEntityDescription,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 
18 from . import GaragesAmsterdamConfigEntry
19 from .coordinator import GaragesAmsterdamDataUpdateCoordinator
20 from .entity import GaragesAmsterdamEntity
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """Class describing Garages Amsterdam binary sensor entity."""
26 
27  is_on: Callable[[Garage], bool]
28 
29 
30 BINARY_SENSORS: tuple[GaragesAmsterdamBinarySensorEntityDescription, ...] = (
32  key="state",
33  translation_key="state",
34  device_class=BinarySensorDeviceClass.PROBLEM,
35  is_on=lambda garage: garage.state != "ok",
36  ),
37 )
38 
39 
41  hass: HomeAssistant,
42  entry: GaragesAmsterdamConfigEntry,
43  async_add_entities: AddEntitiesCallback,
44 ) -> None:
45  """Defer sensor setup to the shared sensor module."""
46  coordinator = entry.runtime_data
47 
50  coordinator=coordinator,
51  garage_name=entry.data["garage_name"],
52  description=description,
53  )
54  for description in BINARY_SENSORS
55  )
56 
57 
59  """Binary Sensor representing garages amsterdam data."""
60 
61  entity_description: GaragesAmsterdamBinarySensorEntityDescription
62 
63  def __init__(
64  self,
65  *,
66  coordinator: GaragesAmsterdamDataUpdateCoordinator,
67  garage_name: str,
68  description: GaragesAmsterdamBinarySensorEntityDescription,
69  ) -> None:
70  """Initialize garages amsterdam binary sensor."""
71  super().__init__(coordinator, garage_name)
72  self.entity_descriptionentity_description = description
73  self._attr_unique_id_attr_unique_id = f"{garage_name}-{description.key}"
74 
75  @property
76  def is_on(self) -> bool:
77  """If the binary sensor is currently on or off."""
78  return self.entity_descriptionentity_description.is_on(self.coordinator.data[self._garage_name_garage_name])
None __init__(self, *GaragesAmsterdamDataUpdateCoordinator coordinator, str garage_name, GaragesAmsterdamBinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, GaragesAmsterdamConfigEntry entry, AddEntitiesCallback async_add_entities)