Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Electra Air Conditioner integration."""
2 
3 from __future__ import annotations
4 
5 from typing import cast
6 
7 from electrasmart.api import ElectraAPI, ElectraApiError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_TOKEN, Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryNotReady
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import CONF_IMEI, DOMAIN
16 
17 PLATFORMS: list[Platform] = [Platform.CLIMATE]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
21  """Set up Electra Smart Air Conditioner from a config entry."""
22  hass.data.setdefault(DOMAIN, {})
23  entry.async_on_unload(entry.add_update_listener(update_listener))
24  hass.data[DOMAIN][entry.entry_id] = ElectraAPI(
25  async_get_clientsession(hass), entry.data[CONF_IMEI], entry.data[CONF_TOKEN]
26  )
27 
28  try:
29  await cast(ElectraAPI, hass.data[DOMAIN][entry.entry_id]).fetch_devices()
30  except ElectraApiError as exp:
31  raise ConfigEntryNotReady(f"Error communicating with API: {exp}") from exp
32 
33  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
34  return True
35 
36 
37 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
38  """Unload a config entry."""
39  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
40  hass.data[DOMAIN].pop(entry.entry_id)
41 
42  return unload_ok
43 
44 
45 async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
46  """Update listener."""
47  await hass.config_entries.async_reload(config_entry.entry_id)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:20
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:37
None update_listener(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:45
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)