Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Efergy integration."""
2 
3 from __future__ import annotations
4 
5 from pyefergy import Efergy, exceptions
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_API_KEY, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
11 from homeassistant.helpers.aiohttp_client import async_get_clientsession
12 
13 PLATFORMS = [Platform.SENSOR]
14 type EfergyConfigEntry = ConfigEntry[Efergy]
15 
16 
17 async def async_setup_entry(hass: HomeAssistant, entry: EfergyConfigEntry) -> bool:
18  """Set up Efergy from a config entry."""
19  api = Efergy(
20  entry.data[CONF_API_KEY],
21  session=async_get_clientsession(hass),
22  utc_offset=hass.config.time_zone,
23  currency=hass.config.currency,
24  )
25 
26  try:
27  await api.async_status(get_sids=True)
28  except (exceptions.ConnectError, exceptions.DataError) as ex:
29  raise ConfigEntryNotReady(f"Failed to connect to device: {ex}") from ex
30  except exceptions.InvalidAuth as ex:
32  "API Key is no longer valid. Please reauthenticate"
33  ) from ex
34 
35  entry.runtime_data = api
36 
37  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
38 
39  return True
40 
41 
42 async def async_unload_entry(hass: HomeAssistant, entry: EfergyConfigEntry) -> bool:
43  """Unload a config entry."""
44  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_setup_entry(HomeAssistant hass, EfergyConfigEntry entry)
Definition: __init__.py:17
bool async_unload_entry(HomeAssistant hass, EfergyConfigEntry entry)
Definition: __init__.py:42
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)