Use reauth helpers in google_mail (#128584)

This commit is contained in:
epenet 2024-10-18 09:04:55 +02:00 committed by GitHub
parent c1c0a281cf
commit 5674c1d82f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 17 deletions

View File

@ -9,11 +9,10 @@ from typing import Any, cast
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN
from homeassistant.helpers import config_entry_oauth2_flow
from . import GoogleMailConfigEntry
from .const import DEFAULT_ACCESS, DOMAIN
@ -24,8 +23,6 @@ class OAuth2FlowHandler(
DOMAIN = DOMAIN
reauth_entry: GoogleMailConfigEntry | None = None
@property
def logger(self) -> logging.Logger:
"""Return logger."""
@ -45,9 +42,6 @@ class OAuth2FlowHandler(
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(
@ -69,18 +63,15 @@ class OAuth2FlowHandler(
credentials = Credentials(data[CONF_TOKEN][CONF_ACCESS_TOKEN])
email = await self.hass.async_add_executor_job(_get_profile)
if not self.reauth_entry:
await self.async_set_unique_id(email)
await self.async_set_unique_id(email)
if self.source != SOURCE_REAUTH:
self._abort_if_unique_id_configured()
return self.async_create_entry(title=email, data=data)
if self.reauth_entry.unique_id == email:
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")
return self.async_abort(
reauth_entry = self._get_reauth_entry()
self._abort_if_unique_id_mismatch(
reason="wrong_account",
description_placeholders={"email": cast(str, self.reauth_entry.unique_id)},
description_placeholders={"email": cast(str, reauth_entry.unique_id)},
)
return self.async_update_reload_and_abort(reauth_entry, data=data)

View File

@ -2447,6 +2447,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
self,
*,
reason: str = "unique_id_mismatch",
description_placeholders: Mapping[str, str] | None = None,
) -> None:
"""Abort if the unique ID does not match the reauth/reconfigure context.
@ -2460,7 +2461,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
self.source == SOURCE_RECONFIGURE
and self._get_reconfigure_entry().unique_id != self.unique_id
):
raise data_entry_flow.AbortFlow(reason)
raise data_entry_flow.AbortFlow(reason, description_placeholders)
@callback
def _abort_if_unique_id_configured(