2019-09-27 21:18:34 +00:00
|
|
|
"""Support for the Hive devices and services."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2022-04-28 18:52:08 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Awaitable, Callable, Coroutine
|
2019-09-27 21:18:34 +00:00
|
|
|
from functools import wraps
|
2018-01-15 22:24:12 +00:00
|
|
|
import logging
|
2024-05-17 14:27:02 +00:00
|
|
|
from typing import Any, Concatenate
|
2019-02-14 15:01:46 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from aiohttp.web_exceptions import HTTPException
|
2022-06-30 21:47:02 +00:00
|
|
|
from apyhiveapi import Auth, Hive
|
2021-03-15 11:27:10 +00:00
|
|
|
from apyhiveapi.helper.hive_exceptions import HiveReauthRequired
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-01-02 15:31:48 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-12-05 21:16:18 +00:00
|
|
|
from homeassistant.const import CONF_SCAN_INTERVAL
|
2022-01-02 15:31:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-10 05:41:29 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2024-12-05 21:16:18 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client
|
2024-09-17 13:44:52 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntry
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from .const import DOMAIN, PLATFORM_LOOKUP, PLATFORMS
|
2024-09-17 13:44:52 +00:00
|
|
|
from .entity import HiveEntity
|
2019-02-14 15:01:46 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
|
2022-01-02 15:31:48 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-03-15 11:27:10 +00:00
|
|
|
"""Set up Hive from a config entry."""
|
2024-12-05 21:16:18 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2021-03-15 11:27:10 +00:00
|
|
|
|
2022-06-02 21:54:26 +00:00
|
|
|
web_session = aiohttp_client.async_get_clientsession(hass)
|
2021-03-15 11:27:10 +00:00
|
|
|
hive_config = dict(entry.data)
|
2022-06-02 21:54:26 +00:00
|
|
|
hive = Hive(web_session)
|
2021-03-15 11:27:10 +00:00
|
|
|
|
|
|
|
hive_config["options"] = {}
|
|
|
|
hive_config["options"].update(
|
|
|
|
{CONF_SCAN_INTERVAL: dict(entry.options).get(CONF_SCAN_INTERVAL, 120)}
|
|
|
|
)
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = hive
|
|
|
|
|
|
|
|
try:
|
|
|
|
devices = await hive.session.startSession(hive_config)
|
|
|
|
except HTTPException as error:
|
|
|
|
_LOGGER.error("Could not connect to the internet: %s", error)
|
2024-03-17 23:40:38 +00:00
|
|
|
raise ConfigEntryNotReady from error
|
2021-04-10 05:41:29 +00:00
|
|
|
except HiveReauthRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(
|
|
|
|
entry,
|
|
|
|
[
|
|
|
|
ha_type
|
|
|
|
for ha_type, hive_type in PLATFORM_LOOKUP.items()
|
|
|
|
if devices.get(hive_type)
|
|
|
|
],
|
|
|
|
)
|
2019-09-27 21:18:34 +00:00
|
|
|
|
2018-01-15 22:24:12 +00:00
|
|
|
return True
|
2019-09-27 21:18:34 +00:00
|
|
|
|
|
|
|
|
2022-01-02 15:31:48 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2021-03-15 11:27:10 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 14:09:59 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-03-15 11:27:10 +00:00
|
|
|
if unload_ok:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2022-06-30 21:47:02 +00:00
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
|
|
"""Remove a config entry."""
|
|
|
|
hive = Auth(entry.data["username"], entry.data["password"])
|
|
|
|
await hive.forget_device(
|
|
|
|
entry.data["tokens"]["AuthenticationResult"]["AccessToken"],
|
|
|
|
entry.data["device_data"][1],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-05 19:43:33 +00:00
|
|
|
async def async_remove_config_entry_device(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Remove a config entry from a device."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-05-17 14:27:02 +00:00
|
|
|
def refresh_system[_HiveEntityT: HiveEntity, **_P](
|
2023-12-20 22:55:09 +00:00
|
|
|
func: Callable[Concatenate[_HiveEntityT, _P], Awaitable[Any]],
|
2022-04-28 18:52:08 +00:00
|
|
|
) -> Callable[Concatenate[_HiveEntityT, _P], Coroutine[Any, Any, None]]:
|
2019-09-27 21:18:34 +00:00
|
|
|
"""Force update all entities after state change."""
|
|
|
|
|
|
|
|
@wraps(func)
|
2022-04-28 18:52:08 +00:00
|
|
|
async def wrapper(self: _HiveEntityT, *args: _P.args, **kwargs: _P.kwargs) -> None:
|
2021-02-09 21:03:49 +00:00
|
|
|
await func(self, *args, **kwargs)
|
|
|
|
async_dispatcher_send(self.hass, DOMAIN)
|
2019-09-27 21:18:34 +00:00
|
|
|
|
|
|
|
return wrapper
|