Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The NASweb integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from webio_api import WebioAPI
8 from webio_api.api_client import AuthError
9 
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
14 from homeassistant.helpers import device_registry as dr
15 from homeassistant.helpers.network import NoURLAvailableError
16 from homeassistant.util.hass_dict import HassKey
17 
18 from .const import DOMAIN, MANUFACTURER, SUPPORT_EMAIL
19 from .coordinator import NASwebCoordinator
20 from .nasweb_data import NASwebData
21 
22 PLATFORMS: list[Platform] = [Platform.SWITCH]
23 
24 NASWEB_CONFIG_URL = "https://{host}/page"
25 
26 _LOGGER = logging.getLogger(__name__)
27 type NASwebConfigEntry = ConfigEntry[NASwebCoordinator]
28 DATA_NASWEB: HassKey[NASwebData] = HassKey(DOMAIN)
29 
30 
31 async def async_setup_entry(hass: HomeAssistant, entry: NASwebConfigEntry) -> bool:
32  """Set up NASweb from a config entry."""
33 
34  if DATA_NASWEB not in hass.data:
35  data = NASwebData()
36  data.initialize(hass)
37  hass.data[DATA_NASWEB] = data
38  nasweb_data = hass.data[DATA_NASWEB]
39 
40  webio_api = WebioAPI(
41  entry.data[CONF_HOST], entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
42  )
43  try:
44  if not await webio_api.check_connection():
45  raise ConfigEntryNotReady(
46  f"[{entry.data[CONF_HOST]}] Check connection failed"
47  )
48  if not await webio_api.refresh_device_info():
49  _LOGGER.error("[%s] Refresh device info failed", entry.data[CONF_HOST])
50  raise ConfigEntryError(
51  translation_key="config_entry_error_internal_error",
52  translation_placeholders={"support_email": SUPPORT_EMAIL},
53  )
54  webio_serial = webio_api.get_serial_number()
55  if webio_serial is None:
56  _LOGGER.error("[%s] Serial number not available", entry.data[CONF_HOST])
57  raise ConfigEntryError(
58  translation_key="config_entry_error_internal_error",
59  translation_placeholders={"support_email": SUPPORT_EMAIL},
60  )
61  if entry.unique_id != webio_serial:
62  _LOGGER.error(
63  "[%s] Serial number doesn't match config entry", entry.data[CONF_HOST]
64  )
65  raise ConfigEntryError(translation_key="config_entry_error_serial_mismatch")
66 
67  coordinator = NASwebCoordinator(
68  hass, webio_api, name=f"NASweb[{webio_api.get_name()}]"
69  )
70  entry.runtime_data = coordinator
71  nasweb_data.notify_coordinator.add_coordinator(webio_serial, entry.runtime_data)
72 
73  webhook_url = nasweb_data.get_webhook_url(hass)
74  if not await webio_api.status_subscription(webhook_url, True):
75  _LOGGER.error("Failed to subscribe for status updates from webio")
76  raise ConfigEntryError(
77  translation_key="config_entry_error_internal_error",
78  translation_placeholders={"support_email": SUPPORT_EMAIL},
79  )
80  if not await nasweb_data.notify_coordinator.check_connection(webio_serial):
81  _LOGGER.error("Did not receive status from device")
82  raise ConfigEntryError(
83  translation_key="config_entry_error_no_status_update",
84  translation_placeholders={"support_email": SUPPORT_EMAIL},
85  )
86  except TimeoutError as error:
87  raise ConfigEntryNotReady(
88  f"[{entry.data[CONF_HOST]}] Check connection reached timeout"
89  ) from error
90  except AuthError as error:
91  raise ConfigEntryError(
92  translation_key="config_entry_error_invalid_authentication"
93  ) from error
94  except NoURLAvailableError as error:
95  raise ConfigEntryError(
96  translation_key="config_entry_error_missing_internal_url"
97  ) from error
98 
99  device_registry = dr.async_get(hass)
100  device_registry.async_get_or_create(
101  config_entry_id=entry.entry_id,
102  identifiers={(DOMAIN, webio_serial)},
103  manufacturer=MANUFACTURER,
104  name=webio_api.get_name(),
105  configuration_url=NASWEB_CONFIG_URL.format(host=entry.data[CONF_HOST]),
106  )
107  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
108  return True
109 
110 
111 async def async_unload_entry(hass: HomeAssistant, entry: NASwebConfigEntry) -> bool:
112  """Unload a config entry."""
113  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
114  nasweb_data = hass.data[DATA_NASWEB]
115  coordinator = entry.runtime_data
116  serial = entry.unique_id
117  if serial is not None:
118  nasweb_data.notify_coordinator.remove_coordinator(serial)
119  if nasweb_data.can_be_deinitialized():
120  nasweb_data.deinitialize(hass)
121  hass.data.pop(DATA_NASWEB)
122  webhook_url = nasweb_data.get_webhook_url(hass)
123  await coordinator.webio_api.status_subscription(webhook_url, False)
124 
125  return unload_ok
bool async_unload_entry(HomeAssistant hass, NASwebConfigEntry entry)
Definition: __init__.py:111
bool async_setup_entry(HomeAssistant hass, NASwebConfigEntry entry)
Definition: __init__.py:31