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
|
2022-04-28 18:52:08 +00:00
|
|
|
from typing import Any, TypeVar
|
2019-02-14 15:01:46 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from aiohttp.web_exceptions import HTTPException
|
|
|
|
from apyhiveapi import Hive
|
|
|
|
from apyhiveapi.helper.hive_exceptions import HiveReauthRequired
|
2022-04-28 18:52:08 +00:00
|
|
|
from typing_extensions import Concatenate, ParamSpec
|
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
|
2021-02-09 21:03:49 +00:00
|
|
|
from homeassistant.helpers.dispatcher import (
|
|
|
|
async_dispatcher_connect,
|
|
|
|
async_dispatcher_send,
|
|
|
|
)
|
2019-09-27 21:18:34 +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."""
|
|
|
|
|
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
hive = Hive(websession)
|
|
|
|
hive_config = dict(entry.data)
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
for ha_type, hive_type in PLATFORM_LOOKUP.items():
|
|
|
|
device_list = devices.get(hive_type)
|
|
|
|
if device_list:
|
2021-02-09 21:03:49 +00:00
|
|
|
hass.async_create_task(
|
2021-03-15 11:27:10 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(entry, ha_type)
|
2021-02-09 21:03:49 +00:00
|
|
|
)
|
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-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."""
|
|
|
|
|
2021-02-09 21:03:49 +00:00
|
|
|
def __init__(self, hive, hive_device):
|
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
|
2019-09-27 21:18:34 +00:00
|
|
|
self.attributes = {}
|
2021-02-09 21:03:49 +00:00
|
|
|
self._unique_id = f'{self.device["hiveID"]}-{self.device["hiveType"]}'
|
2019-09-27 21:18:34 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""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
|
|
|
)
|