Migrate ld2410_ble to use runtime_data (#147335)

This commit is contained in:
epenet 2025-06-23 12:13:10 +02:00 committed by GitHub
parent 741e89383b
commit a2785a86dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 27 deletions

View File

@ -11,21 +11,19 @@ from ld2410_ble import LD2410BLE
from homeassistant.components import bluetooth
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
from .coordinator import LD2410BLECoordinator
from .models import LD2410BLEData
from .models import LD2410BLEConfigEntry, LD2410BLEData
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: LD2410BLEConfigEntry) -> bool:
"""Set up LD2410 BLE from a config entry."""
address: str = entry.data[CONF_ADDRESS]
@ -69,9 +67,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = LD2410BLEData(
entry.title, ld2410_ble, coordinator
)
entry.runtime_data = LD2410BLEData(entry.title, ld2410_ble, coordinator)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
@ -86,17 +82,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def _async_update_listener(
hass: HomeAssistant, entry: LD2410BLEConfigEntry
) -> None:
"""Handle options update."""
data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
if entry.title != data.title:
if entry.title != entry.runtime_data.title:
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: LD2410BLEConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
data: LD2410BLEData = hass.data[DOMAIN].pop(entry.entry_id)
await data.device.stop()
await entry.runtime_data.device.stop()
return unload_ok

View File

@ -5,7 +5,6 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
@ -13,8 +12,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import LD2410BLE, LD2410BLECoordinator
from .const import DOMAIN
from .models import LD2410BLEData
from .models import LD2410BLEConfigEntry
ENTITY_DESCRIPTIONS = (
BinarySensorEntityDescription(
@ -30,11 +28,11 @@ ENTITY_DESCRIPTIONS = (
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: LD2410BLEConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the platform for LD2410BLE."""
data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
data = entry.runtime_data
async_add_entities(
LD2410BLEBinarySensor(data.coordinator, data.device, entry.title, description)
for description in ENTITY_DESCRIPTIONS

View File

@ -1,18 +1,23 @@
"""Data coordinator for receiving LD2410B updates."""
from __future__ import annotations
from datetime import datetime
import logging
import time
from typing import TYPE_CHECKING
from ld2410_ble import LD2410BLE, LD2410BLEState
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN
if TYPE_CHECKING:
from .models import LD2410BLEConfigEntry
_LOGGER = logging.getLogger(__name__)
NEVER_TIME = -86400.0
@ -22,10 +27,13 @@ DEBOUNCE_SECONDS = 1.0
class LD2410BLECoordinator(DataUpdateCoordinator[None]):
"""Data coordinator for receiving LD2410B updates."""
config_entry: ConfigEntry
config_entry: LD2410BLEConfigEntry
def __init__(
self, hass: HomeAssistant, config_entry: ConfigEntry, ld2410_ble: LD2410BLE
self,
hass: HomeAssistant,
config_entry: LD2410BLEConfigEntry,
ld2410_ble: LD2410BLE,
) -> None:
"""Initialise the coordinator."""
super().__init__(

View File

@ -6,8 +6,12 @@ from dataclasses import dataclass
from ld2410_ble import LD2410BLE
from homeassistant.config_entries import ConfigEntry
from .coordinator import LD2410BLECoordinator
type LD2410BLEConfigEntry = ConfigEntry[LD2410BLEData]
@dataclass
class LD2410BLEData:

View File

@ -1,12 +1,13 @@
"""LD2410 BLE integration sensor platform."""
from ld2410_ble import LD2410BLE
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory, UnitOfLength
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
@ -14,9 +15,8 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import LD2410BLE, LD2410BLECoordinator
from .const import DOMAIN
from .models import LD2410BLEData
from .coordinator import LD2410BLECoordinator
from .models import LD2410BLEConfigEntry
MOVING_TARGET_DISTANCE_DESCRIPTION = SensorEntityDescription(
key="moving_target_distance",
@ -121,11 +121,11 @@ SENSOR_DESCRIPTIONS = [
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: LD2410BLEConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the platform for LD2410BLE."""
data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id]
data = entry.runtime_data
async_add_entities(
LD2410BLESensor(
data.coordinator,