Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Whois integration."""
2 
3 from __future__ import annotations
4 
5 from whois import Domain, query as whois_query
6 from whois.exceptions import (
7  FailedParsingWhoisOutput,
8  UnknownDateFormat,
9  UnknownTld,
10  WhoisCommandFailed,
11 )
12 
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import CONF_DOMAIN
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
17 
18 from .const import DOMAIN, LOGGER, PLATFORMS, SCAN_INTERVAL
19 
20 
21 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
22  """Set up from a config entry."""
23 
24  async def _async_query_domain() -> Domain | None:
25  """Query WHOIS for domain information."""
26  try:
27  return await hass.async_add_executor_job(
28  whois_query, entry.data[CONF_DOMAIN]
29  )
30  except UnknownTld as ex:
31  raise UpdateFailed("Could not set up whois, TLD is unknown") from ex
32  except (FailedParsingWhoisOutput, WhoisCommandFailed, UnknownDateFormat) as ex:
33  raise UpdateFailed("An error occurred during WHOIS lookup") from ex
34 
35  coordinator: DataUpdateCoordinator[Domain | None] = DataUpdateCoordinator(
36  hass,
37  LOGGER,
38  config_entry=entry,
39  name=f"{DOMAIN}_APK",
40  update_interval=SCAN_INTERVAL,
41  update_method=_async_query_domain,
42  )
43  await coordinator.async_config_entry_first_refresh()
44  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
45 
46  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
47 
48  return True
49 
50 
51 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
52  """Unload a config entry."""
53  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
54  if unload_ok:
55  del hass.data[DOMAIN][entry.entry_id]
56  return unload_ok
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:21
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:51