Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for youless integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 from urllib.error import HTTPError, URLError
8 
9 import voluptuous as vol
10 from youless_api import YoulessAPI
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_DEVICE, CONF_HOST
14 
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
20 
21 
22 class YoulessConfigFlow(ConfigFlow, domain=DOMAIN):
23  """Handle a config flow for youless."""
24 
25  VERSION = 1
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle the initial step."""
31  errors = {}
32  if user_input is not None:
33  try:
34  api = YoulessAPI(user_input[CONF_HOST])
35  await self.hass.async_add_executor_job(api.initialize)
36  except (HTTPError, URLError):
37  _LOGGER.exception("Cannot connect to host")
38  errors["base"] = "cannot_connect"
39  else:
40  return self.async_create_entryasync_create_entryasync_create_entry(
41  title=user_input[CONF_HOST],
42  data={
43  CONF_HOST: user_input[CONF_HOST],
44  CONF_DEVICE: api.mac_address,
45  },
46  )
47 
48  return self.async_show_formasync_show_formasync_show_form(
49  step_id="user", data_schema=DATA_SCHEMA, errors=errors
50  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
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)