2019-02-14 04:35:12 +00:00
|
|
|
"""Support for UV data from openuv.io."""
|
2021-07-24 12:50:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-08-29 15:56:12 +00:00
|
|
|
import asyncio
|
2021-07-24 12:50:01 +00:00
|
|
|
from typing import Any
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2019-12-06 21:13:43 +00:00
|
|
|
from pyopenuv import Client
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2022-10-27 08:22:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2018-08-02 05:42:12 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_BINARY_SENSORS,
|
|
|
|
CONF_ELEVATION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_SENSORS,
|
2021-12-06 03:06:35 +00:00
|
|
|
Platform,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-10-27 08:22:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import aiohttp_client
|
2022-10-21 01:37:20 +00:00
|
|
|
from homeassistant.helpers.entity import EntityDescription
|
2022-10-27 08:22:44 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2020-11-22 11:50:22 +00:00
|
|
|
from .const import (
|
2021-08-24 20:37:50 +00:00
|
|
|
CONF_FROM_WINDOW,
|
|
|
|
CONF_TO_WINDOW,
|
2020-11-22 11:50:22 +00:00
|
|
|
DATA_PROTECTION_WINDOW,
|
|
|
|
DATA_UV,
|
2021-08-24 20:37:50 +00:00
|
|
|
DEFAULT_FROM_WINDOW,
|
|
|
|
DEFAULT_TO_WINDOW,
|
2020-11-22 11:50:22 +00:00
|
|
|
DOMAIN,
|
|
|
|
LOGGER,
|
|
|
|
)
|
2022-11-08 14:41:09 +00:00
|
|
|
from .coordinator import InvalidApiKeyMonitor, OpenUvCoordinator
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-12-06 03:06:35 +00:00
|
|
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2021-10-05 21:03:39 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-09-04 07:22:44 +00:00
|
|
|
"""Set up OpenUV as config entry."""
|
2021-11-19 20:53:47 +00:00
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
2022-10-21 01:37:20 +00:00
|
|
|
client = Client(
|
|
|
|
entry.data[CONF_API_KEY],
|
|
|
|
entry.data.get(CONF_LATITUDE, hass.config.latitude),
|
|
|
|
entry.data.get(CONF_LONGITUDE, hass.config.longitude),
|
|
|
|
altitude=entry.data.get(CONF_ELEVATION, hass.config.elevation),
|
|
|
|
session=websession,
|
2021-11-19 20:53:47 +00:00
|
|
|
)
|
|
|
|
|
2022-10-21 01:37:20 +00:00
|
|
|
async def async_update_protection_data() -> dict[str, Any]:
|
|
|
|
"""Update binary sensor (protection window) data."""
|
|
|
|
low = entry.options.get(CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW)
|
|
|
|
high = entry.options.get(CONF_TO_WINDOW, DEFAULT_TO_WINDOW)
|
|
|
|
return await client.uv_protection_window(low=low, high=high)
|
2021-11-19 20:53:47 +00:00
|
|
|
|
2022-11-08 14:41:09 +00:00
|
|
|
invalid_api_key_monitor = InvalidApiKeyMonitor(hass, entry)
|
|
|
|
|
2022-10-21 01:37:20 +00:00
|
|
|
coordinators: dict[str, OpenUvCoordinator] = {
|
|
|
|
coordinator_name: OpenUvCoordinator(
|
|
|
|
hass,
|
|
|
|
name=coordinator_name,
|
|
|
|
latitude=client.latitude,
|
|
|
|
longitude=client.longitude,
|
|
|
|
update_method=update_method,
|
2022-11-08 14:41:09 +00:00
|
|
|
invalid_api_key_monitor=invalid_api_key_monitor,
|
2022-10-21 01:37:20 +00:00
|
|
|
)
|
|
|
|
for coordinator_name, update_method in (
|
|
|
|
(DATA_UV, client.uv_index),
|
|
|
|
(DATA_PROTECTION_WINDOW, async_update_protection_data),
|
|
|
|
)
|
|
|
|
}
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2022-10-21 01:37:20 +00:00
|
|
|
# We disable the client's request retry abilities here to avoid a lengthy (and
|
|
|
|
# blocking) startup; then, if the initial update is successful, we re-enable client
|
|
|
|
# request retries:
|
|
|
|
client.disable_request_retries()
|
|
|
|
init_tasks = [
|
|
|
|
coordinator.async_config_entry_first_refresh()
|
|
|
|
for coordinator in coordinators.values()
|
|
|
|
]
|
|
|
|
await asyncio.gather(*init_tasks)
|
|
|
|
client.enable_request_retries()
|
2021-11-19 20:53:47 +00:00
|
|
|
|
2021-11-14 18:06:27 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2022-10-21 01:37:20 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = coordinators
|
2021-11-14 18:06:27 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-10-05 21:03:39 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2018-09-04 07:22:44 +00:00
|
|
|
"""Unload an OpenUV config entry."""
|
2021-10-05 21:03:39 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2020-05-27 22:48:28 +00:00
|
|
|
if unload_ok:
|
2021-10-17 05:59:19 +00:00
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2018-10-15 19:21:21 +00:00
|
|
|
|
2020-05-27 22:48:28 +00:00
|
|
|
return unload_ok
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
|
2021-10-05 21:03:39 +00:00
|
|
|
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2020-01-23 01:48:20 +00:00
|
|
|
"""Migrate the config entry upon new versions."""
|
2021-10-05 21:03:39 +00:00
|
|
|
version = entry.version
|
|
|
|
data = {**entry.data}
|
2020-01-23 01:48:20 +00:00
|
|
|
|
2020-11-22 11:50:22 +00:00
|
|
|
LOGGER.debug("Migrating from version %s", version)
|
2020-01-23 01:48:20 +00:00
|
|
|
|
|
|
|
# 1 -> 2: Remove unused condition data:
|
|
|
|
if version == 1:
|
|
|
|
data.pop(CONF_BINARY_SENSORS, None)
|
|
|
|
data.pop(CONF_SENSORS, None)
|
2021-10-05 21:03:39 +00:00
|
|
|
version = entry.version = 2
|
|
|
|
hass.config_entries.async_update_entry(entry, data=data)
|
2020-11-22 11:50:22 +00:00
|
|
|
LOGGER.debug("Migration to version %s successful", version)
|
2020-01-23 01:48:20 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-10-21 01:37:20 +00:00
|
|
|
class OpenUvEntity(CoordinatorEntity):
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Define a generic OpenUV entity."""
|
|
|
|
|
2022-07-10 20:45:09 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
|
2022-10-21 01:37:20 +00:00
|
|
|
def __init__(
|
|
|
|
self, coordinator: OpenUvCoordinator, description: EntityDescription
|
|
|
|
) -> None:
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Initialize."""
|
2022-10-21 01:37:20 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
|
2021-11-14 18:06:27 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2022-09-18 15:24:13 +00:00
|
|
|
self._attr_unique_id = (
|
2022-10-21 01:37:20 +00:00
|
|
|
f"{coordinator.latitude}_{coordinator.longitude}_{description.key}"
|
2022-09-18 15:24:13 +00:00
|
|
|
)
|
2021-08-25 12:18:53 +00:00
|
|
|
self.entity_description = description
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2022-09-17 23:56:45 +00:00
|
|
|
@callback
|
2022-10-21 01:37:20 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Respond to a DataUpdateCoordinator update."""
|
|
|
|
self._update_from_latest_data()
|
2022-09-17 23:56:45 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2022-10-21 01:37:20 +00:00
|
|
|
@callback
|
|
|
|
def _update_from_latest_data(self) -> None:
|
|
|
|
"""Update the entity from the latest data."""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-07-24 12:50:01 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2022-10-21 01:37:20 +00:00
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self._update_from_latest_data()
|