Use reauth helpers in google_sheets (#128587)

pull/128590/head^2
epenet 2024-10-18 10:04:03 +02:00 committed by GitHub
parent d2eb0e1fde
commit 5a0ef149a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 13 deletions

View File

@ -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(

View File

@ -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