Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Plum Lightpad."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aiohttp import ContentTypeError
9 from requests.exceptions import ConnectTimeout, HTTPError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
14 
15 from .const import DOMAIN
16 from .utils import load_plum
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 class PlumLightpadConfigFlow(ConfigFlow, domain=DOMAIN):
22  """Config flow for Plum Lightpad integration."""
23 
24  VERSION = 1
25 
26  def _show_form(self, errors=None):
27  schema = {
28  vol.Required(CONF_USERNAME): str,
29  vol.Required(CONF_PASSWORD): str,
30  }
31 
32  return self.async_show_formasync_show_formasync_show_form(
33  step_id="user",
34  data_schema=vol.Schema(schema),
35  errors=errors or {},
36  )
37 
38  async def async_step_user(
39  self, user_input: dict[str, Any] | None = None
40  ) -> ConfigFlowResult:
41  """Handle a flow initialized by the user or redirected to by import."""
42  if not user_input:
43  return self._show_form_show_form()
44 
45  username = user_input[CONF_USERNAME]
46  password = user_input[CONF_PASSWORD]
47 
48  # load Plum just so we know username/password work
49  try:
50  await load_plum(username, password, self.hass)
51  except (ContentTypeError, ConnectTimeout, HTTPError) as ex:
52  _LOGGER.error("Unable to connect/authenticate to Plum cloud: %s", str(ex))
53  return self._show_form_show_form({"base": "cannot_connect"})
54 
55  await self.async_set_unique_idasync_set_unique_id(username)
56  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
57 
58  return self.async_create_entryasync_create_entryasync_create_entry(
59  title=username, data={CONF_USERNAME: username, CONF_PASSWORD: password}
60  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:40
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)
Plum load_plum(str username, str password, HomeAssistant hass)
Definition: utils.py:9