Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Huum integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from huum.exceptions import Forbidden, NotAuthenticated
8 from huum.huum import Huum
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryNotReady
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import DOMAIN, PLATFORMS
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
22  """Set up Huum from a config entry."""
23  username = entry.data[CONF_USERNAME]
24  password = entry.data[CONF_PASSWORD]
25 
26  huum = Huum(username, password, session=async_get_clientsession(hass))
27 
28  try:
29  await huum.status()
30  except (Forbidden, NotAuthenticated) as err:
31  _LOGGER.error("Could not log in to Huum with given credentials")
32  raise ConfigEntryNotReady(
33  "Could not log in to Huum with given credentials"
34  ) from err
35 
36  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = huum
37 
38  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
39  return True
40 
41 
42 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
43  """Unload a config entry."""
44  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
45  hass.data[DOMAIN].pop(entry.entry_id)
46 
47  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:42
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:21
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)