Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for SimpleFIN integration."""
2 
3 from typing import Any
4 
5 from simplefin4py import SimpleFin
6 from simplefin4py.exceptions import (
7  SimpleFinAuthError,
8  SimpleFinClaimError,
9  SimpleFinInvalidAccountURLError,
10  SimpleFinInvalidClaimTokenError,
11  SimpleFinPaymentRequiredError,
12 )
13 import voluptuous as vol
14 
15 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
16 
17 from .const import CONF_ACCESS_URL, DOMAIN, LOGGER
18 
19 
20 class SimpleFinConfigFlow(ConfigFlow, domain=DOMAIN):
21  """Handle a config flow for the initial setup of a SimpleFIN integration."""
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Prompt user for SimpleFIN API credentials."""
27  errors: dict[str, str] = {}
28 
29  if user_input is not None:
30  access_url: str = user_input[CONF_ACCESS_URL]
31  self._async_abort_entries_match_async_abort_entries_match({CONF_ACCESS_URL: access_url})
32 
33  try:
34  if not access_url.startswith("http"):
35  # Claim token detected - convert to access url
36  LOGGER.debug("[Setup Token] - Claiming Access URL")
37  access_url = await SimpleFin.claim_setup_token(access_url)
38  else:
39  LOGGER.debug("[Access Url] - 'http' string detected")
40  # Validate the access URL
41  LOGGER.debug("[Access Url] - validating access url")
42  SimpleFin.decode_access_url(access_url)
43 
44  LOGGER.debug("[Access Url] - Fetching data")
45  simple_fin = SimpleFin(access_url=access_url)
46  await simple_fin.fetch_data()
47 
48  except SimpleFinInvalidAccountURLError:
49  errors["base"] = "url_error"
50  except SimpleFinInvalidClaimTokenError:
51  errors["base"] = "invalid_claim_token"
52  except SimpleFinClaimError:
53  errors["base"] = "claim_error"
54  except SimpleFinPaymentRequiredError:
55  errors["base"] = "payment_required"
56  except SimpleFinAuthError:
57  errors["base"] = "invalid_auth"
58  else:
59  # We passed validation
60  user_input[CONF_ACCESS_URL] = access_url
61 
62  return self.async_create_entryasync_create_entryasync_create_entry(
63  title="SimpleFIN",
64  data={CONF_ACCESS_URL: user_input[CONF_ACCESS_URL]},
65  )
66 
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="user",
69  data_schema=vol.Schema(
70  {
71  vol.Required(CONF_ACCESS_URL): str,
72  }
73  ),
74  errors=errors,
75  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:25
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)
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)