Skip to content

Commit

Permalink
Use reauth helpers in fitbit (#128568)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Oct 17, 2024
1 parent f37c0e0 commit f08d271
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions homeassistant/components/fitbit/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from typing import Any

from homeassistant.config_entries import ConfigEntry, ConfigFlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.const import CONF_TOKEN
from homeassistant.helpers import config_entry_oauth2_flow

Expand All @@ -22,8 +22,6 @@ class OAuth2FlowHandler(

DOMAIN = DOMAIN

reauth_entry: ConfigEntry | None = None

@property
def logger(self) -> logging.Logger:
"""Return logger."""
Expand All @@ -34,16 +32,13 @@ def extra_authorize_data(self) -> dict[str, str]:
"""Extra data that needs to be appended to the authorize url."""
return {
"scope": " ".join(OAUTH_SCOPES),
"prompt": "consent" if not self.reauth_entry else "none",
"prompt": "consent" if self.source != SOURCE_REAUTH else "none",
}

async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Perform reauth upon an API authentication error."""
self.reauth_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(
Expand Down Expand Up @@ -82,14 +77,13 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResu
_LOGGER.error("Failed to fetch user profile for Fitbit API: %s", err)
return self.async_abort(reason="cannot_connect")

if self.reauth_entry:
if self.reauth_entry.unique_id != profile.encoded_id:
return self.async_abort(reason="wrong_account")
self.hass.config_entries.async_update_entry(self.reauth_entry, data=data)
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")

await self.async_set_unique_id(profile.encoded_id)
if self.source == SOURCE_REAUTH:
self._abort_if_unique_id_mismatch(reason="wrong_account")
return self.async_update_reload_and_abort(
self._get_reauth_entry(), data=data
)

self._abort_if_unique_id_configured()
return self.async_create_entry(title=profile.display_name, data=data)

Expand Down

0 comments on commit f08d271

Please sign in to comment.