Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for OurGroceries integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aiohttp import ClientError
9 from ourgroceries import OurGroceries
10 from ourgroceries.exceptions import InvalidLoginException
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 STEP_USER_DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_USERNAME): str,
23  vol.Required(CONF_PASSWORD): str,
24  }
25 )
26 
27 
28 class OurGroceriesConfigFlow(ConfigFlow, domain=DOMAIN):
29  """Handle a config flow for OurGroceries."""
30 
31  VERSION = 1
32 
33  async def async_step_user(
34  self, user_input: dict[str, Any] | None = None
35  ) -> ConfigFlowResult:
36  """Handle the initial step."""
37  errors: dict[str, str] = {}
38  if user_input is not None:
39  og = OurGroceries(user_input[CONF_USERNAME], user_input[CONF_PASSWORD])
40  try:
41  await og.login()
42  except (TimeoutError, ClientError):
43  errors["base"] = "cannot_connect"
44  except InvalidLoginException:
45  errors["base"] = "invalid_auth"
46  except Exception:
47  _LOGGER.exception("Unexpected exception")
48  errors["base"] = "unknown"
49  else:
50  return self.async_create_entryasync_create_entryasync_create_entry(
51  title=user_input[CONF_USERNAME], data=user_input
52  )
53 
54  return self.async_show_formasync_show_formasync_show_form(
55  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
56  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
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_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)