1 """Config flow for Picnic integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from python_picnic_api
import PicnicAPI
10 from python_picnic_api.session
import PicnicAuthError
12 import voluptuous
as vol
24 from .const
import COUNTRY_CODES, DOMAIN
26 _LOGGER = logging.getLogger(__name__)
28 STEP_USER_DATA_SCHEMA = vol.Schema(
30 vol.Required(CONF_USERNAME): str,
31 vol.Required(CONF_PASSWORD): str,
32 vol.Required(CONF_COUNTRY_CODE, default=COUNTRY_CODES[0]): vol.In(
40 """Hub class to test user authentication."""
43 def authenticate(username, password, country_code) -> tuple[str, dict]:
44 """Test if we can authenticate with the Picnic API."""
45 picnic = PicnicAPI(username, password, country_code)
46 return picnic.session.auth_token, picnic.get_user()
50 """Validate the user input allows us to connect.
52 Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
57 auth_token, user_data = await hass.async_add_executor_job(
61 data[CONF_COUNTRY_CODE],
63 except requests.exceptions.ConnectionError
as error:
64 raise CannotConnect
from error
65 except PicnicAuthError
as error:
66 raise InvalidAuth
from error
70 f
'{user_data["address"]["street"]} {user_data["address"]["house_number"]}'
71 f
'{user_data["address"]["house_number_ext"]}'
75 "unique_id": user_data[
"user_id"],
80 """Handle a config flow for Picnic."""
85 self, entry_data: Mapping[str, Any]
86 ) -> ConfigFlowResult:
87 """Perform the re-auth step upon an API authentication error."""
91 self, user_input: dict[str, Any] |
None =
None
92 ) -> ConfigFlowResult:
93 """Handle the authentication step, this is the generic step for both `step_user` and `step_reauth`."""
94 if user_input
is None:
96 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA
103 except CannotConnect:
104 errors[
"base"] =
"cannot_connect"
106 errors[
"base"] =
"invalid_auth"
108 _LOGGER.exception(
"Unexpected exception")
109 errors[
"base"] =
"unknown"
112 CONF_ACCESS_TOKEN: auth_token,
113 CONF_COUNTRY_CODE: user_input[CONF_COUNTRY_CODE],
124 self.hass.config_entries.async_update_entry(existing_entry, data=data)
125 await self.hass.config_entries.async_reload(existing_entry.entry_id)
129 errors[
"base"] =
"different_account"
132 step_id=
"user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
137 """Error to indicate we cannot connect."""
140 class InvalidAuth(HomeAssistantError):
141 """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
tuple[str, dict] authenticate(username, password, country_code)
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_step_user(self, dict[str, Any]|None user_input=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)
def validate_input(HomeAssistant hass, data)