1 """Config flow for Vilfo Router integration."""
6 from vilfo
import Client
as VilfoClient
7 from vilfo.exceptions
import (
8 AuthenticationException
as VilfoAuthenticationException,
11 import voluptuous
as vol
19 from .const
import DOMAIN, ROUTER_DEFAULT_HOST
21 _LOGGER = logging.getLogger(__name__)
23 DATA_SCHEMA = vol.Schema(
25 vol.Required(CONF_HOST, default=ROUTER_DEFAULT_HOST): str,
26 vol.Required(CONF_ACCESS_TOKEN, default=
""): str,
30 RESULT_SUCCESS =
"success"
31 RESULT_CANNOT_CONNECT =
"cannot_connect"
32 RESULT_INVALID_AUTH =
"invalid_auth"
36 """Attempt to connect and call the ping endpoint and, if successful, fetch basic information."""
39 controller = VilfoClient(host=host, token=token)
40 result = {
"type":
None,
"data": {}}
44 except VilfoException:
45 result[
"type"] = RESULT_CANNOT_CONNECT
46 result[
"data"] = CannotConnect
51 controller.get_board_information()
52 except VilfoAuthenticationException:
53 result[
"type"] = RESULT_INVALID_AUTH
54 result[
"data"] = InvalidAuth
58 result[
"data"][CONF_ID] = controller.mac
59 result[
"data"][CONF_MAC] = controller.mac
61 result[
"data"][CONF_ID] = host
62 result[
"data"][CONF_MAC] =
None
64 result[
"type"] = RESULT_SUCCESS
70 """Validate the user input allows us to connect.
72 Data has the keys from DATA_SCHEMA with values provided by the user.
81 result = await hass.async_add_executor_job(
82 _try_connect_and_fetch_basic_info, data[CONF_HOST], data[CONF_ACCESS_TOKEN]
85 if result[
"type"] != RESULT_SUCCESS:
89 result_data = result[
"data"]
90 config[
"title"] = f
"{data[CONF_HOST]}"
91 config[CONF_MAC] = result_data[CONF_MAC]
92 config[CONF_HOST] = data[CONF_HOST]
93 config[CONF_ID] = result_data[CONF_ID]
99 """Handle a config flow for Vilfo Router."""
104 self, user_input: dict[str, Any] |
None =
None
105 ) -> ConfigFlowResult:
106 """Handle the initial step."""
108 if user_input
is not None:
112 errors[
"base"] =
"invalid_host"
113 except CannotConnect:
114 errors[
"base"] =
"cannot_connect"
116 errors[
"base"] =
"invalid_auth"
117 except Exception
as err:
118 _LOGGER.error(
"Unexpected exception: %s", err)
119 errors[
"base"] =
"unknown"
127 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
132 """Error to indicate we cannot connect."""
135 class InvalidAuth(HomeAssistantError):
136 """Error to indicate there is invalid auth."""
140 """Error to indicate that hostname/IP address is invalid."""
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)
_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)
def validate_input(HomeAssistant hass, data)
def _try_connect_and_fetch_basic_info(host, token)
bool is_host_valid(str host)