Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Yardian integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pyyardian import (
9  AsyncYardianClient,
10  DeviceInfo,
11  NetworkException,
12  NotAuthorizedException,
13 )
14 import voluptuous as vol
15 
16 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
18 from homeassistant.helpers.aiohttp_client import async_get_clientsession
19 
20 from .const import DOMAIN, PRODUCT_NAME
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 STEP_USER_DATA_SCHEMA = vol.Schema(
25  {
26  vol.Required(CONF_HOST): str,
27  vol.Required(CONF_ACCESS_TOKEN): str,
28  }
29 )
30 
31 
32 class YardianConfigFlow(ConfigFlow, domain=DOMAIN):
33  """Handle a config flow for Yardian."""
34 
35  VERSION = 1
36 
37  async def async_fetch_device_info(self, host: str, access_token: str) -> DeviceInfo:
38  """Fetch device info from Yardian."""
39  yarcli = AsyncYardianClient(
40  async_get_clientsession(self.hass),
41  host,
42  access_token,
43  )
44  return await yarcli.fetch_device_info()
45 
46  async def async_step_user(
47  self, user_input: dict[str, Any] | None = None
48  ) -> ConfigFlowResult:
49  """Handle the initial step."""
50  errors: dict[str, str] = {}
51  if user_input is not None:
52  try:
53  device_info = await self.async_fetch_device_infoasync_fetch_device_info(
54  user_input["host"], user_input["access_token"]
55  )
56  except NotAuthorizedException:
57  errors["base"] = "invalid_auth"
58  except NetworkException:
59  errors["base"] = "cannot_connect"
60  except Exception:
61  _LOGGER.exception("Unexpected exception")
62  errors["base"] = "unknown"
63  else:
64  await self.async_set_unique_idasync_set_unique_id(device_info["yid"])
65  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  data=user_input | device_info,
68  title=PRODUCT_NAME,
69  )
70 
71  return self.async_show_formasync_show_formasync_show_form(
72  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
73  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:48
DeviceInfo async_fetch_device_info(self, str host, str access_token)
Definition: config_flow.py:37
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)