mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use runtime_data in ipma (#144972)
* Use runtime_data in ipma * Cleanup const
This commit is contained in:
parent
e8281bb009
commit
28990e1db5
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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": {
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user