1 """The LD2410 BLE integration."""
5 from bleak_retry_connector
import (
7 close_stale_connections_by_address,
10 from ld2410_ble
import LD2410BLE
19 from .const
import DOMAIN
20 from .coordinator
import LD2410BLECoordinator
21 from .models
import LD2410BLEData
23 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
25 _LOGGER = logging.getLogger(__name__)
29 """Set up LD2410 BLE from a config entry."""
30 address: str = entry.data[CONF_ADDRESS]
32 await close_stale_connections_by_address(address)
34 ble_device = bluetooth.async_ble_device_from_address(
35 hass, address.upper(),
True
39 f
"Could not find LD2410B device with address {address}"
42 ld2410_ble = LD2410BLE(ble_device)
47 await ld2410_ble.initialise()
48 except BleakError
as exc:
50 f
"Could not initialise LD2410B device with address {address}"
54 def _async_update_ble(
55 service_info: bluetooth.BluetoothServiceInfoBleak,
56 change: bluetooth.BluetoothChange,
58 """Update from a ble callback."""
59 ld2410_ble.set_ble_device_and_advertisement_data(
60 service_info.device, service_info.advertisement
63 entry.async_on_unload(
64 bluetooth.async_register_callback(
68 bluetooth.BluetoothScanningMode.ACTIVE,
72 hass.data.setdefault(DOMAIN, {})[entry.entry_id] =
LD2410BLEData(
73 entry.title, ld2410_ble, coordinator
76 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
77 entry.async_on_unload(entry.add_update_listener(_async_update_listener))
80 """Close the connection."""
81 await ld2410_ble.stop()
83 entry.async_on_unload(
84 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
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)
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()
DeviceEntry get_device(HomeAssistant hass, str unique_id)
None _async_stop(HomeAssistant hass, bool restart)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)