Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for wiffi component.
2 
3 Used by UI to setup a wiffi integration.
4 """
5 
6 from __future__ import annotations
7 
8 import errno
9 from typing import Any
10 
11 import voluptuous as vol
12 from wiffi import WiffiTcpServer
13 
14 from homeassistant.config_entries import (
15  ConfigEntry,
16  ConfigFlow,
17  ConfigFlowResult,
18  OptionsFlow,
19 )
20 from homeassistant.const import CONF_PORT, CONF_TIMEOUT
21 from homeassistant.core import callback
22 
23 from .const import DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN
24 
25 
26 class WiffiFlowHandler(ConfigFlow, domain=DOMAIN):
27  """Wiffi server setup config flow."""
28 
29  VERSION = 1
30 
31  @staticmethod
32  @callback
34  config_entry: ConfigEntry,
35  ) -> OptionsFlowHandler:
36  """Create Wiffi server setup option flow."""
37  return OptionsFlowHandler()
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle the start of the config flow.
43 
44  Called after wiffi integration has been selected in the 'add integration
45  UI'. The user_input is set to None in this case. We will open a config
46  flow form then.
47  This function is also called if the form has been submitted. user_input
48  contains a dict with the user entered values then.
49  """
50  if user_input is None:
51  return self._async_show_form_async_show_form()
52 
53  # received input from form or configuration.yaml
54  self._async_abort_entries_match_async_abort_entries_match(user_input)
55 
56  try:
57  # try to start server to check whether port is in use
58  server = WiffiTcpServer(user_input[CONF_PORT])
59  await server.start_server()
60  await server.close_server()
61  return self.async_create_entryasync_create_entryasync_create_entry(
62  title=f"Port {user_input[CONF_PORT]}", data=user_input
63  )
64  except OSError as exc:
65  if exc.errno == errno.EADDRINUSE:
66  return self.async_abortasync_abortasync_abort(reason="addr_in_use")
67  return self.async_abortasync_abortasync_abort(reason="start_server_failed")
68 
69  @callback
70  def _async_show_form(self, errors=None):
71  """Show the config flow form to the user."""
72  data_schema = {vol.Required(CONF_PORT, default=DEFAULT_PORT): int}
73 
74  return self.async_show_formasync_show_formasync_show_form(
75  step_id="user", data_schema=vol.Schema(data_schema), errors=errors or {}
76  )
77 
78 
80  """Wiffi server setup option flow."""
81 
82  async def async_step_init(
83  self, user_input: dict[str, int] | None = None
84  ) -> ConfigFlowResult:
85  """Manage the options."""
86  if user_input is not None:
87  return self.async_create_entryasync_create_entry(title="", data=user_input)
88 
89  return self.async_show_formasync_show_form(
90  step_id="init",
91  data_schema=vol.Schema(
92  {
93  vol.Optional(
94  CONF_TIMEOUT,
95  default=self.config_entryconfig_entryconfig_entry.options.get(
96  CONF_TIMEOUT, DEFAULT_TIMEOUT
97  ),
98  ): int,
99  }
100  ),
101  )
ConfigFlowResult async_step_init(self, dict[str, int]|None user_input=None)
Definition: config_flow.py:84
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
OptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:35
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
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)
None config_entry(self, ConfigEntry value)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)