1 """Support for the Hive devices and services."""
3 from __future__
import annotations
5 from collections.abc
import Awaitable, Callable, Coroutine
6 from functools
import wraps
8 from typing
import Any, Concatenate
10 from aiohttp.web_exceptions
import HTTPException
11 from apyhiveapi
import Auth, Hive
12 from apyhiveapi.helper.hive_exceptions
import HiveReauthRequired
13 import voluptuous
as vol
15 from homeassistant
import config_entries
25 from .const
import DOMAIN, PLATFORM_LOOKUP, PLATFORMS
26 from .entity
import HiveEntity
28 _LOGGER = logging.getLogger(__name__)
30 CONFIG_SCHEMA = vol.Schema(
32 cv.deprecated(DOMAIN),
36 vol.Required(CONF_PASSWORD): cv.string,
37 vol.Required(CONF_USERNAME): cv.string,
38 vol.Optional(CONF_SCAN_INTERVAL, default=2): cv.positive_int,
43 extra=vol.ALLOW_EXTRA,
47 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
48 """Hive configuration setup."""
49 hass.data[DOMAIN] = {}
51 if DOMAIN
not in config:
56 if not hass.config_entries.async_entries(DOMAIN):
57 hass.async_create_task(
58 hass.config_entries.flow.async_init(
60 context={
"source": config_entries.SOURCE_IMPORT},
62 CONF_USERNAME: conf[CONF_USERNAME],
63 CONF_PASSWORD: conf[CONF_PASSWORD],
71 """Set up Hive from a config entry."""
73 web_session = aiohttp_client.async_get_clientsession(hass)
74 hive_config =
dict(entry.data)
75 hive = Hive(web_session)
77 hive_config[
"options"] = {}
78 hive_config[
"options"].
update(
79 {CONF_SCAN_INTERVAL:
dict(entry.options).
get(CONF_SCAN_INTERVAL, 120)}
81 hass.data[DOMAIN][entry.entry_id] = hive
84 devices = await hive.session.startSession(hive_config)
85 except HTTPException
as error:
86 _LOGGER.error(
"Could not connect to the internet: %s", error)
87 raise ConfigEntryNotReady
from error
88 except HiveReauthRequired
as err:
89 raise ConfigEntryAuthFailed
from err
91 await hass.config_entries.async_forward_entry_setups(
95 for ha_type, hive_type
in PLATFORM_LOOKUP.items()
96 if devices.get(hive_type)
104 """Unload a config entry."""
105 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
107 hass.data[DOMAIN].pop(entry.entry_id)
113 """Remove a config entry."""
114 hive = Auth(entry.data[
"username"], entry.data[
"password"])
115 await hive.forget_device(
116 entry.data[
"tokens"][
"AuthenticationResult"][
"AccessToken"],
117 entry.data[
"device_data"][1],
122 hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
124 """Remove a config entry from a device."""
128 def refresh_system[_HiveEntityT: HiveEntity, **_P](
129 func: Callable[Concatenate[_HiveEntityT, _P], Awaitable[Any]],
130 ) -> Callable[Concatenate[_HiveEntityT, _P], Coroutine[Any, Any,
None]]:
131 """Force update all entities after state change."""
134 async
def wrapper(self: _HiveEntityT, *args: _P.args, **kwargs: _P.kwargs) ->
None:
135 await func(self, *args, **kwargs)
web.Response get(self, web.Request request, str config_key)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None async_remove_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, DeviceEntry device_entry)
IssData update(pyiss.ISS iss)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)