mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 23:07:09 +00:00
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
@ -1,12 +1,15 @@
|
|||||||
"""The myUplink integration."""
|
"""The myUplink integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from http import HTTPStatus
|
||||||
|
|
||||||
|
from aiohttp import ClientError, ClientResponseError
|
||||||
from myuplink import MyUplinkAPI
|
from myuplink import MyUplinkAPI
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
aiohttp_client,
|
aiohttp_client,
|
||||||
config_entry_oauth2_flow,
|
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)
|
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):
|
if set(config_entry.data["token"]["scope"].split(" ")) != set(OAUTH2_SCOPES):
|
||||||
raise ConfigEntryAuthFailed("Incorrect OAuth2 scope")
|
raise ConfigEntryAuthFailed("Incorrect OAuth2 scope")
|
||||||
|
|
||||||
auth = AsyncConfigEntryAuth(aiohttp_client.async_get_clientsession(hass), session)
|
|
||||||
|
|
||||||
# Setup MyUplinkAPI and coordinator for data fetch
|
# Setup MyUplinkAPI and coordinator for data fetch
|
||||||
api = MyUplinkAPI(auth)
|
api = MyUplinkAPI(auth)
|
||||||
coordinator = MyUplinkDataCoordinator(hass, api)
|
coordinator = MyUplinkDataCoordinator(hass, api)
|
||||||
|
@ -38,7 +38,7 @@ async def test_load_unload_entry(
|
|||||||
(
|
(
|
||||||
time.time() - 3600,
|
time.time() - 3600,
|
||||||
http.HTTPStatus.UNAUTHORIZED,
|
http.HTTPStatus.UNAUTHORIZED,
|
||||||
ConfigEntryState.SETUP_RETRY, # Will trigger reauth in the future
|
ConfigEntryState.SETUP_ERROR,
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
time.time() - 3600,
|
time.time() - 3600,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user