Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Ecoforest integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import httpx
8 from pyecoforest.api import EcoforestApi
9 from pyecoforest.exceptions import (
10  EcoforestAuthenticationRequired,
11  EcoforestConnectionError,
12 )
13 
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import ConfigEntryNotReady
18 
19 from .const import DOMAIN
20 from .coordinator import EcoforestCoordinator
21 
22 PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
28  """Set up Ecoforest from a config entry."""
29 
30  host = entry.data[CONF_HOST]
31  auth = httpx.BasicAuth(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD])
32  api = EcoforestApi(host, auth)
33 
34  try:
35  device = await api.get()
36  _LOGGER.debug("Ecoforest: %s", device)
37  except EcoforestAuthenticationRequired:
38  _LOGGER.error("Authentication on device %s failed", host)
39  return False
40  except EcoforestConnectionError as err:
41  _LOGGER.error("Error communicating with device %s", host)
42  raise ConfigEntryNotReady from err
43 
44  coordinator = EcoforestCoordinator(hass, api)
45 
46  await coordinator.async_config_entry_first_refresh()
47 
48  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = 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: ConfigEntry) -> bool:
56  """Unload a config entry."""
57  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
58  hass.data[DOMAIN].pop(entry.entry_id)
59 
60  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:55
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:27