Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Integration for Apple's WeatherKit API."""
2 
3 from __future__ import annotations
4 
5 from apple_weatherkit.client import (
6  WeatherKitApiClient,
7  WeatherKitApiClientAuthenticationError,
8  WeatherKitApiClientError,
9 )
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryNotReady
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import (
18  CONF_KEY_ID,
19  CONF_KEY_PEM,
20  CONF_SERVICE_ID,
21  CONF_TEAM_ID,
22  DOMAIN,
23  LOGGER,
24 )
25 from .coordinator import WeatherKitDataUpdateCoordinator
26 
27 PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.WEATHER]
28 
29 
30 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
31  """Set up this integration using UI."""
32  hass.data.setdefault(DOMAIN, {})
33  coordinator = WeatherKitDataUpdateCoordinator(
34  hass=hass,
35  client=WeatherKitApiClient(
36  key_id=entry.data[CONF_KEY_ID],
37  service_id=entry.data[CONF_SERVICE_ID],
38  team_id=entry.data[CONF_TEAM_ID],
39  key_pem=entry.data[CONF_KEY_PEM],
40  session=async_get_clientsession(hass),
41  ),
42  )
43 
44  try:
45  await coordinator.update_supported_data_sets()
46  except WeatherKitApiClientAuthenticationError as ex:
47  LOGGER.error("Authentication error initializing integration: %s", ex)
48  return False
49  except WeatherKitApiClientError as ex:
50  raise ConfigEntryNotReady from ex
51 
52  await coordinator.async_config_entry_first_refresh()
53  hass.data[DOMAIN][entry.entry_id] = coordinator
54 
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56  return True
57 
58 
59 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
60  """Handle removal of an entry."""
61  if unloaded := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
62  hass.data[DOMAIN].pop(entry.entry_id)
63  return unloaded
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:59
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)