Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for JustNimbus integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 import justnimbus
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_CLIENT_ID
14 from homeassistant.helpers import config_validation as cv
15 
16 from .const import CONF_ZIP_CODE, DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 STEP_USER_DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_CLIENT_ID): cv.string,
23  vol.Required(CONF_ZIP_CODE): cv.string,
24  },
25 )
26 
27 
28 class JustNimbusConfigFlow(ConfigFlow, domain=DOMAIN):
29  """Handle a config flow for JustNimbus."""
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  if user_input is None:
38  return self.async_show_formasync_show_formasync_show_form(
39  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
40  )
41 
42  errors = {}
43 
44  unique_id = f"{user_input[CONF_CLIENT_ID]}{user_input[CONF_ZIP_CODE]}"
45  await self.async_set_unique_idasync_set_unique_id(unique_id=unique_id)
46  if self.sourcesourcesource != SOURCE_REAUTH:
47  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
48 
49  client = justnimbus.JustNimbusClient(
50  client_id=user_input[CONF_CLIENT_ID], zip_code=user_input[CONF_ZIP_CODE]
51  )
52  try:
53  await self.hass.async_add_executor_job(client.get_data)
54  except justnimbus.InvalidClientID:
55  errors["base"] = "invalid_auth"
56  except justnimbus.JustNimbusError:
57  errors["base"] = "cannot_connect"
58  except Exception:
59  _LOGGER.exception("Unexpected exception")
60  errors["base"] = "unknown"
61  else:
62  if self.sourcesourcesource != SOURCE_REAUTH:
63  return self.async_create_entryasync_create_entryasync_create_entry(title="JustNimbus", data=user_input)
64  return self.async_update_reload_and_abortasync_update_reload_and_abort(
65  self._get_reauth_entry_get_reauth_entry(), data=user_input, unique_id=unique_id
66  )
67 
68  return self.async_show_formasync_show_formasync_show_form(
69  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
70  )
71 
72  async def async_step_reauth(
73  self, entry_data: Mapping[str, Any]
74  ) -> ConfigFlowResult:
75  """Perform reauth upon an API authentication error."""
76  return await self.async_step_userasync_step_userasync_step_user()
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:35
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:74
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_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)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=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)
str|None source(self)