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 import bluetooth
from homeassistant.components.bluetooth.match import ADDRESS, BluetoothCallbackMatcher 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.const import CONF_ADDRESS, EVENT_HOMEASSISTANT_STOP, Platform
from homeassistant.core import Event, HomeAssistant, callback from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from .const import DOMAIN
from .coordinator import LD2410BLECoordinator from .coordinator import LD2410BLECoordinator
from .models import LD2410BLEData from .models import LD2410BLEConfigEntry, LD2410BLEData
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _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.""" """Set up LD2410 BLE from a config entry."""
address: str = entry.data[CONF_ADDRESS] 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.runtime_data = LD2410BLEData(entry.title, ld2410_ble, coordinator)
entry.title, ld2410_ble, coordinator
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(_async_update_listener)) 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 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.""" """Handle options update."""
data: LD2410BLEData = hass.data[DOMAIN][entry.entry_id] if entry.title != entry.runtime_data.title:
if entry.title != data.title:
await hass.config_entries.async_reload(entry.entry_id) 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.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
data: LD2410BLEData = hass.data[DOMAIN].pop(entry.entry_id) await entry.runtime_data.device.stop()
await data.device.stop()
return unload_ok return unload_ok

View File

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

View File

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

View File

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

View File

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