2021-02-13 20:53:28 +00:00
|
|
|
"""The AEMET OpenData component."""
|
2023-08-29 13:43:14 +00:00
|
|
|
|
2023-10-19 08:42:31 +00:00
|
|
|
import asyncio
|
2021-02-13 20:53:28 +00:00
|
|
|
import logging
|
|
|
|
|
2023-08-29 13:43:14 +00:00
|
|
|
from aemet_opendata.exceptions import TownNotFound
|
2023-08-25 10:53:26 +00:00
|
|
|
from aemet_opendata.interface import AEMET, ConnectionOptions
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-07-05 20:24:08 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-10-19 08:42:31 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2023-08-22 21:21:42 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
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,
|
|
|
|
PLATFORMS,
|
|
|
|
)
|
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."""
|
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
|
|
|
|
2023-09-24 20:18:31 +00:00
|
|
|
options = ConnectionOptions(api_key, station_updates, True)
|
2023-08-25 10:53:26 +00:00
|
|
|
aemet = AEMET(aiohttp_client.async_get_clientsession(hass), options)
|
2023-08-29 13:43:14 +00:00
|
|
|
try:
|
|
|
|
await aemet.select_coordinates(latitude, longitude)
|
|
|
|
except TownNotFound as err:
|
|
|
|
_LOGGER.error(err)
|
|
|
|
return False
|
2023-10-19 08:42:31 +00:00
|
|
|
except asyncio.TimeoutError as err:
|
|
|
|
raise ConfigEntryNotReady("AEMET OpenData API timed out") from err
|
2021-02-13 20:53:28 +00:00
|
|
|
|
2023-08-29 13:43:14 +00:00
|
|
|
weather_coordinator = WeatherUpdateCoordinator(hass, aemet)
|
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,
|
|
|
|
}
|
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(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
|