2021-02-13 20:53:28 +00:00
|
|
|
"""The AEMET OpenData component."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from aemet_opendata.interface import AEMET
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
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
|
|
|
|
|
|
|
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-05-27 13:56:20 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
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
|