Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for WeatherflowCloud integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiohttp import ClientResponseError
9 import voluptuous as vol
10 from weatherflow4py.api import WeatherFlowRestAPI
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_API_TOKEN
14 
15 from .const import DOMAIN
16 
17 
18 async def _validate_api_token(api_token: str) -> dict[str, Any]:
19  """Validate the API token."""
20  try:
21  async with WeatherFlowRestAPI(api_token) as api:
22  await api.async_get_stations()
23  except ClientResponseError as err:
24  if err.status == 401:
25  return {"base": "invalid_api_key"}
26  return {"base": "cannot_connect"}
27  return {}
28 
29 
30 class WeatherFlowCloudConfigFlow(ConfigFlow, domain=DOMAIN):
31  """Handle a config flow for WeatherFlowCloud."""
32 
33  VERSION = 1
34 
35  async def async_step_reauth(
36  self, entry_data: Mapping[str, Any]
37  ) -> ConfigFlowResult:
38  """Handle a flow for reauth."""
39  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
40 
42  self, user_input: dict[str, Any] | None = None
43  ) -> ConfigFlowResult:
44  """Handle a flow initiated by reauthentication."""
45  errors = {}
46 
47  if user_input is not None:
48  api_token = user_input[CONF_API_TOKEN]
49  errors = await _validate_api_token(api_token)
50  if not errors:
51  # Update the existing entry and abort
52  return self.async_update_reload_and_abortasync_update_reload_and_abort(
53  self._get_reauth_entry_get_reauth_entry(),
54  data={CONF_API_TOKEN: api_token},
55  reload_even_if_entry_is_unchanged=False,
56  )
57 
58  return self.async_show_formasync_show_formasync_show_form(
59  step_id="reauth_confirm",
60  data_schema=vol.Schema({vol.Required(CONF_API_TOKEN): str}),
61  errors=errors,
62  )
63 
64  async def async_step_user(
65  self, user_input: dict[str, Any] | None = None
66  ) -> ConfigFlowResult:
67  """Handle a flow initialized by the user."""
68  errors = {}
69 
70  if user_input is not None:
71  self._async_abort_entries_match_async_abort_entries_match(user_input)
72  api_token = user_input[CONF_API_TOKEN]
73  errors = await _validate_api_token(api_token)
74  if not errors:
75  return self.async_create_entryasync_create_entryasync_create_entry(
76  title="Weatherflow REST",
77  data={CONF_API_TOKEN: api_token},
78  )
79 
80  return self.async_show_formasync_show_formasync_show_form(
81  step_id="user",
82  data_schema=vol.Schema({vol.Required(CONF_API_TOKEN): str}),
83  errors=errors,
84  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:37
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:66
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:43
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
dict[str, Any] _validate_api_token(str api_token)
Definition: config_flow.py:18