From 0f5734779784e2ff26dd94d435c44bc680d50602 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 4 Feb 2025 08:44:24 +0100 Subject: [PATCH] Use runtime_data in fastdotcom (#137293) --- .../components/fastdotcom/__init__.py | 18 ++++++++---------- .../components/fastdotcom/coordinator.py | 6 +++++- .../components/fastdotcom/diagnostics.py | 14 +++----------- homeassistant/components/fastdotcom/sensor.py | 8 +++----- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/fastdotcom/__init__.py b/homeassistant/components/fastdotcom/__init__.py index 967e7ef8e35..59cb3f984d2 100644 --- a/homeassistant/components/fastdotcom/__init__.py +++ b/homeassistant/components/fastdotcom/__init__.py @@ -4,20 +4,20 @@ from __future__ import annotations import logging -from homeassistant.config_entries import ConfigEntry, ConfigEntryState +from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.helpers.start import async_at_started -from .const import DOMAIN, PLATFORMS -from .coordinator import FastdotcomDataUpdateCoordinator +from .const import PLATFORMS +from .coordinator import FastdotcomConfigEntry, FastdotcomDataUpdateCoordinator _LOGGER = logging.getLogger(__name__) -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: FastdotcomConfigEntry) -> bool: """Set up Fast.com from a config entry.""" - coordinator = FastdotcomDataUpdateCoordinator(hass) - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator + coordinator = FastdotcomDataUpdateCoordinator(hass, entry) + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups( entry, @@ -36,8 +36,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: FastdotcomConfigEntry) -> bool: """Unload Fast.com 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/fastdotcom/coordinator.py b/homeassistant/components/fastdotcom/coordinator.py index 75ac55b8314..8365692804c 100644 --- a/homeassistant/components/fastdotcom/coordinator.py +++ b/homeassistant/components/fastdotcom/coordinator.py @@ -6,20 +6,24 @@ from datetime import timedelta from fastdotcom import fast_com +from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DEFAULT_INTERVAL, DOMAIN, LOGGER +type FastdotcomConfigEntry = ConfigEntry[FastdotcomDataUpdateCoordinator] + class FastdotcomDataUpdateCoordinator(DataUpdateCoordinator[float]): """Class to manage fetching Fast.com data API.""" - def __init__(self, hass: HomeAssistant) -> None: + def __init__(self, hass: HomeAssistant, entry: FastdotcomConfigEntry) -> None: """Initialize the coordinator for Fast.com.""" super().__init__( hass, LOGGER, + config_entry=entry, name=DOMAIN, update_interval=timedelta(hours=DEFAULT_INTERVAL), ) diff --git a/homeassistant/components/fastdotcom/diagnostics.py b/homeassistant/components/fastdotcom/diagnostics.py index d7383ef0c6a..42f4e32f49e 100644 --- a/homeassistant/components/fastdotcom/diagnostics.py +++ b/homeassistant/components/fastdotcom/diagnostics.py @@ -4,21 +4,13 @@ from __future__ import annotations from typing import Any -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from .const import DOMAIN -from .coordinator import FastdotcomDataUpdateCoordinator +from .coordinator import FastdotcomConfigEntry async def async_get_config_entry_diagnostics( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: FastdotcomConfigEntry ) -> dict[str, Any]: """Return diagnostics for the config entry.""" - coordinator: FastdotcomDataUpdateCoordinator = hass.data[DOMAIN][ - config_entry.entry_id - ] - - return { - "coordinator_data": coordinator.data, - } + return {"coordinator_data": config_entry.runtime_data.data} diff --git a/homeassistant/components/fastdotcom/sensor.py b/homeassistant/components/fastdotcom/sensor.py index 721290e8c0d..b633cb25628 100644 --- a/homeassistant/components/fastdotcom/sensor.py +++ b/homeassistant/components/fastdotcom/sensor.py @@ -7,7 +7,6 @@ from homeassistant.components.sensor import ( SensorEntity, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfDataRate from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo @@ -15,17 +14,16 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN -from .coordinator import FastdotcomDataUpdateCoordinator +from .coordinator import FastdotcomConfigEntry, FastdotcomDataUpdateCoordinator async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: FastdotcomConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Fast.com sensor.""" - coordinator: FastdotcomDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] - async_add_entities([SpeedtestSensor(entry.entry_id, coordinator)]) + async_add_entities([SpeedtestSensor(entry.entry_id, entry.runtime_data)]) class SpeedtestSensor(CoordinatorEntity[FastdotcomDataUpdateCoordinator], SensorEntity):