Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The ista Ecotrend integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pyecotrend_ista import KeycloakError, LoginError, PyEcotrendIsta, ServerError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
13 
14 from .const import DOMAIN
15 from .coordinator import IstaCoordinator
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 PLATFORMS: list[Platform] = [Platform.SENSOR]
20 
21 type IstaConfigEntry = ConfigEntry[IstaCoordinator]
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: IstaConfigEntry) -> bool:
25  """Set up ista EcoTrend from a config entry."""
26  ista = PyEcotrendIsta(
27  entry.data[CONF_EMAIL],
28  entry.data[CONF_PASSWORD],
29  _LOGGER,
30  )
31  try:
32  await hass.async_add_executor_job(ista.login)
33  except ServerError as e:
34  raise ConfigEntryNotReady(
35  translation_domain=DOMAIN,
36  translation_key="connection_exception",
37  ) from e
38  except (LoginError, KeycloakError) as e:
40  translation_domain=DOMAIN,
41  translation_key="authentication_exception",
42  translation_placeholders={CONF_EMAIL: entry.data[CONF_EMAIL]},
43  ) from e
44 
45  coordinator = IstaCoordinator(hass, ista)
46  await coordinator.async_config_entry_first_refresh()
47 
48  entry.runtime_data = coordinator
49 
50  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
51 
52  return True
53 
54 
55 async def async_unload_entry(hass: HomeAssistant, entry: IstaConfigEntry) -> bool:
56  """Unload a config entry."""
57  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, IstaConfigEntry entry)
Definition: __init__.py:24
bool async_unload_entry(HomeAssistant hass, IstaConfigEntry entry)
Definition: __init__.py:55