Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Freebox integration."""
2 
3 import logging
4 from typing import Any
5 
6 from freebox_api.exceptions import AuthorizationError, HttpRequestError
7 import voluptuous as vol
8 
9 from homeassistant.components import zeroconf
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST, CONF_PORT
12 
13 from .const import DOMAIN
14 from .router import get_api, get_hosts_list_if_supported
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
19 class FreeboxFlowHandler(ConfigFlow, domain=DOMAIN):
20  """Handle a config flow."""
21 
22  VERSION = 1
23 
24  def __init__(self) -> None:
25  """Initialize config flow."""
26  self._data_data: dict[str, Any] = {}
27 
28  async def async_step_user(
29  self, user_input: dict[str, Any] | None = None
30  ) -> ConfigFlowResult:
31  """Handle a flow initiated by the user."""
32  if user_input is None:
33  return self.async_show_formasync_show_formasync_show_form(
34  step_id="user",
35  data_schema=vol.Schema(
36  {
37  vol.Required(CONF_HOST): str,
38  vol.Required(CONF_PORT): int,
39  }
40  ),
41  errors={},
42  )
43 
44  self._data_data = user_input
45 
46  # Check if already configured
47  await self.async_set_unique_idasync_set_unique_id(self._data_data[CONF_HOST])
48  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
49 
50  return await self.async_step_linkasync_step_link()
51 
52  async def async_step_link(
53  self, user_input: dict[str, Any] | None = None
54  ) -> ConfigFlowResult:
55  """Attempt to link with the Freebox router.
56 
57  Given a configured host, will ask the user to press the button
58  to connect to the router.
59  """
60  if user_input is None:
61  return self.async_show_formasync_show_formasync_show_form(step_id="link")
62 
63  errors = {}
64 
65  fbx = await get_api(self.hass, self._data_data[CONF_HOST])
66  try:
67  # Open connection and check authentication
68  await fbx.open(self._data_data[CONF_HOST], self._data_data[CONF_PORT])
69 
70  # Check permissions
71  await fbx.system.get_config()
73 
74  # Close connection
75  await fbx.close()
76 
77  return self.async_create_entryasync_create_entryasync_create_entry(
78  title=self._data_data[CONF_HOST],
79  data=self._data_data,
80  )
81 
82  except AuthorizationError as error:
83  _LOGGER.error(error)
84  errors["base"] = "register_failed"
85 
86  except HttpRequestError:
87  _LOGGER.error(
88  "Error connecting to the Freebox router at %s", self._data_data[CONF_HOST]
89  )
90  errors["base"] = "cannot_connect"
91 
92  except Exception:
93  _LOGGER.exception(
94  "Unknown error connecting with Freebox router at %s",
95  self._data_data[CONF_HOST],
96  )
97  errors["base"] = "unknown"
98 
99  return self.async_show_formasync_show_formasync_show_form(step_id="link", errors=errors)
100 
102  self, discovery_info: zeroconf.ZeroconfServiceInfo
103  ) -> ConfigFlowResult:
104  """Initialize flow from zeroconf."""
105  zeroconf_properties = discovery_info.properties
106  host = zeroconf_properties["api_domain"]
107  port = zeroconf_properties["https_port"]
108  return await self.async_step_userasync_step_userasync_step_user({CONF_HOST: host, CONF_PORT: port})
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:30
ConfigFlowResult async_step_link(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:54
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:103
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_step_user(self, dict[str, Any]|None user_input=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)
Freepybox get_api(HomeAssistant hass, str host)
Definition: router.py:56
tuple[bool, list[dict[str, Any]]] get_hosts_list_if_supported(Freepybox fbx_api)
Definition: router.py:70