Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Lupusec integration."""
2 
3 from json import JSONDecodeError
4 import logging
5 from typing import Any
6 
7 import lupupy
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 DATA_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_HOST): str,
22  vol.Required(CONF_USERNAME): str,
23  vol.Required(CONF_PASSWORD): str,
24  }
25 )
26 
27 
28 class LupusecConfigFlowHandler(ConfigFlow, domain=DOMAIN):
29  """Lupusec config flow."""
30 
31  async def async_step_user(
32  self, user_input: dict[str, Any] | None = None
33  ) -> ConfigFlowResult:
34  """Handle a flow initiated by the user."""
35  errors = {}
36 
37  if user_input is not None:
38  self._async_abort_entries_match_async_abort_entries_match(user_input)
39  host = user_input[CONF_HOST]
40  username = user_input[CONF_USERNAME]
41  password = user_input[CONF_PASSWORD]
42 
43  try:
44  await test_host_connection(self.hass, host, username, password)
45  except CannotConnect:
46  errors["base"] = "cannot_connect"
47  except JSONDecodeError:
48  errors["base"] = "cannot_connect"
49  except Exception:
50  _LOGGER.exception("Unexpected exception")
51  errors["base"] = "unknown"
52 
53  else:
54  return self.async_create_entryasync_create_entryasync_create_entry(
55  title=host,
56  data=user_input,
57  )
58 
59  return self.async_show_formasync_show_formasync_show_form(
60  step_id="user", data_schema=DATA_SCHEMA, errors=errors
61  )
62 
63 
65  hass: HomeAssistant, host: str, username: str, password: str
66 ):
67  """Test if the host is reachable and is actually a Lupusec device."""
68 
69  try:
70  await hass.async_add_executor_job(lupupy.Lupusec, username, password, host)
71  except lupupy.LupusecException as ex:
72  _LOGGER.error("Failed to connect to Lupusec device at %s", host)
73  raise CannotConnect from ex
74 
75 
77  """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:33
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)
_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 test_host_connection(HomeAssistant hass, str host, str username, str password)
Definition: config_flow.py:66