Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure Dynalite hub."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_HOST, CONF_PORT
11 from homeassistant.helpers import config_validation as cv
12 
13 from .bridge import DynaliteBridge
14 from .const import DEFAULT_PORT, DOMAIN, LOGGER
15 from .convert_config import convert_config
16 
17 
18 class DynaliteFlowHandler(ConfigFlow, domain=DOMAIN):
19  """Handle a Dynalite config flow."""
20 
21  VERSION = 1
22 
23  def __init__(self) -> None:
24  """Initialize the Dynalite flow."""
25  self.hosthost = None
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Step when user initializes a integration."""
31  if user_input is not None:
32  return await self._try_create_try_create(user_input)
33 
34  schema = vol.Schema(
35  {
36  vol.Required(CONF_HOST): cv.string,
37  vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
38  }
39  )
40  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=schema)
41 
42  async def _try_create(self, info: dict[str, Any]) -> ConfigFlowResult:
43  """Try to connect and if successful, create entry."""
44  host = info[CONF_HOST]
45  configured_hosts = [
46  entry.data[CONF_HOST] for entry in self._async_current_entries_async_current_entries()
47  ]
48  if host in configured_hosts:
49  return self.async_abortasync_abortasync_abort(reason="already_configured")
50  bridge = DynaliteBridge(self.hass, convert_config(info))
51  if not await bridge.async_setup():
52  LOGGER.error("Unable to setup bridge - import info=%s", info)
53  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
54  LOGGER.debug("Creating entry for the bridge - %s", info)
55  return self.async_create_entryasync_create_entryasync_create_entry(title=info[CONF_HOST], data=info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
ConfigFlowResult _try_create(self, dict[str, Any] info)
Definition: config_flow.py:42
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] convert_config(dict[str, Any]|MappingProxyType[str, Any] config)