Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for SwitchBot binary sensors."""
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 .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
15 from .entity import SwitchbotEntity
16 
17 PARALLEL_UPDATES = 0
18 
19 BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
20  "calibration": BinarySensorEntityDescription(
21  key="calibration",
22  translation_key="calibration",
23  entity_category=EntityCategory.DIAGNOSTIC,
24  ),
25  "motion_detected": BinarySensorEntityDescription(
26  key="pir_state",
27  name=None,
28  device_class=BinarySensorDeviceClass.MOTION,
29  ),
30  "contact_open": BinarySensorEntityDescription(
31  key="contact_open",
32  name=None,
33  device_class=BinarySensorDeviceClass.DOOR,
34  ),
35  "contact_timeout": BinarySensorEntityDescription(
36  key="contact_timeout",
37  translation_key="door_timeout",
38  device_class=BinarySensorDeviceClass.PROBLEM,
39  entity_category=EntityCategory.DIAGNOSTIC,
40  ),
42  key="is_light",
43  device_class=BinarySensorDeviceClass.LIGHT,
44  ),
45  "door_open": BinarySensorEntityDescription(
46  key="door_status",
47  name=None,
48  device_class=BinarySensorDeviceClass.DOOR,
49  ),
50  "unclosed_alarm": BinarySensorEntityDescription(
51  key="unclosed_alarm",
52  translation_key="door_unclosed_alarm",
53  entity_category=EntityCategory.DIAGNOSTIC,
54  device_class=BinarySensorDeviceClass.PROBLEM,
55  ),
56  "unlocked_alarm": BinarySensorEntityDescription(
57  key="unlocked_alarm",
58  translation_key="door_unlocked_alarm",
59  entity_category=EntityCategory.DIAGNOSTIC,
60  device_class=BinarySensorDeviceClass.PROBLEM,
61  ),
62  "auto_lock_paused": BinarySensorEntityDescription(
63  key="auto_lock_paused",
64  translation_key="door_auto_lock_paused",
65  entity_category=EntityCategory.DIAGNOSTIC,
66  ),
67 }
68 
69 
71  hass: HomeAssistant,
72  entry: SwitchbotConfigEntry,
73  async_add_entities: AddEntitiesCallback,
74 ) -> None:
75  """Set up Switchbot curtain based on a config entry."""
76  coordinator = entry.runtime_data
78  SwitchBotBinarySensor(coordinator, binary_sensor)
79  for binary_sensor in coordinator.device.parsed_data
80  if binary_sensor in BINARY_SENSOR_TYPES
81  )
82 
83 
85  """Representation of a Switchbot binary sensor."""
86 
87  def __init__(
88  self,
89  coordinator: SwitchbotDataUpdateCoordinator,
90  binary_sensor: str,
91  ) -> None:
92  """Initialize the Switchbot sensor."""
93  super().__init__(coordinator)
94  self._sensor_sensor = binary_sensor
95  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{coordinator.base_unique_id}-{binary_sensor}"
96  self.entity_descriptionentity_description = BINARY_SENSOR_TYPES[binary_sensor]
97 
98  @property
99  def is_on(self) -> bool:
100  """Return the state of the sensor."""
101  return self.parsed_dataparsed_data[self._sensor_sensor]
None __init__(self, SwitchbotDataUpdateCoordinator coordinator, str binary_sensor)
None async_setup_entry(HomeAssistant hass, SwitchbotConfigEntry entry, AddEntitiesCallback async_add_entities)