2021-02-13 20:53:28 +00:00
|
|
|
"""The AEMET OpenData component."""
|
2022-07-04 12:06:32 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-02-13 20:53:28 +00:00
|
|
|
import logging
|
2022-07-04 12:06:32 +00:00
|
|
|
from typing import Any
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
from aemet_opendata.interface import AEMET
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-07-04 12:06:32 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_NAME,
|
|
|
|
Platform,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
2021-02-13 20:53:28 +00:00
|
|
|
|
2021-05-14 11:28:48 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_STATION_UPDATES,
|
|
|
|
DOMAIN,
|
|
|
|
ENTRY_NAME,
|
|
|
|
ENTRY_WEATHER_COORDINATOR,
|
2022-07-04 12:06:32 +00:00
|
|
|
FORECAST_MODES,
|
2021-05-14 11:28:48 +00:00
|
|
|
PLATFORMS,
|
2022-07-04 12:06:32 +00:00
|
|
|
RENAMED_FORECAST_SENSOR_KEYS,
|
2021-05-14 11:28:48 +00:00
|
|
|
)
|
2021-02-13 20:53:28 +00:00
|
|
|
from .weather_update_coordinator import WeatherUpdateCoordinator
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-05-27 15:39:06 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Set up AEMET OpenData as config entry."""
|
2022-07-04 12:06:32 +00:00
|
|
|
await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry)
|
|
|
|
|
2021-05-27 13:56:20 +00:00
|
|
|
name = entry.data[CONF_NAME]
|
|
|
|
api_key = entry.data[CONF_API_KEY]
|
|
|
|
latitude = entry.data[CONF_LATITUDE]
|
|
|
|
longitude = entry.data[CONF_LONGITUDE]
|
|
|
|
station_updates = entry.options.get(CONF_STATION_UPDATES, True)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
aemet = AEMET(api_key)
|
2021-05-14 11:28:48 +00:00
|
|
|
weather_coordinator = WeatherUpdateCoordinator(
|
|
|
|
hass, aemet, latitude, longitude, station_updates
|
|
|
|
)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
2021-03-29 22:51:39 +00:00
|
|
|
await weather_coordinator.async_config_entry_first_refresh()
|
2021-02-13 20:53:28 +00:00
|
|
|
|
2021-03-29 23:23:44 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2021-05-27 13:56:20 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = {
|
2021-02-13 20:53:28 +00:00
|
|
|
ENTRY_NAME: name,
|
|
|
|
ENTRY_WEATHER_COORDINATOR: weather_coordinator,
|
|
|
|
}
|
|
|
|
|
2021-05-27 13:56:20 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
2021-05-27 13:56:20 +00:00
|
|
|
entry.async_on_unload(entry.add_update_listener(async_update_options))
|
2021-05-14 11:28:48 +00:00
|
|
|
|
2021-02-13 20:53:28 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-05-27 13:56:20 +00:00
|
|
|
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2021-05-14 11:28:48 +00:00
|
|
|
"""Update options."""
|
2021-05-27 13:56:20 +00:00
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
2021-05-14 11:28:48 +00:00
|
|
|
|
|
|
|
|
2021-10-06 08:48:11 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Unload a config entry."""
|
2021-05-27 13:56:20 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-04-26 17:46:55 +00:00
|
|
|
|
2021-02-13 20:53:28 +00:00
|
|
|
if unload_ok:
|
2021-05-27 13:56:20 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
return unload_ok
|
2022-07-04 12:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_migrate_entity_entry(entry: er.RegistryEntry) -> dict[str, Any] | None:
|
|
|
|
"""Migrate AEMET entity entries.
|
|
|
|
|
|
|
|
- Migrates unique ID from old forecast sensors to the new unique ID
|
|
|
|
"""
|
|
|
|
if entry.domain != Platform.SENSOR:
|
|
|
|
return None
|
|
|
|
for old_key, new_key in RENAMED_FORECAST_SENSOR_KEYS.items():
|
|
|
|
for forecast_mode in FORECAST_MODES:
|
|
|
|
old_suffix = f"-forecast-{forecast_mode}-{old_key}"
|
|
|
|
if entry.unique_id.endswith(old_suffix):
|
|
|
|
new_suffix = f"-forecast-{forecast_mode}-{new_key}"
|
|
|
|
return {
|
|
|
|
"new_unique_id": entry.unique_id.replace(old_suffix, new_suffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
# No migration needed
|
|
|
|
return None
|