2019-02-14 05:35:12 +01:00
|
|
|
"""Support for UV data from openuv.io."""
|
2024-03-08 15:04:07 +01:00
|
|
|
|
2019-08-29 11:56:12 -04:00
|
|
|
import asyncio
|
2021-07-24 06:50:01 -06:00
|
|
|
from typing import Any
|
2018-08-01 23:42:12 -06:00
|
|
|
|
2019-12-06 22:13:43 +01:00
|
|
|
from pyopenuv import Client
|
2018-08-01 23:42:12 -06:00
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
|
CONF_API_KEY,
|
|
|
|
|
CONF_BINARY_SENSORS,
|
|
|
|
|
CONF_ELEVATION,
|
2019-02-14 05:35:12 +01:00
|
|
|
CONF_LATITUDE,
|
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
|
CONF_SENSORS,
|
2021-12-06 04:06:35 +01:00
|
|
|
Platform,
|
2019-02-14 05:35:12 +01:00
|
|
|
)
|
2023-10-13 18:12:00 -06:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-10-27 02:22:44 -06:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
2018-08-01 23:42:12 -06:00
|
|
|
|
2020-11-22 04:50:22 -07:00
|
|
|
from .const import (
|
2021-08-24 14:37:50 -06:00
|
|
|
CONF_FROM_WINDOW,
|
|
|
|
|
CONF_TO_WINDOW,
|
2020-11-22 04:50:22 -07:00
|
|
|
DATA_PROTECTION_WINDOW,
|
|
|
|
|
DATA_UV,
|
2021-08-24 14:37:50 -06:00
|
|
|
DEFAULT_FROM_WINDOW,
|
|
|
|
|
DEFAULT_TO_WINDOW,
|
2020-11-22 04:50:22 -07:00
|
|
|
LOGGER,
|
|
|
|
|
)
|
2026-04-01 11:34:10 +02:00
|
|
|
from .coordinator import (
|
|
|
|
|
OpenUvConfigEntry,
|
|
|
|
|
OpenUvCoordinator,
|
|
|
|
|
OpenUvProtectionWindowCoordinator,
|
|
|
|
|
)
|
2018-08-01 23:42:12 -06:00
|
|
|
|
2021-12-06 04:06:35 +01:00
|
|
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
2018-08-01 23:42:12 -06:00
|
|
|
|
|
|
|
|
|
2026-04-01 11:34:10 +02:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: OpenUvConfigEntry) -> bool:
|
2018-09-04 01:22:44 -06:00
|
|
|
"""Set up OpenUV as config entry."""
|
2021-11-19 13:53:47 -07:00
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
2022-10-20 19:37:20 -06: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,
|
2023-01-10 01:48:39 -07:00
|
|
|
check_status_before_request=True,
|
2021-11-19 13:53:47 -07:00
|
|
|
)
|
|
|
|
|
|
2022-10-20 19:37:20 -06: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)
|
|
|
|
|
|
|
|
|
|
coordinators: dict[str, OpenUvCoordinator] = {
|
2025-09-10 13:18:50 -07:00
|
|
|
coordinator_name: coordinator_cls(
|
2022-10-20 19:37:20 -06:00
|
|
|
hass,
|
2023-01-10 01:48:39 -07:00
|
|
|
entry=entry,
|
2022-10-20 19:37:20 -06:00
|
|
|
name=coordinator_name,
|
|
|
|
|
latitude=client.latitude,
|
|
|
|
|
longitude=client.longitude,
|
|
|
|
|
update_method=update_method,
|
|
|
|
|
)
|
2025-09-10 13:18:50 -07:00
|
|
|
for coordinator_cls, coordinator_name, update_method in (
|
|
|
|
|
(OpenUvCoordinator, DATA_UV, client.uv_index),
|
|
|
|
|
(
|
|
|
|
|
OpenUvProtectionWindowCoordinator,
|
|
|
|
|
DATA_PROTECTION_WINDOW,
|
|
|
|
|
async_update_protection_data,
|
|
|
|
|
),
|
2022-10-20 19:37:20 -06:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init_tasks = [
|
|
|
|
|
coordinator.async_config_entry_first_refresh()
|
|
|
|
|
for coordinator in coordinators.values()
|
|
|
|
|
]
|
|
|
|
|
await asyncio.gather(*init_tasks)
|
2021-11-19 13:53:47 -07:00
|
|
|
|
2026-04-01 11:34:10 +02:00
|
|
|
entry.runtime_data = coordinators
|
2021-11-14 11:06:27 -07:00
|
|
|
|
2022-07-09 10:27:42 -05:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2018-08-01 23:42:12 -06:00
|
|
|
|
2018-09-04 01:22:44 -06:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2026-04-01 11:34:10 +02:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: OpenUvConfigEntry) -> bool:
|
2018-09-04 01:22:44 -06:00
|
|
|
"""Unload an OpenUV config entry."""
|
2026-04-01 11:34:10 +02:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2018-08-01 23:42:12 -06:00
|
|
|
|
|
|
|
|
|
2026-04-01 11:34:10 +02:00
|
|
|
async def async_migrate_entry(hass: HomeAssistant, entry: OpenUvConfigEntry) -> bool:
|
2020-01-22 18:48:20 -07:00
|
|
|
"""Migrate the config entry upon new versions."""
|
2021-10-05 15:03:39 -06:00
|
|
|
version = entry.version
|
|
|
|
|
data = {**entry.data}
|
2020-01-22 18:48:20 -07:00
|
|
|
|
2020-11-22 04:50:22 -07:00
|
|
|
LOGGER.debug("Migrating from version %s", version)
|
2020-01-22 18:48:20 -07:00
|
|
|
|
|
|
|
|
# 1 -> 2: Remove unused condition data:
|
|
|
|
|
if version == 1:
|
|
|
|
|
data.pop(CONF_BINARY_SENSORS, None)
|
|
|
|
|
data.pop(CONF_SENSORS, None)
|
2024-02-12 11:34:26 -06:00
|
|
|
version = 2
|
|
|
|
|
hass.config_entries.async_update_entry(entry, data=data, version=2)
|
2020-11-22 04:50:22 -07:00
|
|
|
LOGGER.debug("Migration to version %s successful", version)
|
2020-01-22 18:48:20 -07:00
|
|
|
|
|
|
|
|
return True
|