Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Ambient PWS component."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aioambient import API
8 from aioambient.errors import AmbientError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_API_KEY
13 from homeassistant.helpers import aiohttp_client
14 
15 from .const import CONF_APP_KEY, DOMAIN
16 
17 
18 class AmbientStationFlowHandler(ConfigFlow, domain=DOMAIN):
19  """Handle an Ambient PWS config flow."""
20 
21  VERSION = 2
22 
23  def __init__(self) -> None:
24  """Initialize the config flow."""
25  self.data_schemadata_schema = vol.Schema(
26  {vol.Required(CONF_API_KEY): str, vol.Required(CONF_APP_KEY): str}
27  )
28 
29  async def _show_form(self, errors: dict | None = None) -> ConfigFlowResult:
30  """Show the form to the user."""
31  return self.async_show_formasync_show_formasync_show_form(
32  step_id="user",
33  data_schema=self.data_schemadata_schema,
34  errors=errors if errors else {},
35  )
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle the start of the config flow."""
41  if not user_input:
42  return await self._show_form_show_form()
43 
44  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_APP_KEY])
45  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
46 
47  session = aiohttp_client.async_get_clientsession(self.hass)
48  api = API(user_input[CONF_APP_KEY], user_input[CONF_API_KEY], session=session)
49 
50  try:
51  devices = await api.get_devices()
52  except AmbientError:
53  return await self._show_form_show_form({"base": "invalid_key"})
54 
55  if not devices:
56  return await self._show_form_show_form({"base": "no_devices"})
57 
58  # The Application Key (which identifies each config entry) is too long
59  # to show nicely in the UI, so we take the first 12 characters (similar
60  # to how GitHub does it):
61  return self.async_create_entryasync_create_entryasync_create_entry(
62  title=user_input[CONF_APP_KEY][:12], data=user_input
63  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
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)
_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)