Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Yale Smart Alarm 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 from yalesmartalarmclient.client import YaleSmartAlarmClient
10 from yalesmartalarmclient.exceptions import AuthenticationError
11 
12 from homeassistant.config_entries import (
13  ConfigEntry,
14  ConfigFlow,
15  ConfigFlowResult,
16  OptionsFlow,
17 )
18 from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
19 from homeassistant.core import callback
21 
22 from .const import (
23  CONF_AREA_ID,
24  CONF_LOCK_CODE_DIGITS,
25  DEFAULT_AREA_ID,
26  DEFAULT_NAME,
27  DOMAIN,
28  YALE_BASE_ERRORS,
29 )
30 
31 DATA_SCHEMA = vol.Schema(
32  {
33  vol.Required(CONF_USERNAME): cv.string,
34  vol.Required(CONF_PASSWORD): cv.string,
35  vol.Required(CONF_AREA_ID, default=DEFAULT_AREA_ID): cv.string,
36  }
37 )
38 
39 DATA_SCHEMA_AUTH = vol.Schema(
40  {
41  vol.Required(CONF_PASSWORD): cv.string,
42  }
43 )
44 
45 OPTIONS_SCHEMA = vol.Schema(
46  {
47  vol.Optional(
48  CONF_LOCK_CODE_DIGITS,
49  ): int,
50  }
51 )
52 
53 
54 def validate_credentials(username: str, password: str) -> dict[str, Any]:
55  """Validate credentials."""
56  errors: dict[str, str] = {}
57  try:
58  YaleSmartAlarmClient(username, password)
59  except AuthenticationError:
60  errors = {"base": "invalid_auth"}
61  except YALE_BASE_ERRORS:
62  errors = {"base": "cannot_connect"}
63  return errors
64 
65 
66 class YaleConfigFlow(ConfigFlow, domain=DOMAIN):
67  """Handle a config flow for Yale integration."""
68 
69  VERSION = 2
70 
71  @staticmethod
72  @callback
73  def async_get_options_flow(config_entry: ConfigEntry) -> YaleOptionsFlowHandler:
74  """Get the options flow for this handler."""
75  return YaleOptionsFlowHandler()
76 
77  async def async_step_reauth(
78  self, entry_data: Mapping[str, Any]
79  ) -> ConfigFlowResult:
80  """Handle initiation of re-authentication with Yale."""
81  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
82 
84  self, user_input: dict[str, Any] | None = None
85  ) -> ConfigFlowResult:
86  """Dialog that informs the user that reauth is required."""
87  errors: dict[str, str] = {}
88 
89  if user_input is not None:
90  reauth_entry = self._get_reauth_entry_get_reauth_entry()
91  username = reauth_entry.data[CONF_USERNAME]
92  password = user_input[CONF_PASSWORD]
93 
94  errors = await self.hass.async_add_executor_job(
95  validate_credentials, username, password
96  )
97  if not errors:
98  return self.async_update_reload_and_abortasync_update_reload_and_abort(
99  reauth_entry,
100  data_updates={CONF_PASSWORD: password},
101  )
102 
103  return self.async_show_formasync_show_formasync_show_form(
104  step_id="reauth_confirm",
105  data_schema=DATA_SCHEMA_AUTH,
106  errors=errors,
107  )
108 
110  self, user_input: dict[str, Any] | None = None
111  ) -> ConfigFlowResult:
112  """Handle reconfiguration of existing entry."""
113  errors: dict[str, str] = {}
114 
115  if user_input is not None:
116  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
117  username = user_input[CONF_USERNAME]
118 
119  errors = await self.hass.async_add_executor_job(
120  validate_credentials, username, user_input[CONF_PASSWORD]
121  )
122  if (
123  username != reconfigure_entry.unique_id
124  and await self.async_set_unique_idasync_set_unique_id(username)
125  ):
126  errors["base"] = "unique_id_exists"
127  if not errors:
128  return self.async_update_reload_and_abortasync_update_reload_and_abort(
129  reconfigure_entry,
130  unique_id=username,
131  data_updates=user_input,
132  )
133 
134  return self.async_show_formasync_show_formasync_show_form(
135  step_id="reconfigure",
136  data_schema=DATA_SCHEMA,
137  errors=errors,
138  )
139 
140  async def async_step_user(
141  self, user_input: dict[str, Any] | None = None
142  ) -> ConfigFlowResult:
143  """Handle the initial step."""
144  errors: dict[str, str] = {}
145 
146  if user_input is not None:
147  username = user_input[CONF_USERNAME]
148  password = user_input[CONF_PASSWORD]
149  name = DEFAULT_NAME
150  area = user_input.get(CONF_AREA_ID, DEFAULT_AREA_ID)
151 
152  errors = await self.hass.async_add_executor_job(
153  validate_credentials, username, password
154  )
155  if not errors:
156  await self.async_set_unique_idasync_set_unique_id(username)
157  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
158 
159  return self.async_create_entryasync_create_entryasync_create_entry(
160  title=username,
161  data={
162  CONF_USERNAME: username,
163  CONF_PASSWORD: password,
164  CONF_NAME: name,
165  CONF_AREA_ID: area,
166  },
167  )
168 
169  return self.async_show_formasync_show_formasync_show_form(
170  step_id="user",
171  data_schema=DATA_SCHEMA,
172  errors=errors,
173  )
174 
175 
177  """Handle Yale options."""
178 
179  async def async_step_init(
180  self, user_input: dict[str, Any] | None = None
181  ) -> ConfigFlowResult:
182  """Manage Yale options."""
183 
184  if user_input is not None:
185  return self.async_create_entryasync_create_entry(data=user_input)
186 
187  return self.async_show_formasync_show_form(
188  step_id="init",
189  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
190  OPTIONS_SCHEMA,
191  self.config_entryconfig_entryconfig_entry.options,
192  ),
193  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:85
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:79
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:142
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:111
YaleOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:73
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:181
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_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)
None config_entry(self, ConfigEntry value)
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)
dict[str, Any] validate_credentials(str username, str password)
Definition: config_flow.py:54