diff --git a/homeassistant/components/google_sheets/config_flow.py b/homeassistant/components/google_sheets/config_flow.py index 4008d42f52d..81c82bf1bc4 100644 --- a/homeassistant/components/google_sheets/config_flow.py +++ b/homeassistant/components/google_sheets/config_flow.py @@ -9,11 +9,10 @@ from typing import Any from google.oauth2.credentials import Credentials from gspread import Client, GSpreadException -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 GoogleSheetsConfigEntry from .const import DEFAULT_ACCESS, DEFAULT_NAME, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -26,8 +25,6 @@ class OAuth2FlowHandler( DOMAIN = DOMAIN - reauth_entry: GoogleSheetsConfigEntry | None = None - @property def logger(self) -> logging.Logger: """Return logger.""" @@ -47,9 +44,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( @@ -66,24 +60,23 @@ class OAuth2FlowHandler( Credentials(data[CONF_TOKEN][CONF_ACCESS_TOKEN]) # type: ignore[no-untyped-call] ) - if self.reauth_entry: + if self.source == SOURCE_REAUTH: + reauth_entry = self._get_reauth_entry() _LOGGER.debug("service.open_by_key") try: await self.hass.async_add_executor_job( service.open_by_key, - self.reauth_entry.unique_id, + reauth_entry.unique_id, ) except GSpreadException as err: _LOGGER.error( "Could not find spreadsheet '%s': %s", - self.reauth_entry.unique_id, + reauth_entry.unique_id, str(err), ) return self.async_abort(reason="open_spreadsheet_failure") - 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_update_reload_and_abort(reauth_entry, data=data) try: doc = await self.hass.async_add_executor_job( diff --git a/tests/components/google_sheets/test_config_flow.py b/tests/components/google_sheets/test_config_flow.py index a504d8c4280..756ff080212 100644 --- a/tests/components/google_sheets/test_config_flow.py +++ b/tests/components/google_sheets/test_config_flow.py @@ -235,6 +235,7 @@ async def test_reauth( "homeassistant.components.google_sheets.async_setup_entry", return_value=True ) as mock_setup: result = await hass.config_entries.flow.async_configure(result["flow_id"]) + await hass.async_block_till_done() assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert len(mock_setup.mock_calls) == 1