Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for FireServiceRota."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from pyfireservicerota import FireServiceRota, InvalidAuthError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_URL, CONF_USERNAME
13 
14 from .const import DOMAIN, URL_LIST
15 
16 DATA_SCHEMA = vol.Schema(
17  {
18  vol.Required(CONF_URL, default="www.brandweerrooster.nl"): vol.In(URL_LIST),
19  vol.Required(CONF_USERNAME): str,
20  vol.Required(CONF_PASSWORD): str,
21  }
22 )
23 
24 
25 class FireServiceRotaFlowHandler(ConfigFlow, domain=DOMAIN):
26  """Handle a FireServiceRota config flow."""
27 
28  VERSION = 1
29 
30  def __init__(self) -> None:
31  """Initialize config flow."""
32  self.apiapi = None
33  self._base_url_base_url = None
34  self._username_username = None
35  self._password_password = None
36  self._existing_entry_existing_entry: dict[str, Any] | None = None
37  self._description_placeholders_description_placeholders: dict[str, str] | None = None
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle a flow initiated by the user."""
43  errors: dict[str, str] = {}
44 
45  if user_input is None:
46  return self._show_setup_form_show_setup_form(user_input, errors)
47 
48  return await self._validate_and_create_entry_validate_and_create_entry(user_input, "user")
49 
50  async def _validate_and_create_entry(self, user_input, step_id):
51  """Check if config is valid and create entry if so."""
52  self._password_password = user_input[CONF_PASSWORD]
53 
54  extra_inputs = user_input
55 
56  if self._existing_entry_existing_entry:
57  extra_inputs = self._existing_entry_existing_entry
58 
59  self._username_username = extra_inputs[CONF_USERNAME]
60  self._base_url_base_url = extra_inputs[CONF_URL]
61 
62  if self.unique_idunique_id is None:
63  await self.async_set_unique_idasync_set_unique_id(self._username_username)
64  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
65 
66  self.apiapi = FireServiceRota(
67  base_url=self._base_url_base_url,
68  username=self._username_username,
69  password=self._password_password,
70  )
71 
72  try:
73  token_info = await self.hass.async_add_executor_job(self.apiapi.request_tokens)
74  except InvalidAuthError:
75  self.apiapi = None
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id=step_id,
78  data_schema=DATA_SCHEMA,
79  errors={"base": "invalid_auth"},
80  )
81 
82  data = {
83  "auth_implementation": DOMAIN,
84  CONF_URL: self._base_url_base_url,
85  CONF_USERNAME: self._username_username,
86  CONF_TOKEN: token_info,
87  }
88 
89  if step_id == "user":
90  return self.async_create_entryasync_create_entryasync_create_entry(title=self._username_username, data=data)
91 
92  entry = await self.async_set_unique_idasync_set_unique_id(self.unique_idunique_id)
93  self.hass.config_entries.async_update_entry(entry, data=data)
94  await self.hass.config_entries.async_reload(entry.entry_id)
95  return self.async_abortasync_abortasync_abort(reason="reauth_successful")
96 
97  def _show_setup_form(self, user_input=None, errors=None, step_id="user"):
98  """Show the setup form to the user."""
99 
100  if user_input is None:
101  user_input = {}
102 
103  if step_id == "user":
104  schema = {
105  vol.Required(CONF_URL, default="www.brandweerrooster.nl"): vol.In(
106  URL_LIST
107  ),
108  vol.Required(CONF_USERNAME): str,
109  vol.Required(CONF_PASSWORD): str,
110  }
111  else:
112  schema = {vol.Required(CONF_PASSWORD): str}
113 
114  return self.async_show_formasync_show_formasync_show_form(
115  step_id=step_id,
116  data_schema=vol.Schema(schema),
117  errors=errors or {},
118  description_placeholders=self._description_placeholders_description_placeholders,
119  )
120 
121  async def async_step_reauth(
122  self, entry_data: Mapping[str, Any]
123  ) -> ConfigFlowResult:
124  """Initialise re-authentication."""
125  await self.async_set_unique_idasync_set_unique_id(entry_data[CONF_USERNAME])
126  self._existing_entry_existing_entry = {**entry_data}
127  self._description_placeholders_description_placeholders = {CONF_USERNAME: entry_data[CONF_USERNAME]}
128  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
129 
131  self, user_input: dict[str, str] | None = None
132  ) -> ConfigFlowResult:
133  """Get new tokens for a config entry that can't authenticate."""
134  if user_input is None:
135  return self._show_setup_form_show_setup_form(step_id="reauth_confirm")
136 
137  return await self._validate_and_create_entry_validate_and_create_entry(user_input, "reauth_confirm")
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:123
def _show_setup_form(self, user_input=None, errors=None, step_id="user")
Definition: config_flow.py:97
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:132
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)