Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Velux integration."""
2 
3 from pyvlx import PyVLX, PyVLXException
4 import voluptuous as vol
5 
6 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
7 from homeassistant.const import CONF_HOST, CONF_PASSWORD
9 
10 from .const import DOMAIN, LOGGER
11 
12 DATA_SCHEMA = vol.Schema(
13  {
14  vol.Required(CONF_HOST): cv.string,
15  vol.Required(CONF_PASSWORD): cv.string,
16  }
17 )
18 
19 
20 class VeluxConfigFlow(ConfigFlow, domain=DOMAIN):
21  """Handle a config flow for velux."""
22 
23  async def async_step_user(
24  self, user_input: dict[str, str] | None = None
25  ) -> ConfigFlowResult:
26  """Handle the initial step."""
27  errors: dict[str, str] = {}
28 
29  if user_input is not None:
30  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
31 
32  pyvlx = PyVLX(
33  host=user_input[CONF_HOST], password=user_input[CONF_PASSWORD]
34  )
35  try:
36  await pyvlx.connect()
37  await pyvlx.disconnect()
38  except (PyVLXException, ConnectionError) as err:
39  errors["base"] = "cannot_connect"
40  LOGGER.debug("Cannot connect: %s", err)
41  except Exception as err: # noqa: BLE001
42  LOGGER.exception("Unexpected exception: %s", err)
43  errors["base"] = "unknown"
44  else:
45  return self.async_create_entryasync_create_entryasync_create_entry(
46  title=user_input[CONF_HOST],
47  data=user_input,
48  )
49 
50  data_schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(DATA_SCHEMA, user_input)
51  return self.async_show_formasync_show_formasync_show_form(
52  step_id="user",
53  data_schema=data_schema,
54  errors=errors,
55  )
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:25
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)
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_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)