Use config entry runtime_data in arve (#127078)

This commit is contained in:
epenet 2024-09-30 10:06:16 +02:00 committed by GitHub
parent b035649c75
commit dbecd7a99c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 16 deletions

View File

@ -2,33 +2,28 @@
from __future__ import annotations from __future__ import annotations
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 from .coordinator import ArveConfigEntry, ArveCoordinator
from .coordinator import ArveCoordinator
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ArveConfigEntry) -> bool:
"""Set up Arve from a config entry.""" """Set up Arve from a config entry."""
coordinator = ArveCoordinator(hass) coordinator = ArveCoordinator(hass)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
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)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ArveConfigEntry) -> 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

@ -21,11 +21,13 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
from .const import DOMAIN, LOGGER from .const import DOMAIN, LOGGER
type ArveConfigEntry = ConfigEntry[ArveCoordinator]
class ArveCoordinator(DataUpdateCoordinator[ArveSensProData]): class ArveCoordinator(DataUpdateCoordinator[ArveSensProData]):
"""Arve coordinator.""" """Arve coordinator."""
config_entry: ConfigEntry config_entry: ArveConfigEntry
devices: ArveDevices devices: ArveDevices
def __init__(self, hass: HomeAssistant) -> None: def __init__(self, hass: HomeAssistant) -> None:

View File

@ -11,7 +11,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
@ -21,8 +20,7 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from .coordinator import ArveConfigEntry
from .coordinator import ArveCoordinator
from .entity import ArveDeviceEntity from .entity import ArveDeviceEntity
@ -85,10 +83,10 @@ SENSORS: tuple[ArveDeviceEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ArveConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
"""Set up Arve device based on a config entry.""" """Set up Arve device based on a config entry."""
coordinator: ArveCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
async_add_entities( async_add_entities(
ArveDevice(coordinator, description, sn) ArveDevice(coordinator, description, sn)