* Add NASweb integration * Fix DeviceInfo import * Remove commented out code * Change class name for uniquness * Drop CoordinatorEntity inheritance * Rename class Output to more descriptive: RelaySwitch * Update required webio-api version * Implement on-the-fly addition/removal of entities * Set coordinator name matching device name * Set entities with too old status as unavailable * Drop Optional in favor of modern typing * Fix spelling of a variable * Rename commons to more fitting name: helper * Remove redundant code * Let unload fail when there is no coordinator * Fix bad docstring * Rename cord to coordinator for clarity * Remove default value for pop and let it raise exception * Drop workaround and use get_url from helper.network * Use webhook to send data from device * Deinitialize coordinator when no longer needed * Use Python formattable string * Use dataclass to store integration data in hass.data * Raise ConfigEntryNotReady when appropriate * Refactor NASwebData class * Move RelaySwitch to switch.py * Fix ConfigFlow tests * Create issues when entry fails to load * Respond when correctly received status update * Depend on webhook instead of http * Create issue when status is not received during entry set up * Make issue_id unique across integration entries * Remove unnecessary initializations * Inherit CoordinatorEntity to avoid code duplication * Optimize property access via assignment in __init__ * Use preexisting mechanism to fill schema with user input * Fix translation strings * Handle unavailable or unreachable internal url * Implement custom coordinator for push driven data updates * Move module-specific constants to respective modules * Fix requirements_all.txt * Fix CODEOWNERS file * Raise ConfigEntryError instead of issue creation * Fix entity registry import * Use HassKey as key in hass.data * Use typed ConfigEntry * Store runtime data in config entry * Rewrite to be more Pythonic * Move add/remove of switch entities to switch.py * Skip unnecessary check * Remove unnecessary type hints * Remove unnecessary nonlocal * Use a more descriptive docstring * Add docstrings to NASwebCoordinator * Fix formatting * Use correct return type * Fix tests to align with changed code * Remove commented code * Use serial number as config entry id * Catch AbortFlow exception * Update tests to check ConfigEntry Unique ID * Remove unnecessary form abort
125 lines
5 KiB
Python
125 lines
5 KiB
Python
"""The NASweb integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from webio_api import WebioAPI
|
|
from webio_api.api_client import AuthError
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
|
|
from homeassistant.helpers import device_registry as dr
|
|
from homeassistant.helpers.network import NoURLAvailableError
|
|
from homeassistant.util.hass_dict import HassKey
|
|
|
|
from .const import DOMAIN, MANUFACTURER, SUPPORT_EMAIL
|
|
from .coordinator import NASwebCoordinator
|
|
from .nasweb_data import NASwebData
|
|
|
|
PLATFORMS: list[Platform] = [Platform.SWITCH]
|
|
|
|
NASWEB_CONFIG_URL = "https://{host}/page"
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
type NASwebConfigEntry = ConfigEntry[NASwebCoordinator]
|
|
DATA_NASWEB: HassKey[NASwebData] = HassKey(DOMAIN)
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: NASwebConfigEntry) -> bool:
|
|
"""Set up NASweb from a config entry."""
|
|
|
|
if DATA_NASWEB not in hass.data:
|
|
data = NASwebData()
|
|
data.initialize(hass)
|
|
hass.data[DATA_NASWEB] = data
|
|
nasweb_data = hass.data[DATA_NASWEB]
|
|
|
|
webio_api = WebioAPI(
|
|
entry.data[CONF_HOST], entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
|
|
)
|
|
try:
|
|
if not await webio_api.check_connection():
|
|
raise ConfigEntryNotReady(
|
|
f"[{entry.data[CONF_HOST]}] Check connection failed"
|
|
)
|
|
if not await webio_api.refresh_device_info():
|
|
_LOGGER.error("[%s] Refresh device info failed", entry.data[CONF_HOST])
|
|
raise ConfigEntryError(
|
|
translation_key="config_entry_error_internal_error",
|
|
translation_placeholders={"support_email": SUPPORT_EMAIL},
|
|
)
|
|
webio_serial = webio_api.get_serial_number()
|
|
if webio_serial is None:
|
|
_LOGGER.error("[%s] Serial number not available", entry.data[CONF_HOST])
|
|
raise ConfigEntryError(
|
|
translation_key="config_entry_error_internal_error",
|
|
translation_placeholders={"support_email": SUPPORT_EMAIL},
|
|
)
|
|
if entry.unique_id != webio_serial:
|
|
_LOGGER.error(
|
|
"[%s] Serial number doesn't match config entry", entry.data[CONF_HOST]
|
|
)
|
|
raise ConfigEntryError(translation_key="config_entry_error_serial_mismatch")
|
|
|
|
coordinator = NASwebCoordinator(
|
|
hass, webio_api, name=f"NASweb[{webio_api.get_name()}]"
|
|
)
|
|
entry.runtime_data = coordinator
|
|
nasweb_data.notify_coordinator.add_coordinator(webio_serial, entry.runtime_data)
|
|
|
|
webhook_url = nasweb_data.get_webhook_url(hass)
|
|
if not await webio_api.status_subscription(webhook_url, True):
|
|
_LOGGER.error("Failed to subscribe for status updates from webio")
|
|
raise ConfigEntryError(
|
|
translation_key="config_entry_error_internal_error",
|
|
translation_placeholders={"support_email": SUPPORT_EMAIL},
|
|
)
|
|
if not await nasweb_data.notify_coordinator.check_connection(webio_serial):
|
|
_LOGGER.error("Did not receive status from device")
|
|
raise ConfigEntryError(
|
|
translation_key="config_entry_error_no_status_update",
|
|
translation_placeholders={"support_email": SUPPORT_EMAIL},
|
|
)
|
|
except TimeoutError as error:
|
|
raise ConfigEntryNotReady(
|
|
f"[{entry.data[CONF_HOST]}] Check connection reached timeout"
|
|
) from error
|
|
except AuthError as error:
|
|
raise ConfigEntryError(
|
|
translation_key="config_entry_error_invalid_authentication"
|
|
) from error
|
|
except NoURLAvailableError as error:
|
|
raise ConfigEntryError(
|
|
translation_key="config_entry_error_missing_internal_url"
|
|
) from error
|
|
|
|
device_registry = dr.async_get(hass)
|
|
device_registry.async_get_or_create(
|
|
config_entry_id=entry.entry_id,
|
|
identifiers={(DOMAIN, webio_serial)},
|
|
manufacturer=MANUFACTURER,
|
|
name=webio_api.get_name(),
|
|
configuration_url=NASWEB_CONFIG_URL.format(host=entry.data[CONF_HOST]),
|
|
)
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: NASwebConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
nasweb_data = hass.data[DATA_NASWEB]
|
|
coordinator = entry.runtime_data
|
|
serial = entry.unique_id
|
|
if serial is not None:
|
|
nasweb_data.notify_coordinator.remove_coordinator(serial)
|
|
if nasweb_data.can_be_deinitialized():
|
|
nasweb_data.deinitialize(hass)
|
|
hass.data.pop(DATA_NASWEB)
|
|
webhook_url = nasweb_data.get_webhook_url(hass)
|
|
await coordinator.webio_api.status_subscription(webhook_url, False)
|
|
|
|
return unload_ok
|