Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for laundrify 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 laundrify_aio import LaundrifyAPI
10 from laundrify_aio.exceptions import (
11  ApiConnectionException,
12  InvalidFormat,
13  UnknownAuthCode,
14 )
15 from voluptuous import Required, Schema
16 
17 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
18 from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CODE
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20 
21 from .const import DOMAIN
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 CONFIG_SCHEMA = Schema({Required(CONF_CODE): str})
26 
27 
28 class LaundrifyConfigFlow(ConfigFlow, domain=DOMAIN):
29  """Handle a config flow for laundrify."""
30 
31  VERSION = 1
32  MINOR_VERSION = 2
33 
34  async def async_step_user(
35  self, user_input: dict[str, Any] | None = None
36  ) -> ConfigFlowResult:
37  """Handle a flow initialized by the user."""
38  return await self.async_step_initasync_step_init(user_input)
39 
40  async def async_step_init(
41  self, user_input: dict[str, Any] | None = None
42  ) -> ConfigFlowResult:
43  """Handle the initial step."""
44  if user_input is None:
45  return self.async_show_formasync_show_formasync_show_form(step_id="init", data_schema=CONFIG_SCHEMA)
46 
47  errors = {}
48 
49  try:
50  access_token = await LaundrifyAPI.exchange_auth_code(user_input[CONF_CODE])
51 
52  session = async_get_clientsession(self.hass)
53  api_client = LaundrifyAPI(access_token, session)
54 
55  account_id = await api_client.get_account_id()
56  except InvalidFormat:
57  errors[CONF_CODE] = "invalid_format"
58  except UnknownAuthCode:
59  errors[CONF_CODE] = "invalid_auth"
60  except ApiConnectionException:
61  errors["base"] = "cannot_connect"
62  except Exception:
63  _LOGGER.exception("Unexpected exception")
64  errors["base"] = "unknown"
65  else:
66  entry_data = {CONF_ACCESS_TOKEN: access_token}
67 
68  await self.async_set_unique_idasync_set_unique_id(str(account_id))
69  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
70 
71  # Create a new entry if it doesn't exist
72  return self.async_create_entryasync_create_entryasync_create_entry(
73  title=DOMAIN,
74  data=entry_data,
75  )
76 
77  return self.async_show_formasync_show_formasync_show_form(
78  step_id="init", data_schema=CONFIG_SCHEMA, errors=errors
79  )
80 
81  async def async_step_reauth(
82  self, entry_data: Mapping[str, Any]
83  ) -> ConfigFlowResult:
84  """Perform reauth upon an API authentication error."""
85  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
86 
88  self, user_input: dict[str, Any] | None = None
89  ) -> ConfigFlowResult:
90  """Dialog that informs the user that reauth is required."""
91  if user_input is None:
92  return self.async_show_formasync_show_formasync_show_form(
93  step_id="reauth_confirm",
94  data_schema=Schema({}),
95  )
96  return await self.async_step_initasync_step_init()
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:36
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:83
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:42
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:89
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_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)
str
_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)