2019-09-27 21:18:34 +00:00
|
|
|
"""Support for the Hive devices and services."""
|
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
|
2023-01-23 06:28:43 +00:00
|
|
|
from typing import Any, Concatenate, ParamSpec, TypeVar
|
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
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from homeassistant import config_entries
|
2022-01-02 15:31:48 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-03-15 11:27:10 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
|
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
|
2021-03-15 11:27:10 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntry, DeviceInfo
|
2021-02-09 21:03:49 +00:00
|
|
|
from homeassistant.helpers.dispatcher import (
|
|
|
|
async_dispatcher_connect,
|
|
|
|
async_dispatcher_send,
|
|
|
|
)
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2022-01-02 15:31:48 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from .const import DOMAIN, PLATFORM_LOOKUP, PLATFORMS
|
2019-02-14 15:01:46 +00:00
|
|
|
|
2022-04-28 18:52:08 +00:00
|
|
|
_HiveEntityT = TypeVar("_HiveEntityT", bound="HiveEntity")
|
|
|
|
_P = ParamSpec("_P")
|
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2021-05-09 15:43:56 +00:00
|
|
|
vol.All(
|
|
|
|
cv.deprecated(DOMAIN),
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_SCAN_INTERVAL, default=2): cv.positive_int,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2019-09-27 21:18:34 +00:00
|
|
|
|
2022-01-02 15:31:48 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2021-03-15 11:27:10 +00:00
|
|
|
"""Hive configuration setup."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
|
|
|
|
if not hass.config_entries.async_entries(DOMAIN):
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={
|
|
|
|
CONF_USERNAME: conf[CONF_USERNAME],
|
|
|
|
CONF_PASSWORD: conf[CONF_PASSWORD],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
2019-09-27 21:18:34 +00:00
|
|
|
|
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."""
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-04-28 18:52:08 +00:00
|
|
|
def refresh_system(
|
|
|
|
func: Callable[Concatenate[_HiveEntityT, _P], Awaitable[Any]]
|
|
|
|
) -> 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
|
|
|
|
|
|
|
|
|
|
|
|
class HiveEntity(Entity):
|
|
|
|
"""Initiate Hive Base Class."""
|
|
|
|
|
2022-08-03 19:26:34 +00:00
|
|
|
def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None:
|
2019-09-27 21:18:34 +00:00
|
|
|
"""Initialize the instance."""
|
2021-02-09 21:03:49 +00:00
|
|
|
self.hive = hive
|
|
|
|
self.device = hive_device
|
2022-05-29 10:08:50 +00:00
|
|
|
self._attr_name = self.device["haName"]
|
|
|
|
self._attr_unique_id = f'{self.device["hiveID"]}-{self.device["hiveType"]}'
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self.device["device_id"])},
|
|
|
|
model=self.device["deviceData"]["model"],
|
|
|
|
manufacturer=self.device["deviceData"]["manufacturer"],
|
|
|
|
name=self.device["device_name"],
|
|
|
|
sw_version=self.device["deviceData"]["version"],
|
|
|
|
via_device=(DOMAIN, self.device["parentDevice"]),
|
|
|
|
)
|
2022-08-03 19:26:34 +00:00
|
|
|
self.attributes: dict[str, Any] = {}
|
2019-09-27 21:18:34 +00:00
|
|
|
|
2022-08-03 19:26:34 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2019-09-27 21:18:34 +00:00
|
|
|
"""When entity is added to Home Assistant."""
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state)
|
2020-04-01 21:19:51 +00:00
|
|
|
)
|