Raise ConfigEntryAuthFailed during setup or coordinator update to start reauth (#48962)
This commit is contained in:
parent
7cc857a298
commit
4cd7f9bd8b
51 changed files with 534 additions and 517 deletions
|
@ -11,9 +11,10 @@ import urllib.error
|
|||
import aiohttp
|
||||
import requests
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import entity, event
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
|
@ -149,7 +150,7 @@ class DataUpdateCoordinator(Generic[T]):
|
|||
fails. Additionally logging is handled by config entry setup
|
||||
to ensure that multiple retries do not cause log spam.
|
||||
"""
|
||||
await self._async_refresh(log_failures=False)
|
||||
await self._async_refresh(log_failures=False, raise_on_auth_failed=True)
|
||||
if self.last_update_success:
|
||||
return
|
||||
ex = ConfigEntryNotReady()
|
||||
|
@ -160,7 +161,9 @@ class DataUpdateCoordinator(Generic[T]):
|
|||
"""Refresh data and log errors."""
|
||||
await self._async_refresh(log_failures=True)
|
||||
|
||||
async def _async_refresh(self, log_failures: bool = True) -> None:
|
||||
async def _async_refresh(
|
||||
self, log_failures: bool = True, raise_on_auth_failed: bool = False
|
||||
) -> None:
|
||||
"""Refresh data."""
|
||||
if self._unsub_refresh:
|
||||
self._unsub_refresh()
|
||||
|
@ -168,6 +171,7 @@ class DataUpdateCoordinator(Generic[T]):
|
|||
|
||||
self._debounced_refresh.async_cancel()
|
||||
start = monotonic()
|
||||
auth_failed = False
|
||||
|
||||
try:
|
||||
self.data = await self._async_update_data()
|
||||
|
@ -205,6 +209,23 @@ class DataUpdateCoordinator(Generic[T]):
|
|||
self.logger.error("Error fetching %s data: %s", self.name, err)
|
||||
self.last_update_success = False
|
||||
|
||||
except ConfigEntryAuthFailed as err:
|
||||
auth_failed = True
|
||||
self.last_exception = err
|
||||
if self.last_update_success:
|
||||
if log_failures:
|
||||
self.logger.error(
|
||||
"Authentication failed while fetching %s data: %s",
|
||||
self.name,
|
||||
err,
|
||||
)
|
||||
self.last_update_success = False
|
||||
if raise_on_auth_failed:
|
||||
raise
|
||||
|
||||
config_entry = config_entries.current_entry.get()
|
||||
if config_entry:
|
||||
config_entry.async_start_reauth(self.hass)
|
||||
except NotImplementedError as err:
|
||||
self.last_exception = err
|
||||
raise err
|
||||
|
@ -228,7 +249,7 @@ class DataUpdateCoordinator(Generic[T]):
|
|||
self.name,
|
||||
monotonic() - start,
|
||||
)
|
||||
if self._listeners:
|
||||
if not auth_failed and self._listeners:
|
||||
self._schedule_refresh()
|
||||
|
||||
for update_callback in self._listeners:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue