mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use reauth helpers in google_sheets (#128587)
This commit is contained in:
parent
d2eb0e1fde
commit
5a0ef149a5
@ -9,11 +9,10 @@ from typing import Any
|
|||||||
from google.oauth2.credentials import Credentials
|
from google.oauth2.credentials import Credentials
|
||||||
from gspread import Client, GSpreadException
|
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.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 GoogleSheetsConfigEntry
|
|
||||||
from .const import DEFAULT_ACCESS, DEFAULT_NAME, DOMAIN
|
from .const import DEFAULT_ACCESS, DEFAULT_NAME, DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -26,8 +25,6 @@ class OAuth2FlowHandler(
|
|||||||
|
|
||||||
DOMAIN = DOMAIN
|
DOMAIN = DOMAIN
|
||||||
|
|
||||||
reauth_entry: GoogleSheetsConfigEntry | None = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logger(self) -> logging.Logger:
|
def logger(self) -> logging.Logger:
|
||||||
"""Return logger."""
|
"""Return logger."""
|
||||||
@ -47,9 +44,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(
|
||||||
@ -66,24 +60,23 @@ class OAuth2FlowHandler(
|
|||||||
Credentials(data[CONF_TOKEN][CONF_ACCESS_TOKEN]) # type: ignore[no-untyped-call]
|
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")
|
_LOGGER.debug("service.open_by_key")
|
||||||
try:
|
try:
|
||||||
await self.hass.async_add_executor_job(
|
await self.hass.async_add_executor_job(
|
||||||
service.open_by_key,
|
service.open_by_key,
|
||||||
self.reauth_entry.unique_id,
|
reauth_entry.unique_id,
|
||||||
)
|
)
|
||||||
except GSpreadException as err:
|
except GSpreadException as err:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Could not find spreadsheet '%s': %s",
|
"Could not find spreadsheet '%s': %s",
|
||||||
self.reauth_entry.unique_id,
|
reauth_entry.unique_id,
|
||||||
str(err),
|
str(err),
|
||||||
)
|
)
|
||||||
return self.async_abort(reason="open_spreadsheet_failure")
|
return self.async_abort(reason="open_spreadsheet_failure")
|
||||||
|
|
||||||
self.hass.config_entries.async_update_entry(self.reauth_entry, data=data)
|
return self.async_update_reload_and_abort(reauth_entry, data=data)
|
||||||
await self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
doc = await self.hass.async_add_executor_job(
|
doc = await self.hass.async_add_executor_job(
|
||||||
|
@ -235,6 +235,7 @@ async def test_reauth(
|
|||||||
"homeassistant.components.google_sheets.async_setup_entry", return_value=True
|
"homeassistant.components.google_sheets.async_setup_entry", return_value=True
|
||||||
) as mock_setup:
|
) as mock_setup:
|
||||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
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(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||||
assert len(mock_setup.mock_calls) == 1
|
assert len(mock_setup.mock_calls) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user