Use runtime_data in ipma (#144972)

* Use runtime_data in ipma

* Cleanup const
This commit is contained in:
epenet 2025-05-15 14:43:58 +02:00 committed by GitHub
parent e8281bb009
commit 28990e1db5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 32 additions and 41 deletions

View File

@ -1,6 +1,7 @@
"""Component for the Portuguese weather service - IPMA.""" """Component for the Portuguese weather service - IPMA."""
import asyncio import asyncio
from dataclasses import dataclass
import logging import logging
from pyipma import IPMAException from pyipma import IPMAException
@ -14,7 +15,6 @@ from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .config_flow import IpmaFlowHandler # noqa: F401 from .config_flow import IpmaFlowHandler # noqa: F401
from .const import DATA_API, DATA_LOCATION, DOMAIN
DEFAULT_NAME = "ipma" DEFAULT_NAME = "ipma"
@ -22,8 +22,18 @@ PLATFORMS = [Platform.SENSOR, Platform.WEATHER]
_LOGGER = logging.getLogger(__name__) _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.""" """Set up IPMA station as config entry."""
latitude = config_entry.data[CONF_LATITUDE] 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, location.global_id_local,
) )
hass.data.setdefault(DOMAIN, {}) config_entry.runtime_data = IpmaRuntimeData(api=api, location=location)
hass.data[DOMAIN][config_entry.entry_id] = {DATA_API: api, DATA_LOCATION: location}
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
return True 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.""" """Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
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

View File

@ -27,9 +27,6 @@ DOMAIN = "ipma"
HOME_LOCATION_NAME = "Home" HOME_LOCATION_NAME = "Home"
DATA_API = "api"
DATA_LOCATION = "location"
ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.ipma_{HOME_LOCATION_NAME}" ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.ipma_{HOME_LOCATION_NAME}"
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30)

View File

@ -4,20 +4,19 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import DATA_API, DATA_LOCATION, DOMAIN from . import IpmaConfigEntry
async def async_get_config_entry_diagnostics( async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry hass: HomeAssistant, entry: IpmaConfigEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a config entry.""" """Return diagnostics for a config entry."""
location = hass.data[DOMAIN][entry.entry_id][DATA_LOCATION] location = entry.runtime_data.location
api = hass.data[DOMAIN][entry.entry_id][DATA_API] api = entry.runtime_data.api
return { return {
"location_information": { "location_information": {

View File

@ -14,12 +14,12 @@ from pyipma.rcm import RCM
from pyipma.uv import UV from pyipma.uv import UV
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import Throttle 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 from .entity import IPMADevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -87,12 +87,12 @@ SENSOR_TYPES: tuple[IPMASensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: IpmaConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the IPMA sensor platform.""" """Set up the IPMA sensor platform."""
api = hass.data[DOMAIN][entry.entry_id][DATA_API] location = entry.runtime_data.location
location = hass.data[DOMAIN][entry.entry_id][DATA_LOCATION] api = entry.runtime_data.api
entities = [IPMASensor(api, location, description) for description in SENSOR_TYPES] entities = [IPMASensor(api, location, description) for description in SENSOR_TYPES]

View File

@ -23,7 +23,6 @@ from homeassistant.components.weather import (
WeatherEntity, WeatherEntity,
WeatherEntityFeature, WeatherEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_MODE, CONF_MODE,
UnitOfPressure, UnitOfPressure,
@ -35,14 +34,8 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.sun import is_up from homeassistant.helpers.sun import is_up
from homeassistant.util import Throttle from homeassistant.util import Throttle
from .const import ( from . import IpmaConfigEntry
ATTRIBUTION, from .const import ATTRIBUTION, CONDITION_MAP, MIN_TIME_BETWEEN_UPDATES
CONDITION_MAP,
DATA_API,
DATA_LOCATION,
DOMAIN,
MIN_TIME_BETWEEN_UPDATES,
)
from .entity import IPMADevice from .entity import IPMADevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -50,12 +43,12 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: IpmaConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Add a weather entity from a config_entry.""" """Add a weather entity from a config_entry."""
api = hass.data[DOMAIN][config_entry.entry_id][DATA_API] location = config_entry.runtime_data.location
location = hass.data[DOMAIN][config_entry.entry_id][DATA_LOCATION] api = config_entry.runtime_data.api
async_add_entities([IPMAWeather(api, location, config_entry)], True) async_add_entities([IPMAWeather(api, location, config_entry)], True)
@ -72,7 +65,7 @@ class IPMAWeather(WeatherEntity, IPMADevice):
) )
def __init__( def __init__(
self, api: IPMA_API, location: Location, config_entry: ConfigEntry self, api: IPMA_API, location: Location, config_entry: IpmaConfigEntry
) -> None: ) -> None:
"""Initialise the platform with a data instance and station name.""" """Initialise the platform with a data instance and station name."""
IPMADevice.__init__(self, api, location) IPMADevice.__init__(self, api, location)

View File

@ -4,7 +4,7 @@ from unittest.mock import patch
import pytest 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.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant

View File

@ -4,7 +4,7 @@ from unittest.mock import patch
from pyipma import IPMAException 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.config_entries import ConfigEntryState
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_MODE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant