Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Tile integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from pytile import async_login
9 from pytile.errors import InvalidAuthError, TileError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
14 from homeassistant.helpers import aiohttp_client
15 
16 from .const import DOMAIN, LOGGER
17 
18 STEP_REAUTH_SCHEMA = vol.Schema(
19  {
20  vol.Required(CONF_PASSWORD): str,
21  }
22 )
23 
24 STEP_USER_SCHEMA = vol.Schema(
25  {
26  vol.Required(CONF_USERNAME): str,
27  vol.Required(CONF_PASSWORD): str,
28  }
29 )
30 
31 
32 class TileFlowHandler(ConfigFlow, domain=DOMAIN):
33  """Handle a Tile config flow."""
34 
35  VERSION = 1
36 
37  def __init__(self) -> None:
38  """Initialize the config flow."""
39  self._password_password: str | None = None
40  self._username_username: str | None = None
41 
42  async def _async_verify(self, step_id: str, schema: vol.Schema) -> ConfigFlowResult:
43  """Attempt to authenticate the provided credentials."""
44  assert self._username_username
45  assert self._password_password
46 
47  errors = {}
48  session = aiohttp_client.async_get_clientsession(self.hass)
49 
50  try:
51  await async_login(self._username_username, self._password_password, session=session)
52  except InvalidAuthError:
53  errors["base"] = "invalid_auth"
54  except TileError as err:
55  LOGGER.error("Unknown Tile error: %s", err)
56  errors["base"] = "unknown"
57 
58  if errors:
59  return self.async_show_formasync_show_formasync_show_form(
60  step_id=step_id, data_schema=schema, errors=errors
61  )
62 
63  data = {CONF_USERNAME: self._username_username, CONF_PASSWORD: self._password_password}
64 
65  if existing_entry := await self.async_set_unique_idasync_set_unique_id(self._username_username):
66  self.hass.config_entries.async_update_entry(existing_entry, data=data)
67  self.hass.async_create_task(
68  self.hass.config_entries.async_reload(existing_entry.entry_id)
69  )
70  return self.async_abortasync_abortasync_abort(reason="reauth_successful")
71 
72  return self.async_create_entryasync_create_entryasync_create_entry(title=self._username_username, data=data)
73 
74  async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
75  """Import a config entry from configuration.yaml."""
76  return await self.async_step_userasync_step_userasync_step_user(import_data)
77 
78  async def async_step_reauth(
79  self, entry_data: Mapping[str, Any]
80  ) -> ConfigFlowResult:
81  """Handle configuration by re-auth."""
82  self._username_username = entry_data[CONF_USERNAME]
83  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
84 
86  self, user_input: dict[str, str] | None = None
87  ) -> ConfigFlowResult:
88  """Handle re-auth completion."""
89  if not user_input:
90  return self.async_show_formasync_show_formasync_show_form(
91  step_id="reauth_confirm", data_schema=STEP_REAUTH_SCHEMA
92  )
93 
94  self._password_password = user_input[CONF_PASSWORD]
95 
96  return await self._async_verify_async_verify("reauth_confirm", STEP_REAUTH_SCHEMA)
97 
98  async def async_step_user(
99  self, user_input: dict[str, Any] | None = None
100  ) -> ConfigFlowResult:
101  """Handle the start of the config flow."""
102  if not user_input:
103  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=STEP_USER_SCHEMA)
104 
105  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME])
106  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
107 
108  self._username_username = user_input[CONF_USERNAME]
109  self._password_password = user_input[CONF_PASSWORD]
110 
111  return await self._async_verify_async_verify("user", STEP_USER_SCHEMA)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:87
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:100
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:80
ConfigFlowResult _async_verify(self, str step_id, vol.Schema schema)
Definition: config_flow.py:42
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
Definition: config_flow.py:74
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_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)
_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)