Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Meater."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from meater import AuthenticationError, MeaterApi, ServiceUnavailableError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
13 from homeassistant.helpers import aiohttp_client
14 
15 from .const import DOMAIN
16 
17 REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
18 USER_SCHEMA = vol.Schema(
19  {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
20 )
21 
22 
23 class MeaterConfigFlow(ConfigFlow, domain=DOMAIN):
24  """Meater Config Flow."""
25 
26  _data_schema = USER_SCHEMA
27  _username: str
28 
29  async def async_step_user(
30  self, user_input: dict[str, str] | None = None
31  ) -> ConfigFlowResult:
32  """Define the login user step."""
33  if user_input is None:
34  return self.async_show_formasync_show_formasync_show_form(
35  step_id="user",
36  data_schema=self._data_schema_data_schema,
37  )
38 
39  username: str = user_input[CONF_USERNAME]
40  await self.async_set_unique_idasync_set_unique_id(username.lower())
41  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
42 
43  username = user_input[CONF_USERNAME]
44  password = user_input[CONF_PASSWORD]
45 
46  return await self._try_connect_meater_try_connect_meater("user", None, username, password)
47 
48  async def async_step_reauth(
49  self, entry_data: Mapping[str, Any]
50  ) -> ConfigFlowResult:
51  """Handle configuration by re-auth."""
52  self._data_schema_data_schema = REAUTH_SCHEMA
53  self._username_username = entry_data[CONF_USERNAME]
54  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
55 
57  self, user_input: dict[str, str] | None = None
58  ) -> ConfigFlowResult:
59  """Handle re-auth completion."""
60  placeholders = {"username": self._username_username}
61  if not user_input:
62  return self.async_show_formasync_show_formasync_show_form(
63  step_id="reauth_confirm",
64  data_schema=self._data_schema_data_schema,
65  description_placeholders=placeholders,
66  )
67 
68  password = user_input[CONF_PASSWORD]
69  return await self._try_connect_meater_try_connect_meater(
70  "reauth_confirm", placeholders, self._username_username, password
71  )
72 
74  self, step_id, placeholders: dict[str, str] | None, username: str, password: str
75  ) -> ConfigFlowResult:
76  session = aiohttp_client.async_get_clientsession(self.hass)
77 
78  api = MeaterApi(session)
79  errors = {}
80 
81  try:
82  await api.authenticate(username, password)
83  except AuthenticationError:
84  errors["base"] = "invalid_auth"
85  except ServiceUnavailableError:
86  errors["base"] = "service_unavailable_error"
87  except Exception: # noqa: BLE001
88  errors["base"] = "unknown_auth_error"
89  else:
90  data = {"username": username, "password": password}
91  existing_entry = await self.async_set_unique_idasync_set_unique_id(username.lower())
92  if existing_entry:
93  self.hass.config_entries.async_update_entry(existing_entry, data=data)
94  await self.hass.config_entries.async_reload(existing_entry.entry_id)
95  return self.async_abortasync_abortasync_abort(reason="reauth_successful")
96  return self.async_create_entryasync_create_entryasync_create_entry(
97  title="Meater",
98  data=data,
99  )
100 
101  return self.async_show_formasync_show_formasync_show_form(
102  step_id=step_id,
103  data_schema=self._data_schema_data_schema,
104  description_placeholders=placeholders,
105  errors=errors,
106  )
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:31
ConfigFlowResult _try_connect_meater(self, step_id, dict[str, str]|None placeholders, str username, str password)
Definition: config_flow.py:75
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:58
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:50
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)