Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Evil Genius Labs integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 import logging
7 from typing import Any
8 
9 import aiohttp
10 import pyevilgenius
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import HomeAssistantError
16 from homeassistant.helpers import aiohttp_client
17 
18 from .const import DOMAIN
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
23 async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
24  """Validate the user input allows us to connect.
25 
26  Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
27  """
28  hub = pyevilgenius.EvilGeniusDevice(
29  data["host"], aiohttp_client.async_get_clientsession(hass)
30  )
31 
32  try:
33  async with asyncio.timeout(10):
34  data = await hub.get_all()
35  info = await hub.get_info()
36  except aiohttp.ClientError as err:
37  _LOGGER.debug("Unable to connect: %s", err)
38  raise CannotConnect from err
39 
40  return {"title": data["name"]["value"], "unique_id": info["wiFiChipId"]}
41 
42 
43 class EvilGeniusLabsConfigFlow(ConfigFlow, domain=DOMAIN):
44  """Handle a config flow for Evil Genius Labs."""
45 
46  VERSION = 1
47 
48  async def async_step_user(
49  self, user_input: dict[str, Any] | None = None
50  ) -> ConfigFlowResult:
51  """Handle the initial step."""
52  if user_input is None:
53  return self.async_show_formasync_show_formasync_show_form(
54  step_id="user",
55  data_schema=vol.Schema(
56  {
57  vol.Required("host"): str,
58  }
59  ),
60  )
61 
62  errors = {}
63 
64  try:
65  info = await validate_input(self.hass, user_input)
66  except TimeoutError:
67  errors["base"] = "timeout"
68  except CannotConnect:
69  errors["base"] = "cannot_connect"
70  except Exception:
71  _LOGGER.exception("Unexpected exception")
72  errors["base"] = "unknown"
73  else:
74  await self.async_set_unique_idasync_set_unique_id(info["unique_id"])
75  return self.async_create_entryasync_create_entryasync_create_entry(title=info["title"], data=user_input)
76 
77  return self.async_show_formasync_show_formasync_show_form(
78  step_id="user",
79  data_schema=vol.Schema(
80  {
81  vol.Required("host", default=user_input["host"]): str,
82  }
83  ),
84  errors=errors,
85  )
86 
87 
89  """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:50
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)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:23