From 5674c1d82f9905648c9354023f74c76475f31f89 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 18 Oct 2024 09:04:55 +0200 Subject: [PATCH] Use reauth helpers in google_mail (#128584) --- .../components/google_mail/config_flow.py | 23 ++++++------------- homeassistant/config_entries.py | 3 ++- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/google_mail/config_flow.py b/homeassistant/components/google_mail/config_flow.py index 5c81f7d49f5..b3a9a0e5d56 100644 --- a/homeassistant/components/google_mail/config_flow.py +++ b/homeassistant/components/google_mail/config_flow.py @@ -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) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index f9c6069295e..c1815df87bf 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -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(