1 """Config flow for NASweb integration."""
3 from __future__
import annotations
8 import voluptuous
as vol
9 from webio_api
import WebioAPI
10 from webio_api.api_client
import AuthError
12 from homeassistant
import config_entries
20 from .const
import DOMAIN
21 from .coordinator
import NASwebCoordinator
22 from .nasweb_data
import NASwebData
24 NASWEB_SCHEMA_IMG_URL = (
25 "https://home-assistant.io/images/integrations/nasweb/nasweb_scheme.png"
28 _LOGGER = logging.getLogger(__name__)
30 STEP_USER_DATA_SCHEMA = vol.Schema(
32 vol.Required(CONF_HOST): str,
33 vol.Required(CONF_USERNAME): str,
34 vol.Required(CONF_PASSWORD): str,
39 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
40 """Validate user-provided data."""
41 webio_api = WebioAPI(data[CONF_HOST], data[CONF_USERNAME], data[CONF_PASSWORD])
42 if not await webio_api.check_connection():
45 await webio_api.refresh_device_info()
46 except AuthError
as e:
47 raise InvalidAuth
from e
50 nasweb_data.initialize(hass)
52 webio_serial = webio_api.get_serial_number()
53 if webio_serial
is None:
57 webhook_url = nasweb_data.get_webhook_url(hass)
58 nasweb_data.notify_coordinator.add_coordinator(webio_serial, coordinator)
59 subscription = await webio_api.status_subscription(webhook_url,
True)
61 nasweb_data.notify_coordinator.remove_coordinator(webio_serial)
63 "Failed to subscribe for status updates from device"
66 result = await nasweb_data.notify_coordinator.check_connection(webio_serial)
67 nasweb_data.notify_coordinator.remove_coordinator(webio_serial)
70 await webio_api.status_subscription(webhook_url,
False)
73 name = webio_api.get_name()
75 nasweb_data.deinitialize(hass)
76 return {
"title": name, CONF_UNIQUE_ID: webio_serial}
80 """Handle a config flow for NASweb."""
85 self, user_input: dict[str, Any] |
None =
None
86 ) -> ConfigFlowResult:
87 """Handle the initial step."""
88 errors: dict[str, str] = {}
89 if user_input
is not None:
95 errors[
"base"] =
"cannot_connect"
97 errors[
"base"] =
"invalid_auth"
98 except NoURLAvailableError:
99 errors[
"base"] =
"missing_internal_url"
100 except MissingNASwebData:
101 errors[
"base"] =
"missing_nasweb_data"
102 except MissingNASwebStatus:
103 errors[
"base"] =
"missing_status"
107 _LOGGER.exception(
"Unexpected exception")
108 errors[
"base"] =
"unknown"
115 STEP_USER_DATA_SCHEMA, user_input
118 description_placeholders={
119 "nasweb_schema_img":
'<img src="' + NASWEB_SCHEMA_IMG_URL +
'"/><br>',
125 """Error to indicate we cannot connect."""
129 """Error to indicate there is invalid auth."""
133 """Error to indicate missing information from NASweb."""
137 """Error to indicate there was no status received from NASweb."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)