1 """Config flow for QNAP QSW."""
3 from __future__
import annotations
8 from aioqsw.exceptions
import LoginError, QswError
9 from aioqsw.localapi
import ConnectionOptions, QnapQswApi
10 import voluptuous
as vol
19 from .const
import DOMAIN
21 _LOGGER = logging.getLogger(__name__)
25 """Handle config flow for a QNAP QSW device."""
27 _discovered_mac: str |
None =
None
28 _discovered_url: str |
None =
None
31 self, user_input: dict[str, Any] |
None =
None
32 ) -> ConfigFlowResult:
33 """Handle the initial step."""
36 if user_input
is not None:
37 url = user_input[CONF_URL]
38 username = user_input[CONF_USERNAME]
39 password = user_input[CONF_PASSWORD]
42 aiohttp_client.async_get_clientsession(self.hass),
43 ConnectionOptions(url, username, password),
47 system_board = await qsw.validate()
49 errors[CONF_PASSWORD] =
"invalid_auth"
51 errors[CONF_URL] =
"cannot_connect"
53 mac = system_board.get_mac()
60 title = f
"QNAP {system_board.get_product()} {mac}"
65 data_schema=vol.Schema(
67 vol.Required(CONF_URL): str,
68 vol.Required(CONF_USERNAME): str,
69 vol.Required(CONF_PASSWORD): str,
76 self, discovery_info: dhcp.DhcpServiceInfo
77 ) -> ConfigFlowResult:
78 """Handle DHCP discovery."""
82 _LOGGER.debug(
"DHCP discovery detected QSW: %s", self.
_discovered_mac_discovered_mac)
91 options = ConnectionOptions(self.
_discovered_url_discovered_url,
"",
"")
92 qsw = QnapQswApi(aiohttp_client.async_get_clientsession(self.hass), options)
96 except QswError
as err:
97 raise AbortFlow(
"cannot_connect")
from err
99 return await self.async_step_discovered_connection()
101 async
def async_step_discovered_connection(
102 self, user_input: dict[str, Any] |
None =
None
103 ) -> ConfigFlowResult:
104 """Confirm discovery."""
108 if user_input
is not None:
109 username = user_input[CONF_USERNAME]
110 password = user_input[CONF_PASSWORD]
113 aiohttp_client.async_get_clientsession(self.hass),
114 ConnectionOptions(self.
_discovered_url_discovered_url, username, password),
118 system_board = await qsw.validate()
120 errors[CONF_PASSWORD] =
"invalid_auth"
122 errors[
"base"] =
"cannot_connect"
124 title = f
"QNAP {system_board.get_product()} {self._discovered_mac}"
129 step_id=
"discovered_connection",
130 data_schema=vol.Schema(
132 vol.Required(CONF_USERNAME): str,
133 vol.Required(CONF_PASSWORD): str,
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")
ConfigFlowResult async_step_dhcp(self, DhcpServiceInfo discovery_info)
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)
_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)