Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The MyPermobil integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from mypermobil import MyPermobil, MyPermobilClientException
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import (
11  CONF_CODE,
12  CONF_EMAIL,
13  CONF_REGION,
14  CONF_TOKEN,
15  CONF_TTL,
16  Platform,
17 )
18 from homeassistant.core import HomeAssistant
19 from homeassistant.exceptions import ConfigEntryAuthFailed
20 from homeassistant.helpers.aiohttp_client import async_get_clientsession
21 
22 from .const import APPLICATION, DOMAIN
23 from .coordinator import MyPermobilCoordinator
24 
25 PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 
30 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
31  """Set up MyPermobil from a config entry."""
32 
33  # create the API object from the config and save it in hass
34  session = async_get_clientsession(hass)
35  p_api = MyPermobil(
36  application=APPLICATION,
37  session=session,
38  email=entry.data[CONF_EMAIL],
39  region=entry.data[CONF_REGION],
40  code=entry.data[CONF_CODE],
41  token=entry.data[CONF_TOKEN],
42  expiration_date=entry.data[CONF_TTL],
43  )
44  try:
45  p_api.self_authenticate()
46  except MyPermobilClientException as err:
47  _LOGGER.error("Error authenticating %s", err)
48  raise ConfigEntryAuthFailed(f"Config error for {p_api.email}") from err
49 
50  # create the coordinator with the API object
51  coordinator = MyPermobilCoordinator(hass, p_api)
52  await coordinator.async_config_entry_first_refresh()
53 
54  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56 
57  return True
58 
59 
60 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
61  """Unload a config entry."""
62  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
63  hass.data[DOMAIN].pop(entry.entry_id)
64 
65  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:60
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:30
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)