diff --git a/homeassistant/components/openexchangerates/__init__.py b/homeassistant/components/openexchangerates/__init__.py index ed704a61fed9..4559c098acbf 100644 --- a/homeassistant/components/openexchangerates/__init__.py +++ b/homeassistant/components/openexchangerates/__init__.py @@ -2,31 +2,28 @@ from __future__ import annotations -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, CONF_BASE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import BASE_UPDATE_INTERVAL, DOMAIN, LOGGER -from .coordinator import OpenexchangeratesCoordinator +from .coordinator import OpenexchangeratesConfigEntry, OpenexchangeratesCoordinator PLATFORMS = [Platform.SENSOR] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry( + hass: HomeAssistant, entry: OpenexchangeratesConfigEntry +) -> bool: """Set up Open Exchange Rates from a config entry.""" api_key: str = entry.data[CONF_API_KEY] base: str = entry.data[CONF_BASE] # Create one coordinator per base currency per API key. - existing_coordinators: dict[str, OpenexchangeratesCoordinator] = hass.data.get( - DOMAIN, {} - ) existing_coordinator_for_api_key = { - existing_coordinator - for config_entry_id, existing_coordinator in existing_coordinators.items() - if (config_entry := hass.config_entries.async_get_entry(config_entry_id)) - and config_entry.data[CONF_API_KEY] == api_key + existing_entry.runtime_data + for existing_entry in hass.config_entries.async_loaded_entries(DOMAIN) + if existing_entry.data[CONF_API_KEY] == api_key } # Adjust update interval by coordinators per API key. @@ -48,16 +45,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: 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) return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, entry: OpenexchangeratesConfigEntry +) -> 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/openexchangerates/coordinator.py b/homeassistant/components/openexchangerates/coordinator.py index 6245877ddbdd..295e6f33d729 100644 --- a/homeassistant/components/openexchangerates/coordinator.py +++ b/homeassistant/components/openexchangerates/coordinator.py @@ -20,16 +20,18 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda from .const import CLIENT_TIMEOUT, DOMAIN, LOGGER +type OpenexchangeratesConfigEntry = ConfigEntry[OpenexchangeratesCoordinator] + class OpenexchangeratesCoordinator(DataUpdateCoordinator[Latest]): """Represent a coordinator for Open Exchange Rates API.""" - config_entry: ConfigEntry + config_entry: OpenexchangeratesConfigEntry def __init__( self, hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: OpenexchangeratesConfigEntry, session: ClientSession, api_key: str, base: str, diff --git a/homeassistant/components/openexchangerates/sensor.py b/homeassistant/components/openexchangerates/sensor.py index 756823ff0ece..cb493ab5e849 100644 --- a/homeassistant/components/openexchangerates/sensor.py +++ b/homeassistant/components/openexchangerates/sensor.py @@ -3,7 +3,6 @@ from __future__ import annotations from homeassistant.components.sensor import SensorEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_QUOTE from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo @@ -11,19 +10,19 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN -from .coordinator import OpenexchangeratesCoordinator +from .coordinator import OpenexchangeratesConfigEntry, OpenexchangeratesCoordinator ATTRIBUTION = "Data provided by openexchangerates.org" async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: OpenexchangeratesConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the Open Exchange Rates sensor.""" quote: str = config_entry.data.get(CONF_QUOTE, "EUR") - coordinator = hass.data[DOMAIN][config_entry.entry_id] + coordinator = config_entry.runtime_data async_add_entities( OpenexchangeratesSensor( @@ -43,7 +42,7 @@ class OpenexchangeratesSensor( def __init__( self, - config_entry: ConfigEntry, + config_entry: OpenexchangeratesConfigEntry, coordinator: OpenexchangeratesCoordinator, quote: str, enabled: bool,