Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for air-Q integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aioairq import AirQ, InvalidAuth
9 from aiohttp.client_exceptions import ClientConnectionError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
14 from homeassistant.core import callback
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17  SchemaFlowFormStep,
18  SchemaOptionsFlowHandler,
19 )
20 from homeassistant.helpers.selector import BooleanSelector
21 
22 from .const import CONF_CLIP_NEGATIVE, CONF_RETURN_AVERAGE, DOMAIN
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 STEP_USER_DATA_SCHEMA = vol.Schema(
27  {
28  vol.Required(CONF_IP_ADDRESS): str,
29  vol.Required(CONF_PASSWORD): str,
30  }
31 )
32 OPTIONS_FLOW = {
33  "init": SchemaFlowFormStep(
34  schema=vol.Schema(
35  {
36  vol.Optional(CONF_RETURN_AVERAGE, default=True): BooleanSelector(),
37  vol.Optional(CONF_CLIP_NEGATIVE, default=True): BooleanSelector(),
38  }
39  )
40  ),
41 }
42 
43 
44 class AirQConfigFlow(ConfigFlow, domain=DOMAIN):
45  """Handle a config flow for air-Q."""
46 
47  VERSION = 1
48 
49  async def async_step_user(
50  self, user_input: dict[str, Any] | None = None
51  ) -> ConfigFlowResult:
52  """Handle the initial (authentication) configuration step."""
53  if user_input is None:
54  return self.async_show_formasync_show_formasync_show_form(
55  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
56  )
57 
58  errors: dict[str, str] = {}
59 
60  session = async_get_clientsession(self.hass)
61  airq = AirQ(user_input[CONF_IP_ADDRESS], user_input[CONF_PASSWORD], session)
62  try:
63  await airq.validate()
64  except ClientConnectionError:
65  _LOGGER.debug(
66  (
67  "Failed to connect to device %s. Check the IP address / device"
68  " ID as well as whether the device is connected to power and"
69  " the WiFi"
70  ),
71  user_input[CONF_IP_ADDRESS],
72  )
73  errors["base"] = "cannot_connect"
74  except InvalidAuth:
75  _LOGGER.debug(
76  "Incorrect password for device %s", user_input[CONF_IP_ADDRESS]
77  )
78  errors["base"] = "invalid_auth"
79  else:
80  _LOGGER.debug("Successfully connected to %s", user_input[CONF_IP_ADDRESS])
81 
82  device_info = await airq.fetch_device_info()
83  await self.async_set_unique_idasync_set_unique_id(device_info["id"])
84  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
85 
86  return self.async_create_entryasync_create_entryasync_create_entry(title=device_info["name"], data=user_input)
87 
88  return self.async_show_formasync_show_formasync_show_form(
89  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
90  )
91 
92  @staticmethod
93  @callback
95  config_entry: ConfigEntry,
96  ) -> SchemaOptionsFlowHandler:
97  """Return the options flow."""
98  return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
SchemaOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:96
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:51
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)