Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The opensky component."""
2 
3 from __future__ import annotations
4 
5 from aiohttp import BasicAuth
6 from python_opensky import OpenSky
7 from python_opensky.exceptions import OpenSkyError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
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_CONTRIBUTING_USER, DOMAIN, PLATFORMS
16 from .coordinator import OpenSkyDataUpdateCoordinator
17 
18 
19 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
20  """Set up opensky from a config entry."""
21 
22  client = OpenSky(session=async_get_clientsession(hass))
23  if CONF_USERNAME in entry.options and CONF_PASSWORD in entry.options:
24  try:
25  await client.authenticate(
26  BasicAuth(
27  login=entry.options[CONF_USERNAME],
28  password=entry.options[CONF_PASSWORD],
29  ),
30  contributing_user=entry.options.get(CONF_CONTRIBUTING_USER, False),
31  )
32  except OpenSkyError as exc:
33  raise ConfigEntryNotReady from exc
34 
35  coordinator = OpenSkyDataUpdateCoordinator(hass, client)
36  await coordinator.async_config_entry_first_refresh()
37  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
38 
39  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
40  entry.async_on_unload(entry.add_update_listener(update_listener))
41 
42  return True
43 
44 
45 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
46  """Unload opensky config entry."""
47 
48  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
49 
50 
51 async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
52  """Handle options update."""
53  await hass.config_entries.async_reload(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:45
None update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:51
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:19
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)