Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """SFR Box config flow."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import TYPE_CHECKING, Any
7 
8 from sfrbox_api.bridge import SFRBox
9 from sfrbox_api.exceptions import SFRBoxAuthenticationError, SFRBoxError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
14 from homeassistant.helpers import selector
15 from homeassistant.helpers.httpx_client import get_async_client
16 
17 from .const import DEFAULT_HOST, DEFAULT_USERNAME, DOMAIN
18 
19 DATA_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_HOST, default=DEFAULT_HOST): selector.TextSelector(),
22  }
23 )
24 AUTH_SCHEMA = vol.Schema(
25  {
26  vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): selector.TextSelector(),
27  vol.Required(CONF_PASSWORD): selector.TextSelector(
28  selector.TextSelectorConfig(type=selector.TextSelectorType.PASSWORD)
29  ),
30  }
31 )
32 
33 
34 class SFRBoxFlowHandler(ConfigFlow, domain=DOMAIN):
35  """SFR Box config flow."""
36 
37  VERSION = 1
38  _box: SFRBox
39  _config: dict[str, Any] = {}
40 
41  async def async_step_user(
42  self, user_input: dict[str, str] | None = None
43  ) -> ConfigFlowResult:
44  """Handle a flow initialized by the user."""
45  errors = {}
46  if user_input is not None:
47  box = SFRBox(ip=user_input[CONF_HOST], client=get_async_client(self.hass))
48  try:
49  system_info = await box.system_get_info()
50  except SFRBoxError:
51  errors["base"] = "cannot_connect"
52  else:
53  if TYPE_CHECKING:
54  assert system_info is not None
55  await self.async_set_unique_idasync_set_unique_id(system_info.mac_addr)
56  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
57  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
58  self._box_box = box
59  self._config.update(user_input)
60  return await self.async_step_choose_authasync_step_choose_auth()
61 
62  data_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(DATA_SCHEMA, user_input)
63  return self.async_show_formasync_show_formasync_show_form(
64  step_id="user", data_schema=data_schema, errors=errors
65  )
66 
68  self, user_input: dict[str, str] | None = None
69  ) -> ConfigFlowResult:
70  """Handle a flow initialized by the user."""
71  return self.async_show_menuasync_show_menu(
72  step_id="choose_auth",
73  menu_options=["auth", "skip_auth"],
74  )
75 
76  async def async_step_auth(
77  self, user_input: dict[str, str] | None = None
78  ) -> ConfigFlowResult:
79  """Check authentication."""
80  errors = {}
81  if user_input is not None:
82  try:
83  await self._box_box.authenticate(
84  username=user_input[CONF_USERNAME],
85  password=user_input[CONF_PASSWORD],
86  )
87  except SFRBoxAuthenticationError:
88  errors["base"] = "invalid_auth"
89  else:
90  if self.sourcesourcesourcesource == SOURCE_REAUTH:
91  return self.async_update_reload_and_abortasync_update_reload_and_abort(
92  self._get_reauth_entry_get_reauth_entry(), data_updates=user_input
93  )
94  self._config.update(user_input)
95  return self.async_create_entryasync_create_entryasync_create_entry(title="SFR Box", data=self._config)
96 
97  suggested_values: Mapping[str, Any] | None = user_input
98  if self.sourcesourcesourcesource == SOURCE_REAUTH and not suggested_values:
99  suggested_values = self._get_reauth_entry_get_reauth_entry().data
100 
101  data_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(AUTH_SCHEMA, suggested_values)
102  return self.async_show_formasync_show_formasync_show_form(
103  step_id="auth", data_schema=data_schema, errors=errors
104  )
105 
107  self, user_input: dict[str, str] | None = None
108  ) -> ConfigFlowResult:
109  """Skip authentication."""
110  return self.async_create_entryasync_create_entryasync_create_entry(title="SFR Box", data=self._config)
111 
112  async def async_step_reauth(
113  self, entry_data: Mapping[str, Any]
114  ) -> ConfigFlowResult:
115  """Handle failed credentials."""
116  self._box_box = SFRBox(ip=entry_data[CONF_HOST], client=get_async_client(self.hass))
117  return await self.async_step_authasync_step_auth()
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:43
ConfigFlowResult async_step_choose_auth(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:69
ConfigFlowResult async_step_skip_auth(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:108
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:114
ConfigFlowResult async_step_auth(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:78
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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_show_menu(self, *str|None step_id=None, Container[str] menu_options, Mapping[str, str]|None description_placeholders=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)
str|None source(self)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
dict[str, str|bool] authenticate(HomeAssistant hass, str host, str security_code)
Definition: config_flow.py:132
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)
Definition: httpx_client.py:41