diff --git a/homeassistant/components/ipma/__init__.py b/homeassistant/components/ipma/__init__.py index 68289d13289..6c48ae4c925 100644 --- a/homeassistant/components/ipma/__init__.py +++ b/homeassistant/components/ipma/__init__.py @@ -1,6 +1,7 @@ """Component for the Portuguese weather service - IPMA.""" import asyncio +from dataclasses import dataclass import logging from pyipma import IPMAException @@ -14,7 +15,6 @@ from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession from .config_flow import IpmaFlowHandler # noqa: F401 -from .const import DATA_API, DATA_LOCATION, DOMAIN DEFAULT_NAME = "ipma" @@ -22,8 +22,18 @@ PLATFORMS = [Platform.SENSOR, Platform.WEATHER] _LOGGER = logging.getLogger(__name__) +type IpmaConfigEntry = ConfigEntry[IpmaRuntimeData] -async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: + +@dataclass +class IpmaRuntimeData: + """IPMA runtime data.""" + + api: IPMA_API + location: Location + + +async def async_setup_entry(hass: HomeAssistant, config_entry: IpmaConfigEntry) -> bool: """Set up IPMA station as config entry.""" latitude = config_entry.data[CONF_LATITUDE] @@ -48,20 +58,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b location.global_id_local, ) - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN][config_entry.entry_id] = {DATA_API: api, DATA_LOCATION: location} + config_entry.runtime_data = IpmaRuntimeData(api=api, location=location) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: IpmaConfigEntry) -> bool: """Unload a config entry.""" - - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - - if not hass.data[DOMAIN]: - hass.data.pop(DOMAIN) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/ipma/const.py b/homeassistant/components/ipma/const.py index dd6f1fba64a..1cb1af17d95 100644 --- a/homeassistant/components/ipma/const.py +++ b/homeassistant/components/ipma/const.py @@ -27,9 +27,6 @@ DOMAIN = "ipma" HOME_LOCATION_NAME = "Home" -DATA_API = "api" -DATA_LOCATION = "location" - ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.ipma_{HOME_LOCATION_NAME}" MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) diff --git a/homeassistant/components/ipma/diagnostics.py b/homeassistant/components/ipma/diagnostics.py index 948b69ee3e5..bf868324593 100644 --- a/homeassistant/components/ipma/diagnostics.py +++ b/homeassistant/components/ipma/diagnostics.py @@ -4,20 +4,19 @@ from __future__ import annotations from typing import Any -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE from homeassistant.core import HomeAssistant -from .const import DATA_API, DATA_LOCATION, DOMAIN +from . import IpmaConfigEntry async def async_get_config_entry_diagnostics( - hass: HomeAssistant, entry: ConfigEntry + hass: HomeAssistant, entry: IpmaConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - location = hass.data[DOMAIN][entry.entry_id][DATA_LOCATION] - api = hass.data[DOMAIN][entry.entry_id][DATA_API] + location = entry.runtime_data.location + api = entry.runtime_data.api return { "location_information": { diff --git a/homeassistant/components/ipma/sensor.py b/homeassistant/components/ipma/sensor.py index 78fd018cf9a..7e71457513b 100644 --- a/homeassistant/components/ipma/sensor.py +++ b/homeassistant/components/ipma/sensor.py @@ -14,12 +14,12 @@ from pyipma.rcm import RCM from pyipma.uv import UV from homeassistant.components.sensor import SensorEntity, SensorEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util import Throttle -from .const import DATA_API, DATA_LOCATION, DOMAIN, MIN_TIME_BETWEEN_UPDATES +from . import IpmaConfigEntry +from .const import MIN_TIME_BETWEEN_UPDATES from .entity import IPMADevice _LOGGER = logging.getLogger(__name__) @@ -87,12 +87,12 @@ SENSOR_TYPES: tuple[IPMASensorEntityDescription, ...] = ( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: IpmaConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the IPMA sensor platform.""" - api = hass.data[DOMAIN][entry.entry_id][DATA_API] - location = hass.data[DOMAIN][entry.entry_id][DATA_LOCATION] + location = entry.runtime_data.location + api = entry.runtime_data.api entities = [IPMASensor(api, location, description) for description in SENSOR_TYPES] diff --git a/homeassistant/components/ipma/weather.py b/homeassistant/components/ipma/weather.py index d285f9e1ad3..74344da8aff 100644 --- a/homeassistant/components/ipma/weather.py +++ b/homeassistant/components/ipma/weather.py @@ -23,7 +23,6 @@ from homeassistant.components.weather import ( WeatherEntity, WeatherEntityFeature, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_MODE, UnitOfPressure, @@ -35,14 +34,8 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.sun import is_up from homeassistant.util import Throttle -from .const import ( - ATTRIBUTION, - CONDITION_MAP, - DATA_API, - DATA_LOCATION, - DOMAIN, - MIN_TIME_BETWEEN_UPDATES, -) +from . import IpmaConfigEntry +from .const import ATTRIBUTION, CONDITION_MAP, MIN_TIME_BETWEEN_UPDATES from .entity import IPMADevice _LOGGER = logging.getLogger(__name__) @@ -50,12 +43,12 @@ _LOGGER = logging.getLogger(__name__) async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: IpmaConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Add a weather entity from a config_entry.""" - api = hass.data[DOMAIN][config_entry.entry_id][DATA_API] - location = hass.data[DOMAIN][config_entry.entry_id][DATA_LOCATION] + location = config_entry.runtime_data.location + api = config_entry.runtime_data.api async_add_entities([IPMAWeather(api, location, config_entry)], True) @@ -72,7 +65,7 @@ class IPMAWeather(WeatherEntity, IPMADevice): ) def __init__( - self, api: IPMA_API, location: Location, config_entry: ConfigEntry + self, api: IPMA_API, location: Location, config_entry: IpmaConfigEntry ) -> None: """Initialise the platform with a data instance and station name.""" IPMADevice.__init__(self, api, location) diff --git a/tests/components/ipma/conftest.py b/tests/components/ipma/conftest.py index 8f2a017dcb8..caf49f594fb 100644 --- a/tests/components/ipma/conftest.py +++ b/tests/components/ipma/conftest.py @@ -4,7 +4,7 @@ from unittest.mock import patch import pytest -from homeassistant.components.ipma import DOMAIN +from homeassistant.components.ipma.const import DOMAIN from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME from homeassistant.core import HomeAssistant diff --git a/tests/components/ipma/test_init.py b/tests/components/ipma/test_init.py index 7967b97dd23..4a0314a0d9a 100644 --- a/tests/components/ipma/test_init.py +++ b/tests/components/ipma/test_init.py @@ -4,7 +4,7 @@ from unittest.mock import patch from pyipma import IPMAException -from homeassistant.components.ipma import DOMAIN +from homeassistant.components.ipma.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE from homeassistant.core import HomeAssistant