hass-core/homeassistant/components/simplefin/config_flow.py
Jeef 0213f1c5c0
Add SimpleFIN integration (#108336)
* reset to latest dev branch

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* formatting tweak

* Removed info errors

* fix bad billing error message

* addressing PR

* addressing PR

* reauth abort and already_confiugred added to strings.json

* adding the reauth message

* ruff

* update reqs

* reset to latest dev branch

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* formatting tweak

* Removed info errors

* fix bad billing error message

* addressing PR

* addressing PR

* reauth abort and already_confiugred added to strings.json

* adding the reauth message

* ruff

* update reqs

* Update homeassistant/components/simplefin/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Addressing a few PR comments - removing nix - and adding runtime data

* updated comments

* rename config flow

* pulling reauth :( - inline stuff

* inline more

* fixed a tab issue

* reverting changes

* various PR updates and code removal

* generator async add

* Update homeassistant/components/simplefin/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* always callable

* Update homeassistant/components/simplefin/coordinator.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* no-verify

* type

* fixing missing domain

* it looks like this file is gone now

* typing

* sorta pass

* fix license

* Update homeassistant/components/simplefin/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/entity.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* addressing PR

* Update homeassistant/components/simplefin/strings.json

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* move property to entity.py

* moved stuff out to else block

* Initial Snappshot Testing ... still have unadressed changes to make

* Addressing PR Comments

* pushing back to joost

* removing non-needed file

* added more asserts

* reducing mocks - need to fix patch paths still

* Changed patch to be more localized to config_flow

* Removed unneeded fixture

* Moved coordinator around

* Cleaning up the code

* Removed a NOQA"

* Upping the number of asserts

* cleanup

* fixed abort call

* incremental update - for Josot... changed a function signature and removed an annotatoin

* no-verify

* Added an abort test

* ruff

* increased coverage but it might not pass muster for JOOST

* increased coverage but it might not pass muster for JOOST

* Much nicer test now

* tried to simplify

* Fix nits

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-07-10 12:44:04 +02:00

75 lines
2.7 KiB
Python

"""Config flow for SimpleFIN integration."""
from typing import Any
from simplefin4py import SimpleFin
from simplefin4py.exceptions import (
SimpleFinAuthError,
SimpleFinClaimError,
SimpleFinInvalidAccountURLError,
SimpleFinInvalidClaimTokenError,
SimpleFinPaymentRequiredError,
)
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from .const import CONF_ACCESS_URL, DOMAIN, LOGGER
class SimpleFinConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for the initial setup of a SimpleFIN integration."""
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Prompt user for SimpleFIN API credentials."""
errors: dict[str, str] = {}
if user_input is not None:
access_url: str = user_input[CONF_ACCESS_URL]
self._async_abort_entries_match({CONF_ACCESS_URL: access_url})
try:
if not access_url.startswith("http"):
# Claim token detected - convert to access url
LOGGER.debug("[Setup Token] - Claiming Access URL")
access_url = await SimpleFin.claim_setup_token(access_url)
else:
LOGGER.debug("[Access Url] - 'http' string detected")
# Validate the access URL
LOGGER.debug("[Access Url] - validating access url")
SimpleFin.decode_access_url(access_url)
LOGGER.debug("[Access Url] - Fetching data")
simple_fin = SimpleFin(access_url=access_url)
await simple_fin.fetch_data()
except SimpleFinInvalidAccountURLError:
errors["base"] = "url_error"
except SimpleFinInvalidClaimTokenError:
errors["base"] = "invalid_claim_token"
except SimpleFinClaimError:
errors["base"] = "claim_error"
except SimpleFinPaymentRequiredError:
errors["base"] = "payment_required"
except SimpleFinAuthError:
errors["base"] = "invalid_auth"
else:
# We passed validation
user_input[CONF_ACCESS_URL] = access_url
return self.async_create_entry(
title="SimpleFIN",
data={CONF_ACCESS_URL: user_input[CONF_ACCESS_URL]},
)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_ACCESS_URL): str,
}
),
errors=errors,
)