Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for ViCare integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from PyViCare.PyViCareUtils import (
10  PyViCareInvalidConfigurationError,
11  PyViCareInvalidCredentialsError,
12 )
13 import voluptuous as vol
14 
15 from homeassistant.components import dhcp
16 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17 from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
19 from homeassistant.helpers.device_registry import format_mac
20 
21 from . import vicare_login
22 from .const import (
23  CONF_HEATING_TYPE,
24  DEFAULT_HEATING_TYPE,
25  DOMAIN,
26  VICARE_NAME,
27  HeatingType,
28 )
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 REAUTH_SCHEMA = vol.Schema(
33  {
34  vol.Required(CONF_PASSWORD): cv.string,
35  vol.Required(CONF_CLIENT_ID): cv.string,
36  }
37 )
38 
39 USER_SCHEMA = REAUTH_SCHEMA.extend(
40  {
41  vol.Required(CONF_USERNAME): cv.string,
42  vol.Required(CONF_HEATING_TYPE, default=DEFAULT_HEATING_TYPE.value): vol.In(
43  [e.value for e in HeatingType]
44  ),
45  }
46 )
47 
48 
49 class ViCareConfigFlow(ConfigFlow, domain=DOMAIN):
50  """Handle a config flow for ViCare."""
51 
52  VERSION = 1
53 
54  async def async_step_user(
55  self, user_input: dict[str, Any] | None = None
56  ) -> ConfigFlowResult:
57  """Invoke when a user initiates a flow via the user interface."""
58  if self._async_current_entries_async_current_entries():
59  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
60 
61  errors: dict[str, str] = {}
62 
63  if user_input is not None:
64  try:
65  await self.hass.async_add_executor_job(
66  vicare_login, self.hass, user_input
67  )
68  except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError):
69  errors["base"] = "invalid_auth"
70  else:
71  return self.async_create_entryasync_create_entryasync_create_entry(title=VICARE_NAME, data=user_input)
72 
73  return self.async_show_formasync_show_formasync_show_form(
74  step_id="user",
75  data_schema=USER_SCHEMA,
76  errors=errors,
77  )
78 
79  async def async_step_reauth(
80  self, entry_data: Mapping[str, Any]
81  ) -> ConfigFlowResult:
82  """Handle re-authentication with ViCare."""
83  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
84 
86  self, user_input: dict[str, Any] | None = None
87  ) -> ConfigFlowResult:
88  """Confirm re-authentication with ViCare."""
89  errors: dict[str, str] = {}
90 
91  reauth_entry = self._get_reauth_entry_get_reauth_entry()
92  if user_input:
93  data = {
94  **reauth_entry.data,
95  **user_input,
96  }
97 
98  try:
99  await self.hass.async_add_executor_job(vicare_login, self.hass, data)
100  except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError):
101  errors["base"] = "invalid_auth"
102  else:
103  return self.async_update_reload_and_abortasync_update_reload_and_abort(reauth_entry, data=data)
104 
105  return self.async_show_formasync_show_formasync_show_form(
106  step_id="reauth_confirm",
107  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
108  REAUTH_SCHEMA, reauth_entry.data
109  ),
110  errors=errors,
111  )
112 
113  async def async_step_dhcp(
114  self, discovery_info: dhcp.DhcpServiceInfo
115  ) -> ConfigFlowResult:
116  """Invoke when a Viessmann MAC address is discovered on the network."""
117  formatted_mac = format_mac(discovery_info.macaddress)
118  _LOGGER.debug("Found device with mac %s", formatted_mac)
119 
120  await self.async_set_unique_idasync_set_unique_id(formatted_mac)
121  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
122 
123  if self._async_current_entries_async_current_entries():
124  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
125 
126  return await self.async_step_userasync_step_userasync_step_user()
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:81
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:56
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
Definition: config_flow.py:115
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:87
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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_step_user(self, dict[str, Any]|None user_input=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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)