Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Litter-Robot 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 pylitterbot import Account
10 from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 STEP_USER_DATA_SCHEMA = vol.Schema(
22  {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
23 )
24 
25 
26 class LitterRobotConfigFlow(ConfigFlow, domain=DOMAIN):
27  """Handle a config flow for Litter-Robot."""
28 
29  VERSION = 1
30 
31  username: str
32 
33  async def async_step_reauth(
34  self, entry_data: Mapping[str, Any]
35  ) -> ConfigFlowResult:
36  """Handle a reauthorization flow request."""
37  self.usernameusername = entry_data[CONF_USERNAME]
38  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
39 
41  self, user_input: dict[str, str] | None = None
42  ) -> ConfigFlowResult:
43  """Handle user's reauth credentials."""
44  errors = {}
45  if user_input:
46  user_input = user_input | {CONF_USERNAME: self.usernameusername}
47  if not (error := await self._async_validate_input_async_validate_input(user_input)):
48  return self.async_update_reload_and_abortasync_update_reload_and_abort(
49  self._get_reauth_entry_get_reauth_entry(), data_updates=user_input
50  )
51 
52  errors["base"] = error
53  return self.async_show_formasync_show_formasync_show_form(
54  step_id="reauth_confirm",
55  data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
56  description_placeholders={CONF_USERNAME: self.usernameusername},
57  errors=errors,
58  )
59 
60  async def async_step_user(
61  self, user_input: Mapping[str, Any] | None = None
62  ) -> ConfigFlowResult:
63  """Handle the initial step."""
64  errors = {}
65 
66  if user_input is not None:
67  self._async_abort_entries_match_async_abort_entries_match({CONF_USERNAME: user_input[CONF_USERNAME]})
68 
69  if not (error := await self._async_validate_input_async_validate_input(user_input)):
70  return self.async_create_entryasync_create_entryasync_create_entry(
71  title=user_input[CONF_USERNAME], data=user_input
72  )
73  errors["base"] = error
74 
75  return self.async_show_formasync_show_formasync_show_form(
76  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
77  )
78 
79  async def _async_validate_input(self, user_input: Mapping[str, Any]) -> str:
80  """Validate login credentials."""
81  account = Account(websession=async_get_clientsession(self.hass))
82  try:
83  await account.connect(
84  username=user_input[CONF_USERNAME],
85  password=user_input[CONF_PASSWORD],
86  )
87  await account.disconnect()
88  except LitterRobotLoginException:
89  return "invalid_auth"
90  except LitterRobotException:
91  return "cannot_connect"
92  except Exception:
93  _LOGGER.exception("Unexpected exception")
94  return "unknown"
95  return ""
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:62
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:42
str _async_validate_input(self, Mapping[str, Any] user_input)
Definition: config_flow.py:79
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:35
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)