Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """LD2410 BLE integration binary sensor platform."""
2 
4  BinarySensorDeviceClass,
5  BinarySensorEntity,
6  BinarySensorEntityDescription,
7 )
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import device_registry as dr
11 from homeassistant.helpers.device_registry import DeviceInfo
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.helpers.update_coordinator import CoordinatorEntity
14 
15 from . import LD2410BLE, LD2410BLECoordinator
16 from .const import DOMAIN
17 from .models import LD2410BLEData
18 
19 ENTITY_DESCRIPTIONS = (
21  key="is_moving",
22  device_class=BinarySensorDeviceClass.MOTION,
23  ),
25  key="is_static",
26  device_class=BinarySensorDeviceClass.OCCUPANCY,
27  ),
28 )
29 
30 
32  hass: HomeAssistant,
33  entry: ConfigEntry,
34  async_add_entities: AddEntitiesCallback,
35 ) -> None:
36  """Set up the platform for LD2410BLE."""
37  data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
39  LD2410BLEBinarySensor(data.coordinator, data.device, entry.title, description)
40  for description in ENTITY_DESCRIPTIONS
41  )
42 
43 
45  CoordinatorEntity[LD2410BLECoordinator], BinarySensorEntity
46 ):
47  """Moving/static sensor for LD2410BLE."""
48 
49  _attr_has_entity_name = True
50 
51  def __init__(
52  self,
53  coordinator: LD2410BLECoordinator,
54  device: LD2410BLE,
55  name: str,
56  description: BinarySensorEntityDescription,
57  ) -> None:
58  """Initialize the sensor."""
59  super().__init__(coordinator)
60  self._coordinator_coordinator = coordinator
61  self._key_key = description.key
62  self._device_device = device
63  self.entity_descriptionentity_description = description
64  self._attr_unique_id_attr_unique_id = f"{device.address}_{self._key}"
65  self._attr_device_info_attr_device_info = DeviceInfo(
66  name=name,
67  connections={(dr.CONNECTION_BLUETOOTH, device.address)},
68  )
69  self._attr_is_on_attr_is_on = getattr(self._device_device, self._key_key)
70 
71  @callback
72  def _handle_coordinator_update(self) -> None:
73  """Handle updated data from the coordinator."""
74  self._attr_is_on_attr_is_on = getattr(self._device_device, self._key_key)
75  self.async_write_ha_stateasync_write_ha_state()
76 
77  @property
78  def available(self) -> bool:
79  """Unavailable if coordinator isn't connected."""
80  return self._coordinator_coordinator.connected and super().available
None __init__(self, LD2410BLECoordinator coordinator, LD2410BLE device, str name, BinarySensorEntityDescription description)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)