Use reauth helpers in google (#128580)

This commit is contained in:
epenet 2024-10-19 09:55:39 +02:00 committed by GitHub
parent 3ac05f1fa9
commit a94968b6bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,7 +11,12 @@ from gcal_sync.api import GoogleCalendarService
from gcal_sync.exceptions import ApiException, ApiForbiddenException from gcal_sync.exceptions import ApiException, ApiForbiddenException
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlowResult, OptionsFlow from homeassistant.config_entries import (
SOURCE_REAUTH,
ConfigEntry,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import config_entry_oauth2_flow from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -73,7 +78,6 @@ class OAuth2FlowHandler(
def __init__(self) -> None: def __init__(self) -> None:
"""Set up instance.""" """Set up instance."""
super().__init__() super().__init__()
self._reauth_config_entry: ConfigEntry | None = None
self._device_flow: DeviceFlow | None = None self._device_flow: DeviceFlow | None = None
# First attempt is device auth, then fallback to web auth # First attempt is device auth, then fallback to web auth
self._web_auth = False self._web_auth = False
@ -117,10 +121,10 @@ class OAuth2FlowHandler(
) )
return self.async_abort(reason="oauth_error") return self.async_abort(reason="oauth_error")
calendar_access = DEFAULT_FEATURE_ACCESS calendar_access = DEFAULT_FEATURE_ACCESS
if self._reauth_config_entry and self._reauth_config_entry.options: if self.source == SOURCE_REAUTH and (
calendar_access = FeatureAccess[ reauth_options := self._get_reauth_entry().options
self._reauth_config_entry.options[CONF_CALENDAR_ACCESS] ):
] calendar_access = FeatureAccess[reauth_options[CONF_CALENDAR_ACCESS]]
try: try:
device_flow = await async_create_device_flow( device_flow = await async_create_device_flow(
self.hass, self.hass,
@ -177,14 +181,10 @@ class OAuth2FlowHandler(
data[CONF_CREDENTIAL_TYPE] = ( data[CONF_CREDENTIAL_TYPE] = (
CredentialType.WEB_AUTH if self._web_auth else CredentialType.DEVICE_AUTH CredentialType.WEB_AUTH if self._web_auth else CredentialType.DEVICE_AUTH
) )
if self._reauth_config_entry: if self.source == SOURCE_REAUTH:
self.hass.config_entries.async_update_entry( return self.async_update_reload_and_abort(
self._reauth_config_entry, data=data self._get_reauth_entry(), data=data
) )
await self.hass.config_entries.async_reload(
self._reauth_config_entry.entry_id
)
return self.async_abort(reason="reauth_successful")
calendar_service = GoogleCalendarService( calendar_service = GoogleCalendarService(
AccessTokenAuthImpl( AccessTokenAuthImpl(
async_get_clientsession(self.hass), data["token"]["access_token"] async_get_clientsession(self.hass), data["token"]["access_token"]
@ -221,9 +221,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_config_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
self._web_auth = entry_data.get(CONF_CREDENTIAL_TYPE) == CredentialType.WEB_AUTH self._web_auth = entry_data.get(CONF_CREDENTIAL_TYPE) == CredentialType.WEB_AUTH
return await self.async_step_reauth_confirm() return await self.async_step_reauth_confirm()