Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Tomorrow.io integration."""
2 
3 from __future__ import annotations
4 
5 from pytomorrowio import TomorrowioV4
6 
7 from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
8 from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_API_KEY
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 
14 from .const import DOMAIN
15 from .coordinator import TomorrowioDataUpdateCoordinator
16 
17 PLATFORMS = [SENSOR_DOMAIN, WEATHER_DOMAIN]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
21  """Set up Tomorrow.io API from a config entry."""
22  hass.data.setdefault(DOMAIN, {})
23 
24  api_key = entry.data[CONF_API_KEY]
25  # If coordinator already exists for this API key, we'll use that, otherwise
26  # we have to create a new one
27  if not (coordinator := hass.data[DOMAIN].get(api_key)):
28  session = async_get_clientsession(hass)
29  # we will not use the class's lat and long so we can pass in garbage
30  # lats and longs
31  api = TomorrowioV4(api_key, 361.0, 361.0, unit_system="metric", session=session)
32  coordinator = TomorrowioDataUpdateCoordinator(hass, api)
33  hass.data[DOMAIN][api_key] = coordinator
34 
35  await coordinator.async_setup_entry(entry)
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, config_entry: ConfigEntry) -> bool:
43  """Unload a config entry."""
44  unload_ok = await hass.config_entries.async_unload_platforms(
45  config_entry, PLATFORMS
46  )
47 
48  api_key = config_entry.data[CONF_API_KEY]
49  coordinator: TomorrowioDataUpdateCoordinator = hass.data[DOMAIN][api_key]
50  # If this is true, we can remove the coordinator
51  if await coordinator.async_unload_entry(config_entry):
52  hass.data[DOMAIN].pop(api_key)
53  if not hass.data[DOMAIN]:
54  hass.data.pop(DOMAIN)
55 
56  return unload_ok
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:20
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_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)