Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The LD2410 BLE integration."""
2 
3 import logging
4 
5 from bleak_retry_connector import (
6  BleakError,
7  close_stale_connections_by_address,
8  get_device,
9 )
10 from ld2410_ble import LD2410BLE
11 
12 from homeassistant.components import bluetooth
13 from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import CONF_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
16 from homeassistant.core import Event, HomeAssistant, callback
17 from homeassistant.exceptions import ConfigEntryNotReady
18 
19 from .const import DOMAIN
20 from .coordinator import LD2410BLECoordinator
21 from .models import LD2410BLEData
22 
23 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
28 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
29  """Set up LD2410 BLE from a config entry."""
30  address: str = entry.data[CONF_ADDRESS]
31 
32  await close_stale_connections_by_address(address)
33 
34  ble_device = bluetooth.async_ble_device_from_address(
35  hass, address.upper(), True
36  ) or await get_device(address)
37  if not ble_device:
38  raise ConfigEntryNotReady(
39  f"Could not find LD2410B device with address {address}"
40  )
41 
42  ld2410_ble = LD2410BLE(ble_device)
43 
44  coordinator = LD2410BLECoordinator(hass, ld2410_ble)
45 
46  try:
47  await ld2410_ble.initialise()
48  except BleakError as exc:
49  raise ConfigEntryNotReady(
50  f"Could not initialise LD2410B device with address {address}"
51  ) from exc
52 
53  @callback
54  def _async_update_ble(
55  service_info: bluetooth.BluetoothServiceInfoBleak,
56  change: bluetooth.BluetoothChange,
57  ) -> None:
58  """Update from a ble callback."""
59  ld2410_ble.set_ble_device_and_advertisement_data(
60  service_info.device, service_info.advertisement
61  )
62 
63  entry.async_on_unload(
64  bluetooth.async_register_callback(
65  hass,
66  _async_update_ble,
67  BluetoothCallbackMatcher({ADDRESS: address}),
68  bluetooth.BluetoothScanningMode.ACTIVE,
69  )
70  )
71 
72  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LD2410BLEData(
73  entry.title, ld2410_ble, coordinator
74  )
75 
76  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
77  entry.async_on_unload(entry.add_update_listener(_async_update_listener))
78 
79  async def _async_stop(event: Event) -> None:
80  """Close the connection."""
81  await ld2410_ble.stop()
82 
83  entry.async_on_unload(
84  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
85  )
86  return True
87 
88 
89 async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
90  """Handle options update."""
91  data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
92  if entry.title != data.title:
93  await hass.config_entries.async_reload(entry.entry_id)
94 
95 
96 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
97  """Unload a config entry."""
98  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
99  data: LD2410BLEData = hass.data[DOMAIN].pop(entry.entry_id)
100  await data.device.stop()
101 
102  return unload_ok
DeviceEntry get_device(HomeAssistant hass, str unique_id)
Definition: util.py:12
None _async_stop(HomeAssistant hass, bool restart)
Definition: __init__.py:392
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:96
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:89
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:28