Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Venstar integration."""
2 
3 from typing import Any
4 
5 from venstarcolortouch import VenstarColorTouch
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import (
10  CONF_HOST,
11  CONF_PASSWORD,
12  CONF_PIN,
13  CONF_SSL,
14  CONF_USERNAME,
15 )
16 from homeassistant.core import HomeAssistant
17 from homeassistant.exceptions import HomeAssistantError
18 
19 from .const import _LOGGER, DOMAIN, VENSTAR_TIMEOUT
20 
21 
22 async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> str:
23  """Validate the user input allows us to connect."""
24  username = data.get(CONF_USERNAME)
25  password = data.get(CONF_PASSWORD)
26  pin = data.get(CONF_PIN)
27  host = data[CONF_HOST]
28  timeout = VENSTAR_TIMEOUT
29  protocol = "https" if data[CONF_SSL] else "http"
30 
31  client = VenstarColorTouch(
32  addr=host,
33  timeout=timeout,
34  user=username,
35  password=password,
36  pin=pin,
37  proto=protocol,
38  )
39 
40  # perform a full info pull, because this calls login also.
41 
42  info_success = await hass.async_add_executor_job(client.update_info)
43  if not info_success:
44  raise CannotConnect
45 
46  return client.name
47 
48 
49 class VenstarConfigFlow(ConfigFlow, domain=DOMAIN):
50  """Handle a venstar config flow."""
51 
52  VERSION = 1
53 
54  async def async_step_user(
55  self, user_input: dict[str, Any] | None = None
56  ) -> ConfigFlowResult:
57  """Create config entry. Show the setup form to the user."""
58  errors = {}
59 
60  if user_input is not None:
61  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
62 
63  try:
64  title = await validate_input(self.hass, user_input)
65  except CannotConnect:
66  errors["base"] = "cannot_connect"
67  except Exception: # noqa: BLE001
68  _LOGGER.exception("Unexpected exception")
69  errors["base"] = "unknown"
70  else:
71  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data=user_input)
72 
73  return self.async_show_formasync_show_formasync_show_form(
74  step_id="user",
75  data_schema=vol.Schema(
76  {
77  vol.Required(CONF_HOST): str,
78  vol.Optional(CONF_USERNAME): str,
79  vol.Optional(CONF_PASSWORD): str,
80  vol.Optional(CONF_PIN): str,
81  vol.Optional(CONF_SSL, default=False): bool,
82  }
83  ),
84  errors=errors,
85  )
86 
87  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
88  """Import entry from configuration.yaml."""
89  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]})
90  return await self.async_step_userasync_step_userasync_step_user(
91  {
92  CONF_HOST: import_data[CONF_HOST],
93  CONF_USERNAME: import_data.get(CONF_USERNAME),
94  CONF_PASSWORD: import_data.get(CONF_PASSWORD),
95  CONF_PIN: import_data.get(CONF_PIN),
96  CONF_SSL: import_data[CONF_SSL],
97  }
98  )
99 
100 
102  """Error to indicate we cannot connect."""
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:87
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:56
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)
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)
str validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:22