Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the devolo home control integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.components import zeroconf
11 from homeassistant.config_entries import (
12  SOURCE_REAUTH,
13  ConfigEntry,
14  ConfigFlow,
15  ConfigFlowResult,
16 )
17 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
18 from homeassistant.core import callback
19 
20 from . import configure_mydevolo
21 from .const import CONF_MYDEVOLO, DEFAULT_MYDEVOLO, DOMAIN, SUPPORTED_MODEL_TYPES
22 from .exceptions import CredentialsInvalid, UuidChanged
23 
24 
26  """Handle a devolo HomeControl config flow."""
27 
28  VERSION = 1
29 
30  _reauth_entry: ConfigEntry
31 
32  def __init__(self) -> None:
33  """Initialize devolo Home Control flow."""
34  self.data_schemadata_schema = {
35  vol.Required(CONF_USERNAME): str,
36  vol.Required(CONF_PASSWORD): str,
37  }
38  self._url_url = DEFAULT_MYDEVOLO
39 
40  async def async_step_user(
41  self, user_input: dict[str, Any] | None = None
42  ) -> ConfigFlowResult:
43  """Handle a flow initiated by the user."""
44  if self.show_advanced_optionsshow_advanced_options:
45  self.data_schemadata_schema[vol.Required(CONF_MYDEVOLO, default=self._url_url)] = str
46  if user_input is None:
47  return self._show_form_show_form(step_id="user")
48  try:
49  return await self._connect_mydevolo_connect_mydevolo(user_input)
50  except CredentialsInvalid:
51  return self._show_form_show_form(step_id="user", errors={"base": "invalid_auth"})
52 
54  self, discovery_info: zeroconf.ZeroconfServiceInfo
55  ) -> ConfigFlowResult:
56  """Handle zeroconf discovery."""
57  # Check if it is a gateway
58  if discovery_info.properties.get("MT") in SUPPORTED_MODEL_TYPES:
59  await self._async_handle_discovery_without_unique_id_async_handle_discovery_without_unique_id()
60  return await self.async_step_zeroconf_confirmasync_step_zeroconf_confirm()
61  return self.async_abortasync_abortasync_abort(reason="Not a devolo Home Control gateway.")
62 
64  self, user_input: dict[str, Any] | None = None
65  ) -> ConfigFlowResult:
66  """Handle a flow initiated by zeroconf."""
67  if user_input is None:
68  return self._show_form_show_form(step_id="zeroconf_confirm")
69  try:
70  return await self._connect_mydevolo_connect_mydevolo(user_input)
71  except CredentialsInvalid:
72  return self._show_form_show_form(
73  step_id="zeroconf_confirm", errors={"base": "invalid_auth"}
74  )
75 
76  async def async_step_reauth(
77  self, entry_data: Mapping[str, Any]
78  ) -> ConfigFlowResult:
79  """Handle reauthentication."""
80  self._reauth_entry_reauth_entry = self._get_reauth_entry_get_reauth_entry()
81  self._url_url = entry_data[CONF_MYDEVOLO]
82  self.data_schemadata_schema = {
83  vol.Required(CONF_USERNAME, default=entry_data[CONF_USERNAME]): str,
84  vol.Required(CONF_PASSWORD): str,
85  }
86  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
87 
89  self, user_input: dict[str, Any] | None = None
90  ) -> ConfigFlowResult:
91  """Handle a flow initiated by reauthentication."""
92  if user_input is None:
93  return self._show_form_show_form(step_id="reauth_confirm")
94  try:
95  return await self._connect_mydevolo_connect_mydevolo(user_input)
96  except CredentialsInvalid:
97  return self._show_form_show_form(
98  step_id="reauth_confirm", errors={"base": "invalid_auth"}
99  )
100  except UuidChanged:
101  return self._show_form_show_form(
102  step_id="reauth_confirm", errors={"base": "reauth_failed"}
103  )
104 
105  async def _connect_mydevolo(self, user_input: dict[str, Any]) -> ConfigFlowResult:
106  """Connect to mydevolo."""
107  user_input[CONF_MYDEVOLO] = user_input.get(CONF_MYDEVOLO, self._url_url)
108  mydevolo = configure_mydevolo(conf=user_input)
109  credentials_valid = await self.hass.async_add_executor_job(
110  mydevolo.credentials_valid
111  )
112  if not credentials_valid:
113  raise CredentialsInvalid
114  uuid = await self.hass.async_add_executor_job(mydevolo.uuid)
115 
116  if self.sourcesourcesource != SOURCE_REAUTH:
117  await self.async_set_unique_idasync_set_unique_id(uuid)
118  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
119  return self.async_create_entryasync_create_entryasync_create_entry(
120  title="devolo Home Control",
121  data={
122  CONF_PASSWORD: mydevolo.password,
123  CONF_USERNAME: mydevolo.user,
124  CONF_MYDEVOLO: mydevolo.url,
125  },
126  )
127 
128  if self._reauth_entry_reauth_entry.unique_id != uuid:
129  # The old user and the new user are not the same. This could mess-up everything as all unique IDs might change.
130  raise UuidChanged
131 
132  return self.async_update_reload_and_abortasync_update_reload_and_abort(
133  self._reauth_entry_reauth_entry, data=user_input, unique_id=uuid
134  )
135 
136  @callback
138  self, step_id: str, errors: dict[str, str] | None = None
139  ) -> ConfigFlowResult:
140  """Show the form to the user."""
141  return self.async_show_formasync_show_formasync_show_form(
142  step_id=step_id,
143  data_schema=vol.Schema(self.data_schemadata_schema),
144  errors=errors if errors else {},
145  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:78
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:90
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:42
ConfigFlowResult async_step_zeroconf_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:65
ConfigFlowResult _show_form(self, str step_id, dict[str, str]|None errors=None)
Definition: config_flow.py:139
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:55
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
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)
bool show_advanced_options(self)
_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)
str|None source(self)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
Mydevolo configure_mydevolo(dict[str, Any]|MappingProxyType[str, Any] conf)
Definition: __init__.py:100