Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for AirTouch4."""
2 
3 from typing import Any
4 
5 from airtouch4pyapi import AirTouch, AirTouchStatus
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_HOST
10 
11 from .const import DOMAIN
12 
13 DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
14 
15 
16 class AirtouchConfigFlow(ConfigFlow, domain=DOMAIN):
17  """Handle an Airtouch config flow."""
18 
19  VERSION = 1
20 
21  async def async_step_user(
22  self, user_input: dict[str, Any] | None = None
23  ) -> ConfigFlowResult:
24  """Handle a flow initialized by the user."""
25  if user_input is None:
26  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=DATA_SCHEMA)
27 
28  errors = {}
29 
30  host = user_input[CONF_HOST]
31  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: host})
32 
33  airtouch = AirTouch(host)
34  await airtouch.UpdateInfo()
35  airtouch_status = airtouch.Status
36  airtouch_has_groups = bool(
37  airtouch.Status == AirTouchStatus.OK and airtouch.GetGroups()
38  )
39 
40  if airtouch_status != AirTouchStatus.OK:
41  errors["base"] = "cannot_connect"
42  elif not airtouch_has_groups:
43  errors["base"] = "no_units"
44 
45  if errors:
46  return self.async_show_formasync_show_formasync_show_form(
47  step_id="user", data_schema=DATA_SCHEMA, errors=errors
48  )
49 
50  return self.async_create_entryasync_create_entryasync_create_entry(
51  title=user_input[CONF_HOST],
52  data={
53  CONF_HOST: user_input[CONF_HOST],
54  },
55  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:23
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)