Migrate inkbird to use entry.runtime_data (#142780)

This commit is contained in:
J. Nick Koston 2025-04-12 00:20:07 -10:00 committed by GitHub
parent 4eda081574
commit 6b65b21ee0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 17 deletions

View File

@ -8,27 +8,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 CONF_DEVICE_TYPE, DOMAIN from .const import CONF_DEVICE_TYPE
from .coordinator import INKBIRDActiveBluetoothProcessorCoordinator from .coordinator import INKBIRDActiveBluetoothProcessorCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
INKBIRDConfigEntry = ConfigEntry[INKBIRDActiveBluetoothProcessorCoordinator]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: INKBIRDConfigEntry) -> bool:
"""Set up INKBIRD BLE device from a config entry.""" """Set up INKBIRD BLE device from a config entry."""
device_type: str | None = entry.data.get(CONF_DEVICE_TYPE) device_type: str | None = entry.data.get(CONF_DEVICE_TYPE)
data = INKBIRDBluetoothDeviceData(device_type) data = INKBIRDBluetoothDeviceData(device_type)
coordinator = INKBIRDActiveBluetoothProcessorCoordinator(hass, entry, data) coordinator = INKBIRDActiveBluetoothProcessorCoordinator(hass, entry, data)
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# 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()) entry.async_on_unload(coordinator.async_start())
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: INKBIRDConfigEntry) -> 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

@ -27,7 +27,9 @@ _LOGGER = logging.getLogger(__name__)
FALLBACK_POLL_INTERVAL = timedelta(seconds=180) FALLBACK_POLL_INTERVAL = timedelta(seconds=180)
class INKBIRDActiveBluetoothProcessorCoordinator(ActiveBluetoothProcessorCoordinator): class INKBIRDActiveBluetoothProcessorCoordinator(
ActiveBluetoothProcessorCoordinator[SensorUpdate]
):
"""Coordinator for INKBIRD Bluetooth devices.""" """Coordinator for INKBIRD Bluetooth devices."""
def __init__( def __init__(

View File

@ -4,12 +4,10 @@ from __future__ import annotations
from inkbird_ble import DeviceClass, DeviceKey, SensorUpdate, Units from inkbird_ble import DeviceClass, DeviceKey, 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,
PassiveBluetoothEntityKey, PassiveBluetoothEntityKey,
PassiveBluetoothProcessorCoordinator,
PassiveBluetoothProcessorEntity, PassiveBluetoothProcessorEntity,
) )
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -27,7 +25,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 INKBIRDConfigEntry
SENSOR_DESCRIPTIONS = { SENSOR_DESCRIPTIONS = {
(DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription( (DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription(
@ -97,20 +95,17 @@ 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: INKBIRDConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the INKBIRD BLE sensors.""" """Set up the INKBIRD BLE sensors."""
coordinator: PassiveBluetoothProcessorCoordinator = hass.data[DOMAIN][
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(
INKBIRDBluetoothSensorEntity, async_add_entities INKBIRDBluetoothSensorEntity, async_add_entities
) )
) )
entry.async_on_unload(coordinator.async_register_processor(processor)) entry.async_on_unload(entry.runtime_data.async_register_processor(processor))
class INKBIRDBluetoothSensorEntity( class INKBIRDBluetoothSensorEntity(