mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Use reauth helpers in google_mail (#128584)
This commit is contained in:
parent
c1c0a281cf
commit
5674c1d82f
@ -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)
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user