Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Weheat integration."""
2 
3 from __future__ import annotations
4 
5 from weheat.abstractions.discovery import HeatPumpDiscovery
6 from weheat.exceptions import UnauthorizedException
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_ACCESS_TOKEN, Platform
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import ConfigEntryAuthFailed
13  OAuth2Session,
14  async_get_config_entry_implementation,
15 )
16 
17 from .const import API_URL, LOGGER
18 from .coordinator import WeheatDataUpdateCoordinator
19 
20 PLATFORMS: list[Platform] = [Platform.SENSOR]
21 
22 type WeheatConfigEntry = ConfigEntry[list[WeheatDataUpdateCoordinator]]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: WeheatConfigEntry) -> bool:
26  """Set up Weheat from a config entry."""
27  implementation = await async_get_config_entry_implementation(hass, entry)
28 
29  session = OAuth2Session(hass, entry, implementation)
30 
31  token = session.token[CONF_ACCESS_TOKEN]
32  entry.runtime_data = []
33 
34  # fetch a list of the heat pumps the entry can access
35  try:
36  discovered_heat_pumps = await HeatPumpDiscovery.discover_active(API_URL, token)
37  except UnauthorizedException as error:
38  raise ConfigEntryAuthFailed from error
39 
40  for pump_info in discovered_heat_pumps:
41  LOGGER.debug("Adding %s", pump_info)
42  # for each pump, add a coordinator
43  new_coordinator = WeheatDataUpdateCoordinator(hass, session, pump_info)
44 
45  await new_coordinator.async_config_entry_first_refresh()
46 
47  entry.runtime_data.append(new_coordinator)
48 
49  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
50 
51  return True
52 
53 
54 async def async_unload_entry(hass: HomeAssistant, entry: WeheatConfigEntry) -> bool:
55  """Unload a config entry."""
56  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, WeheatConfigEntry entry)
Definition: __init__.py:54
bool async_setup_entry(HomeAssistant hass, WeheatConfigEntry entry)
Definition: __init__.py:25
AbstractOAuth2Implementation async_get_config_entry_implementation(HomeAssistant hass, config_entries.ConfigEntry config_entry)