Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The UptimeRobot integration."""
2 
3 from __future__ import annotations
4 
5 from pyuptimerobot import UptimeRobot
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_API_KEY
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryAuthFailed
11 from homeassistant.helpers import device_registry as dr
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 
14 from .const import DOMAIN, PLATFORMS
15 from .coordinator import UptimeRobotDataUpdateCoordinator
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
19  """Set up UptimeRobot from a config entry."""
20  hass.data.setdefault(DOMAIN, {})
21  key: str = entry.data[CONF_API_KEY]
22  if key.startswith(("ur", "m")):
24  "Wrong API key type detected, use the 'main' API key"
25  )
26  uptime_robot_api = UptimeRobot(key, async_get_clientsession(hass))
27  dev_reg = dr.async_get(hass)
28 
29  hass.data[DOMAIN][entry.entry_id] = coordinator = UptimeRobotDataUpdateCoordinator(
30  hass,
31  config_entry_id=entry.entry_id,
32  dev_reg=dev_reg,
33  api=uptime_robot_api,
34  )
35 
36  await coordinator.async_config_entry_first_refresh()
37 
38  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
39 
40  return True
41 
42 
43 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
44  """Unload a config entry."""
45  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
46  if unload_ok:
47  hass.data[DOMAIN].pop(entry.entry_id)
48 
49  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:43
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)