Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Hong Kong Observatory integration."""
2 
3 from __future__ import annotations
4 
5 from hko import LOCATIONS
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_LOCATION, Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers.aiohttp_client import async_get_clientsession
11 
12 from .const import DEFAULT_DISTRICT, DOMAIN, KEY_DISTRICT, KEY_LOCATION
13 from .coordinator import HKOUpdateCoordinator
14 
15 PLATFORMS: list[Platform] = [Platform.WEATHER]
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
19  """Set up Hong Kong Observatory from a config entry."""
20 
21  location = entry.data[CONF_LOCATION]
22  district = next(
23  (item for item in LOCATIONS if item[KEY_LOCATION] == location),
24  {KEY_DISTRICT: DEFAULT_DISTRICT},
25  )[KEY_DISTRICT]
26  websession = async_get_clientsession(hass)
27 
28  coordinator = HKOUpdateCoordinator(hass, websession, district, location)
29  await coordinator.async_config_entry_first_refresh()
30  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
31 
32  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
33 
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
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:37
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:18
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)