Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow utilities."""
2 
3 from typing import Any
4 
5 from pyvesync import VeSync
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
10 from homeassistant.core import callback
12 
13 from .const import DOMAIN
14 
15 DATA_SCHEMA = vol.Schema(
16  {
17  vol.Required(CONF_USERNAME): cv.string,
18  vol.Required(CONF_PASSWORD): cv.string,
19  }
20 )
21 
22 
23 class VeSyncFlowHandler(ConfigFlow, domain=DOMAIN):
24  """Handle a config flow."""
25 
26  VERSION = 1
27 
28  @callback
29  def _show_form(self, errors: dict[str, str] | None = None) -> ConfigFlowResult:
30  """Show form to the user."""
31  return self.async_show_formasync_show_formasync_show_form(
32  step_id="user",
33  data_schema=DATA_SCHEMA,
34  errors=errors if errors else {},
35  )
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle a flow start."""
41  if self._async_current_entries_async_current_entries():
42  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
43 
44  if not user_input:
45  return self._show_form_show_form()
46 
47  username = user_input[CONF_USERNAME]
48  password = user_input[CONF_PASSWORD]
49 
50  manager = VeSync(username, password)
51  login = await self.hass.async_add_executor_job(manager.login)
52  if not login:
53  return self._show_form_show_form(errors={"base": "invalid_auth"})
54 
55  return self.async_create_entryasync_create_entryasync_create_entry(
56  title=username,
57  data={CONF_USERNAME: username, CONF_PASSWORD: password},
58  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
ConfigFlowResult _show_form(self, dict[str, str]|None errors=None)
Definition: config_flow.py:29
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)