Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Renson integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from renson_endura_delta import renson
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import HomeAssistantError
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 STEP_USER_DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_HOST): str,
23  }
24 )
25 
26 
27 class RensonConfigFlow(ConfigFlow, domain=DOMAIN):
28  """Handle a config flow for Renson."""
29 
30  VERSION = 1
31 
32  async def validate_input(
33  self, hass: HomeAssistant, data: dict[str, Any]
34  ) -> dict[str, Any]:
35  """Validate the user input allows us to connect."""
36  api = renson.RensonVentilation(data[CONF_HOST])
37 
38  if not await self.hass.async_add_executor_job(api.connect):
39  raise CannotConnect
40 
41  return {"title": "Renson"}
42 
43  async def async_step_user(
44  self, user_input: dict[str, Any] | None = None
45  ) -> ConfigFlowResult:
46  """Handle the initial step."""
47  if user_input is None:
48  return self.async_show_formasync_show_formasync_show_form(
49  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
50  )
51 
52  errors = {}
53 
54  try:
55  info = await self.validate_inputvalidate_input(self.hass, user_input)
56  except CannotConnect:
57  errors["base"] = "cannot_connect"
58  except Exception:
59  _LOGGER.exception("Unexpected exception")
60  errors["base"] = "unknown"
61  else:
62  return self.async_create_entryasync_create_entryasync_create_entry(title=info["title"], data=user_input)
63 
64  return self.async_show_formasync_show_formasync_show_form(
65  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
66  )
67 
68 
70  """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:45
dict[str, Any] validate_input(self, HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:34
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_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)