Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary sensors for Yale Alarm."""
2 
3 from __future__ import annotations
4 
6  BinarySensorDeviceClass,
7  BinarySensorEntity,
8  BinarySensorEntityDescription,
9 )
10 from homeassistant.const import EntityCategory
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 
14 from . import YaleConfigEntry
15 from .coordinator import YaleDataUpdateCoordinator
16 from .entity import YaleAlarmEntity, YaleEntity
17 
18 SENSOR_TYPES = (
20  key="acfail",
21  device_class=BinarySensorDeviceClass.PROBLEM,
22  entity_category=EntityCategory.DIAGNOSTIC,
23  translation_key="power_loss",
24  ),
26  key="battery",
27  device_class=BinarySensorDeviceClass.PROBLEM,
28  entity_category=EntityCategory.DIAGNOSTIC,
29  translation_key="battery",
30  ),
32  key="tamper",
33  device_class=BinarySensorDeviceClass.PROBLEM,
34  entity_category=EntityCategory.DIAGNOSTIC,
35  translation_key="tamper",
36  ),
38  key="jam",
39  device_class=BinarySensorDeviceClass.PROBLEM,
40  entity_category=EntityCategory.DIAGNOSTIC,
41  translation_key="jam",
42  ),
43 )
44 
45 
47  hass: HomeAssistant, entry: YaleConfigEntry, async_add_entities: AddEntitiesCallback
48 ) -> None:
49  """Set up the Yale binary sensor entry."""
50 
51  coordinator = entry.runtime_data
52  sensors: list[YaleDoorSensor | YaleDoorBatterySensor | YaleProblemSensor] = [
53  YaleDoorSensor(coordinator, data) for data in coordinator.data["door_windows"]
54  ]
55  sensors.extend(
56  YaleDoorBatterySensor(coordinator, data)
57  for data in coordinator.data["door_windows"]
58  )
59  sensors.extend(
60  YaleProblemSensor(coordinator, description) for description in SENSOR_TYPES
61  )
62 
63  async_add_entities(sensors)
64 
65 
67  """Representation of a Yale door sensor."""
68 
69  _attr_device_class = BinarySensorDeviceClass.DOOR
70 
71  @property
72  def is_on(self) -> bool:
73  """Return true if the binary sensor is on."""
74  return bool(self.coordinator.data["sensor_map"][self._attr_unique_id] == "open")
75 
76 
78  """Representation of a Yale door sensor battery status."""
79 
80  _attr_device_class = BinarySensorDeviceClass.BATTERY
81 
82  def __init__(
83  self,
84  coordinator: YaleDataUpdateCoordinator,
85  data: dict,
86  ) -> None:
87  """Initiate Yale door battery Sensor."""
88  super().__init__(coordinator, data)
89  self._attr_unique_id_attr_unique_id = f"{data["address"]}-battery"
90 
91  @property
92  def is_on(self) -> bool:
93  """Return true if the battery is low."""
94  state: bool = self.coordinator.data["sensor_battery_map"][self._attr_unique_id_attr_unique_id]
95  return state
96 
97 
99  """Representation of a Yale problem sensor."""
100 
101  entity_description: BinarySensorEntityDescription
102 
103  def __init__(
104  self,
105  coordinator: YaleDataUpdateCoordinator,
106  entity_description: BinarySensorEntityDescription,
107  ) -> None:
108  """Initiate Yale Problem Sensor."""
109  super().__init__(coordinator)
110  self.entity_descriptionentity_description = entity_description
111  self._attr_unique_id_attr_unique_id = f"{coordinator.entry.entry_id}-{entity_description.key}"
112 
113  @property
114  def is_on(self) -> bool:
115  """Return true if the binary sensor is on."""
116  return bool(
117  self.coordinator.data["status"][self.entity_descriptionentity_description.key]
118  != "main.normal"
119  )
None __init__(self, YaleDataUpdateCoordinator coordinator, dict data)
None __init__(self, YaleDataUpdateCoordinator coordinator, BinarySensorEntityDescription entity_description)
None async_setup_entry(HomeAssistant hass, YaleConfigEntry entry, AddEntitiesCallback async_add_entities)