From 93e270f379c2fc2ea0e16272634e0d1f443c058b Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 26 Oct 2024 02:30:48 +0200 Subject: [PATCH] Use runtime_data in aranet (#129155) --- homeassistant/components/aranet/__init__.py | 35 ++++++++++----------- homeassistant/components/aranet/sensor.py | 12 +++---- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/aranet/__init__.py b/homeassistant/components/aranet/__init__.py index 3a2bc266653..81b3dae04de 100644 --- a/homeassistant/components/aranet/__init__.py +++ b/homeassistant/components/aranet/__init__.py @@ -15,12 +15,14 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from .const import DOMAIN - PLATFORMS: list[Platform] = [Platform.SENSOR] _LOGGER = logging.getLogger(__name__) +type AranetConfigEntry = ConfigEntry[ + PassiveBluetoothProcessorCoordinator[Aranet4Advertisement] +] + def _service_info_to_adv( service_info: BluetoothServiceInfoBleak, @@ -28,30 +30,25 @@ def _service_info_to_adv( return Aranet4Advertisement(service_info.device, service_info.advertisement) -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: AranetConfigEntry) -> bool: """Set up Aranet from a config entry.""" address = entry.unique_id assert address is not None - coordinator = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = ( - PassiveBluetoothProcessorCoordinator( - hass, - _LOGGER, - address=address, - mode=BluetoothScanningMode.PASSIVE, - update_method=_service_info_to_adv, - ) + coordinator = PassiveBluetoothProcessorCoordinator( + hass, + _LOGGER, + address=address, + mode=BluetoothScanningMode.PASSIVE, + update_method=_service_info_to_adv, ) + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) - entry.async_on_unload( - coordinator.async_start() - ) # only start after all platforms have had a chance to subscribe + # only start after all platforms have had a chance to subscribe + entry.async_on_unload(coordinator.async_start()) return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: AranetConfigEntry) -> bool: """Unload a config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/aranet/sensor.py b/homeassistant/components/aranet/sensor.py index 1dc4b9f956e..d7fbd0e4b3b 100644 --- a/homeassistant/components/aranet/sensor.py +++ b/homeassistant/components/aranet/sensor.py @@ -8,12 +8,10 @@ from typing import Any from aranet4.client import Aranet4Advertisement from bleak.backends.device import BLEDevice -from homeassistant import config_entries from homeassistant.components.bluetooth.passive_update_processor import ( PassiveBluetoothDataProcessor, PassiveBluetoothDataUpdate, PassiveBluetoothEntityKey, - PassiveBluetoothProcessorCoordinator, PassiveBluetoothProcessorEntity, ) from homeassistant.components.sensor import ( @@ -38,7 +36,8 @@ from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import ARANET_MANUFACTURER_NAME, DOMAIN +from . import AranetConfigEntry +from .const import ARANET_MANUFACTURER_NAME @dataclass(frozen=True) @@ -174,20 +173,17 @@ def sensor_update_to_bluetooth_data_update( async def async_setup_entry( hass: HomeAssistant, - entry: config_entries.ConfigEntry, + entry: AranetConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Aranet sensors.""" - coordinator: PassiveBluetoothProcessorCoordinator[Aranet4Advertisement] = hass.data[ - DOMAIN - ][entry.entry_id] processor = PassiveBluetoothDataProcessor(sensor_update_to_bluetooth_data_update) entry.async_on_unload( processor.async_add_entities_listener( Aranet4BluetoothSensorEntity, async_add_entities ) ) - entry.async_on_unload(coordinator.async_register_processor(processor)) + entry.async_on_unload(entry.runtime_data.async_register_processor(processor)) class Aranet4BluetoothSensorEntity(