1 """The NASweb integration."""
3 from __future__
import annotations
7 from webio_api
import WebioAPI
8 from webio_api.api_client
import AuthError
18 from .const
import DOMAIN, MANUFACTURER, SUPPORT_EMAIL
19 from .coordinator
import NASwebCoordinator
20 from .nasweb_data
import NASwebData
22 PLATFORMS: list[Platform] = [Platform.SWITCH]
24 NASWEB_CONFIG_URL =
"https://{host}/page"
26 _LOGGER = logging.getLogger(__name__)
27 type NASwebConfigEntry = ConfigEntry[NASwebCoordinator]
28 DATA_NASWEB: HassKey[NASwebData] =
HassKey(DOMAIN)
32 """Set up NASweb from a config entry."""
34 if DATA_NASWEB
not in hass.data:
37 hass.data[DATA_NASWEB] = data
38 nasweb_data = hass.data[DATA_NASWEB]
41 entry.data[CONF_HOST], entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
44 if not await webio_api.check_connection():
46 f
"[{entry.data[CONF_HOST]}] Check connection failed"
48 if not await webio_api.refresh_device_info():
49 _LOGGER.error(
"[%s] Refresh device info failed", entry.data[CONF_HOST])
51 translation_key=
"config_entry_error_internal_error",
52 translation_placeholders={
"support_email": SUPPORT_EMAIL},
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])
58 translation_key=
"config_entry_error_internal_error",
59 translation_placeholders={
"support_email": SUPPORT_EMAIL},
61 if entry.unique_id != webio_serial:
63 "[%s] Serial number doesn't match config entry", entry.data[CONF_HOST]
68 hass, webio_api, name=f
"NASweb[{webio_api.get_name()}]"
70 entry.runtime_data = coordinator
71 nasweb_data.notify_coordinator.add_coordinator(webio_serial, entry.runtime_data)
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")
77 translation_key=
"config_entry_error_internal_error",
78 translation_placeholders={
"support_email": SUPPORT_EMAIL},
80 if not await nasweb_data.notify_coordinator.check_connection(webio_serial):
81 _LOGGER.error(
"Did not receive status from device")
83 translation_key=
"config_entry_error_no_status_update",
84 translation_placeholders={
"support_email": SUPPORT_EMAIL},
86 except TimeoutError
as error:
88 f
"[{entry.data[CONF_HOST]}] Check connection reached timeout"
90 except AuthError
as error:
92 translation_key=
"config_entry_error_invalid_authentication"
94 except NoURLAvailableError
as error:
96 translation_key=
"config_entry_error_missing_internal_url"
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]),
107 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
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)
bool async_unload_entry(HomeAssistant hass, NASwebConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, NASwebConfigEntry entry)