1 """Config flow to add the integration via the UI."""
3 from collections.abc
import Mapping
7 from aioautomower.session
import AutomowerSession
8 from aioautomower.utils
import structure_token
15 from .api
import AsyncConfigFlowAuth
16 from .const
import DOMAIN, NAME
18 _LOGGER = logging.getLogger(__name__)
20 CONF_USER_ID =
"user_id"
21 HUSQVARNA_DEV_PORTAL_URL =
"https://developer.husqvarnagroup.cloud/applications"
25 config_entry_oauth2_flow.AbstractOAuth2FlowHandler,
28 """Handle a config flow."""
34 """Create an entry for the flow."""
35 token = data[CONF_TOKEN]
36 if "amc:api" not in token[
"scope"]
and self.
sourcesource != SOURCE_REAUTH:
37 return self.async_abort(reason=
"missing_amc_scope")
38 user_id = token[CONF_USER_ID]
39 await self.async_set_unique_id(user_id)
41 if self.
sourcesource == SOURCE_REAUTH:
42 reauth_entry = self._get_reauth_entry()
43 if "amc:api" not in token[
"scope"]:
44 return self.async_update_reload_and_abort(
45 reauth_entry, data=data, reason=
"missing_amc_scope"
47 self._abort_if_unique_id_mismatch(reason=
"wrong_account")
48 return self.async_update_reload_and_abort(reauth_entry, data=data)
50 self._abort_if_unique_id_configured()
52 websession = aiohttp_client.async_get_clientsession(self.hass)
53 tz = await dt_util.async_get_time_zone(
str(dt_util.DEFAULT_TIME_ZONE))
56 data = await automower_api.get_status()
58 return self.async_abort(reason=
"unknown")
60 return self.async_abort(reason=
"no_mower_connected")
62 structured_token = structure_token(token[CONF_ACCESS_TOKEN])
63 first_name = structured_token.user.first_name
64 last_name = structured_token.user.last_name
66 return self.async_create_entry(
67 title=f
"{NAME} of {first_name} {last_name}",
74 return logging.getLogger(__name__)
77 self, entry_data: Mapping[str, Any]
78 ) -> ConfigFlowResult:
79 """Perform reauth upon an API authentication error."""
80 if "amc:api" not in entry_data[
"token"][
"scope"]:
85 self, user_input: dict[str, Any] |
None =
None
86 ) -> ConfigFlowResult:
87 """Confirm reauth dialog."""
88 if user_input
is None:
89 return self.async_show_form(
90 step_id=
"reauth_confirm",
91 description_placeholders={CONF_NAME: self._get_reauth_entry().title},
93 return await self.async_step_user()
96 self, user_input: dict[str, Any] |
None =
None
97 ) -> ConfigFlowResult:
98 """Confirm reauth for missing scope."""
99 if user_input
is None and self.
sourcesource == SOURCE_REAUTH:
100 token_structured = structure_token(
101 self._get_reauth_entry().data[
"token"][
"access_token"]
103 return self.async_show_form(
104 step_id=
"missing_scope",
105 description_placeholders={
106 "application_url": f
"{HUSQVARNA_DEV_PORTAL_URL}/{token_structured.client_id}"
109 return await self.async_step_user()
ConfigFlowResult async_step_missing_scope(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
logging.Logger logger(self)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)