1 """Config flow for fitbit."""
3 from collections.abc
import Mapping
12 from .const
import DOMAIN, OAUTH_SCOPES
13 from .exceptions
import FitbitApiException, FitbitAuthException
15 _LOGGER = logging.getLogger(__name__)
19 config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN
21 """Config flow to handle fitbit OAuth2 authentication."""
28 return logging.getLogger(__name__)
32 """Extra data that needs to be appended to the authorize url."""
34 "scope":
" ".join(OAUTH_SCOPES),
35 "prompt":
"consent" if self.
sourcesource != SOURCE_REAUTH
else "none",
39 self, entry_data: Mapping[str, Any]
40 ) -> ConfigFlowResult:
41 """Perform reauth upon an API authentication error."""
45 self, user_input: dict[str, Any] |
None =
None
46 ) -> ConfigFlowResult:
47 """Confirm reauth dialog."""
48 if user_input
is None:
49 return self.async_show_form(step_id=
"reauth_confirm")
50 return await self.async_step_user()
53 self, user_input: dict[str, Any] |
None =
None
54 ) -> ConfigFlowResult:
55 """Create config entry from external data with Fitbit specific error handling."""
58 except FitbitAuthException
as err:
60 "Failed to authenticate when creating Fitbit credentials: %s", err
62 return self.async_abort(reason=
"invalid_auth")
63 except FitbitApiException
as err:
64 _LOGGER.error(
"Failed to create Fitbit credentials: %s", err)
65 return self.async_abort(reason=
"cannot_connect")
68 """Create an entry for the flow, or update existing entry."""
72 profile = await client.async_get_user_profile()
73 except FitbitAuthException
as err:
74 _LOGGER.error(
"Failed to authenticate with Fitbit API: %s", err)
75 return self.async_abort(reason=
"invalid_access_token")
76 except FitbitApiException
as err:
77 _LOGGER.error(
"Failed to fetch user profile for Fitbit API: %s", err)
78 return self.async_abort(reason=
"cannot_connect")
80 await self.async_set_unique_id(profile.encoded_id)
81 if self.
sourcesource == SOURCE_REAUTH:
82 self._abort_if_unique_id_mismatch(reason=
"wrong_account")
83 return self.async_update_reload_and_abort(
84 self._get_reauth_entry(), data=data
87 self._abort_if_unique_id_configured()
88 return self.async_create_entry(title=profile.display_name, data=data)
logging.Logger logger(self)
ConfigFlowResult async_step_creation(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_oauth_create_entry(self, dict[str, Any] data)
dict[str, str] extra_authorize_data(self)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)