Add check for myuplink startup ClientError (#110926)
* Raise ConfigEntryNotReady if appropriate * Catchin exceptions during startup * Change expected_state to SETUP_ERROR
This commit is contained in:
parent
ae49b3a274
commit
230ac417c0
2 changed files with 15 additions and 4 deletions
|
@ -1,12 +1,15 @@
|
|||
"""The myUplink integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from http import HTTPStatus
|
||||
|
||||
from aiohttp import ClientError, ClientResponseError
|
||||
from myuplink import MyUplinkAPI
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import (
|
||||
aiohttp_client,
|
||||
config_entry_oauth2_flow,
|
||||
|
@ -34,12 +37,20 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
)
|
||||
)
|
||||
session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, implementation)
|
||||
auth = AsyncConfigEntryAuth(aiohttp_client.async_get_clientsession(hass), session)
|
||||
|
||||
try:
|
||||
await auth.async_get_access_token()
|
||||
except ClientResponseError as err:
|
||||
if err.status in {HTTPStatus.UNAUTHORIZED, HTTPStatus.FORBIDDEN}:
|
||||
raise ConfigEntryAuthFailed from err
|
||||
raise ConfigEntryNotReady from err
|
||||
except ClientError as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
if set(config_entry.data["token"]["scope"].split(" ")) != set(OAUTH2_SCOPES):
|
||||
raise ConfigEntryAuthFailed("Incorrect OAuth2 scope")
|
||||
|
||||
auth = AsyncConfigEntryAuth(aiohttp_client.async_get_clientsession(hass), session)
|
||||
|
||||
# Setup MyUplinkAPI and coordinator for data fetch
|
||||
api = MyUplinkAPI(auth)
|
||||
coordinator = MyUplinkDataCoordinator(hass, api)
|
||||
|
|
|
@ -38,7 +38,7 @@ async def test_load_unload_entry(
|
|||
(
|
||||
time.time() - 3600,
|
||||
http.HTTPStatus.UNAUTHORIZED,
|
||||
ConfigEntryState.SETUP_RETRY, # Will trigger reauth in the future
|
||||
ConfigEntryState.SETUP_ERROR,
|
||||
),
|
||||
(
|
||||
time.time() - 3600,
|
||||
|
|
Loading…
Add table
Reference in a new issue