Migrate leaone to use runtime_data (#147336)

This commit is contained in:
epenet 2025-06-23 12:12:32 +02:00 committed by GitHub
parent f64533e9e0
commit 741e89383b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 23 deletions

View File

@ -14,27 +14,26 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DOMAIN
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type LeaoneConfigEntry = ConfigEntry[PassiveBluetoothProcessorCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: LeaoneConfigEntry) -> bool:
"""Set up Leaone BLE device from a config entry.""" """Set up Leaone BLE device from a config entry."""
address = entry.unique_id address = entry.unique_id
assert address is not None assert address is not None
data = LeaoneBluetoothDeviceData() data = LeaoneBluetoothDeviceData()
coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = ( coordinator = PassiveBluetoothProcessorCoordinator(
PassiveBluetoothProcessorCoordinator(
hass, hass,
_LOGGER, _LOGGER,
address=address, address=address,
mode=BluetoothScanningMode.PASSIVE, mode=BluetoothScanningMode.PASSIVE,
update_method=data.update, update_method=data.update,
) )
) entry.runtime_data = 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.async_on_unload(
coordinator.async_start() coordinator.async_start()
@ -42,9 +41,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: LeaoneConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -4,11 +4,9 @@ from __future__ import annotations
from leaone_ble import DeviceClass as LeaoneSensorDeviceClass, SensorUpdate, Units from leaone_ble import DeviceClass as LeaoneSensorDeviceClass, SensorUpdate, Units
from homeassistant import config_entries
from homeassistant.components.bluetooth.passive_update_processor import ( from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothDataProcessor, PassiveBluetoothDataProcessor,
PassiveBluetoothDataUpdate, PassiveBluetoothDataUpdate,
PassiveBluetoothProcessorCoordinator,
PassiveBluetoothProcessorEntity, PassiveBluetoothProcessorEntity,
) )
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -26,7 +24,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
from .const import DOMAIN from . import LeaoneConfigEntry
from .device import device_key_to_bluetooth_entity_key from .device import device_key_to_bluetooth_entity_key
SENSOR_DESCRIPTIONS = { SENSOR_DESCRIPTIONS = {
@ -106,13 +104,11 @@ def sensor_update_to_bluetooth_data_update(
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: config_entries.ConfigEntry, entry: LeaoneConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the Leaone BLE sensors.""" """Set up the Leaone BLE sensors."""
coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][ coordinator = entry.runtime_data
entry.entry_id
]
processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update) processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update)
entry.async_on_unload( entry.async_on_unload(
processor.async_add_entities_listener( processor.async_add_entities_listener(