Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the HomematicIP Cloud component."""
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 
11 from .const import _LOGGER, DOMAIN, HMIPC_AUTHTOKEN, HMIPC_HAPID, HMIPC_NAME, HMIPC_PIN
12 from .hap import HomematicipAuth
13 
14 
16  """Config flow for the HomematicIP Cloud component."""
17 
18  VERSION = 1
19 
20  auth: HomematicipAuth
21 
22  def __init__(self) -> None:
23  """Initialize HomematicIP Cloud config flow."""
24 
25  async def async_step_user(
26  self, user_input: dict[str, Any] | None = None
27  ) -> ConfigFlowResult:
28  """Handle a flow initialized by the user."""
29  return await self.async_step_init(user_input)
30 
31  async def async_step_init(
32  self, user_input: dict[str, str] | None = None
33  ) -> ConfigFlowResult:
34  """Handle a flow start."""
35  errors = {}
36 
37  if user_input is not None:
38  user_input[HMIPC_HAPID] = user_input[HMIPC_HAPID].replace("-", "").upper()
39 
40  await self.async_set_unique_idasync_set_unique_id(user_input[HMIPC_HAPID])
41  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
42 
43  self.authauth = HomematicipAuth(self.hass, user_input)
44  connected = await self.authauth.async_setup()
45  if connected:
46  _LOGGER.debug("Connection to HomematicIP Cloud established")
47  return await self.async_step_linkasync_step_link()
48 
49  _LOGGER.debug("Connection to HomematicIP Cloud failed")
50  errors["base"] = "invalid_sgtin_or_pin"
51 
52  return self.async_show_formasync_show_formasync_show_form(
53  step_id="init",
54  data_schema=vol.Schema(
55  {
56  vol.Required(HMIPC_HAPID): str,
57  vol.Optional(HMIPC_NAME): str,
58  vol.Optional(HMIPC_PIN): str,
59  }
60  ),
61  errors=errors,
62  )
63 
64  async def async_step_link(self, user_input: None = None) -> ConfigFlowResult:
65  """Attempt to link with the HomematicIP Cloud access point."""
66  errors = {}
67 
68  pressed = await self.authauth.async_checkbutton()
69  if pressed:
70  authtoken = await self.authauth.async_register()
71  if authtoken:
72  _LOGGER.debug("Write config entry for HomematicIP Cloud")
73  return self.async_create_entryasync_create_entryasync_create_entry(
74  title=self.authauth.config[HMIPC_HAPID],
75  data={
76  HMIPC_HAPID: self.authauth.config[HMIPC_HAPID],
77  HMIPC_AUTHTOKEN: authtoken,
78  HMIPC_NAME: self.authauth.config.get(HMIPC_NAME),
79  },
80  )
81  return self.async_abortasync_abortasync_abort(reason="connection_aborted")
82  errors["base"] = "press_the_button"
83 
84  return self.async_show_formasync_show_formasync_show_form(step_id="link", errors=errors)
85 
86  async def async_step_import(self, import_data: dict[str, str]) -> ConfigFlowResult:
87  """Import a new access point as a config entry."""
88  hapid = import_data[HMIPC_HAPID].replace("-", "").upper()
89  authtoken = import_data[HMIPC_AUTHTOKEN]
90  name = import_data[HMIPC_NAME]
91 
92  await self.async_set_unique_idasync_set_unique_id(hapid)
93  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
94 
95  _LOGGER.debug("Imported authentication for %s", hapid)
96  return self.async_create_entryasync_create_entryasync_create_entry(
97  title=hapid,
98  data={HMIPC_AUTHTOKEN: authtoken, HMIPC_HAPID: hapid, HMIPC_NAME: name},
99  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:27
ConfigFlowResult async_step_init(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:33
ConfigFlowResult async_step_import(self, dict[str, str] import_data)
Definition: config_flow.py:86
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_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)
None async_register(HomeAssistant hass, system_health.SystemHealthRegistration register)
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:46