mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +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 google.oauth2.credentials import Credentials
|
||||||
from googleapiclient.discovery import build
|
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.const import CONF_ACCESS_TOKEN, CONF_TOKEN
|
||||||
from homeassistant.helpers import config_entry_oauth2_flow
|
from homeassistant.helpers import config_entry_oauth2_flow
|
||||||
|
|
||||||
from . import GoogleMailConfigEntry
|
|
||||||
from .const import DEFAULT_ACCESS, DOMAIN
|
from .const import DEFAULT_ACCESS, DOMAIN
|
||||||
|
|
||||||
|
|
||||||
@ -24,8 +23,6 @@ class OAuth2FlowHandler(
|
|||||||
|
|
||||||
DOMAIN = DOMAIN
|
DOMAIN = DOMAIN
|
||||||
|
|
||||||
reauth_entry: GoogleMailConfigEntry | None = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logger(self) -> logging.Logger:
|
def logger(self) -> logging.Logger:
|
||||||
"""Return logger."""
|
"""Return logger."""
|
||||||
@ -45,9 +42,6 @@ class OAuth2FlowHandler(
|
|||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Perform reauth upon an API authentication error."""
|
"""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()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
async def async_step_reauth_confirm(
|
async def async_step_reauth_confirm(
|
||||||
@ -69,18 +63,15 @@ class OAuth2FlowHandler(
|
|||||||
credentials = Credentials(data[CONF_TOKEN][CONF_ACCESS_TOKEN])
|
credentials = Credentials(data[CONF_TOKEN][CONF_ACCESS_TOKEN])
|
||||||
email = await self.hass.async_add_executor_job(_get_profile)
|
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()
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
return self.async_create_entry(title=email, data=data)
|
return self.async_create_entry(title=email, data=data)
|
||||||
|
|
||||||
if self.reauth_entry.unique_id == email:
|
reauth_entry = self._get_reauth_entry()
|
||||||
self.hass.config_entries.async_update_entry(self.reauth_entry, data=data)
|
self._abort_if_unique_id_mismatch(
|
||||||
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
return self.async_abort(
|
|
||||||
reason="wrong_account",
|
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,
|
self,
|
||||||
*,
|
*,
|
||||||
reason: str = "unique_id_mismatch",
|
reason: str = "unique_id_mismatch",
|
||||||
|
description_placeholders: Mapping[str, str] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Abort if the unique ID does not match the reauth/reconfigure context.
|
"""Abort if the unique ID does not match the reauth/reconfigure context.
|
||||||
|
|
||||||
@ -2460,7 +2461,7 @@ class ConfigFlow(ConfigEntryBaseFlow):
|
|||||||
self.source == SOURCE_RECONFIGURE
|
self.source == SOURCE_RECONFIGURE
|
||||||
and self._get_reconfigure_entry().unique_id != self.unique_id
|
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
|
@callback
|
||||||
def _abort_if_unique_id_configured(
|
def _abort_if_unique_id_configured(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user