Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The BleBox devices integration."""
2 
3 import logging
4 
5 from blebox_uniapi.box import Box
6 from blebox_uniapi.error import Error
7 from blebox_uniapi.session import ApiHost
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import (
11  CONF_HOST,
12  CONF_PASSWORD,
13  CONF_PORT,
14  CONF_USERNAME,
15  Platform,
16 )
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import ConfigEntryNotReady
19 
20 from .const import DEFAULT_SETUP_TIMEOUT
21 from .helpers import get_maybe_authenticated_session
22 
23 type BleBoxConfigEntry = ConfigEntry[Box]
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 PLATFORMS = [
28  Platform.BINARY_SENSOR,
29  Platform.BUTTON,
30  Platform.CLIMATE,
31  Platform.COVER,
32  Platform.LIGHT,
33  Platform.SENSOR,
34  Platform.SWITCH,
35 ]
36 
37 PARALLEL_UPDATES = 0
38 
39 
40 async def async_setup_entry(hass: HomeAssistant, entry: BleBoxConfigEntry) -> bool:
41  """Set up BleBox devices from a config entry."""
42  host = entry.data[CONF_HOST]
43  port = entry.data[CONF_PORT]
44 
45  username = entry.data.get(CONF_USERNAME)
46  password = entry.data.get(CONF_PASSWORD)
47 
48  timeout = DEFAULT_SETUP_TIMEOUT
49 
50  websession = get_maybe_authenticated_session(hass, password, username)
51 
52  api_host = ApiHost(host, port, timeout, websession, hass.loop)
53 
54  try:
55  product = await Box.async_from_host(api_host)
56  except Error as ex:
57  _LOGGER.error("Identify failed at %s:%d (%s)", api_host.host, api_host.port, ex)
58  raise ConfigEntryNotReady from ex
59 
60  entry.runtime_data = product
61 
62  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
63 
64  return True
65 
66 
67 async def async_unload_entry(hass: HomeAssistant, entry: BleBoxConfigEntry) -> bool:
68  """Unload a config entry."""
69  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
aiohttp.ClientSession get_maybe_authenticated_session(HomeAssistant hass, str|None password, str|None username)
Definition: helpers.py:16
bool async_unload_entry(HomeAssistant hass, BleBoxConfigEntry entry)
Definition: __init__.py:67
bool async_setup_entry(HomeAssistant hass, BleBoxConfigEntry entry)
Definition: __init__.py:40