Implement myuplink OAuth2 ImplementationUnavailableError (#155872)

This commit is contained in:
Åke Strandberg
2025-11-08 15:16:58 +01:00
committed by GitHub
parent 890894b3ae
commit dfb0ea4202
3 changed files with 41 additions and 12 deletions

View File

@@ -12,10 +12,12 @@ from myuplink import MyUplinkAPI, get_manufacturer, get_model, get_system_name
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import (
aiohttp_client,
config_entry_oauth2_flow,
device_registry as dr,
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.config_entry_oauth2_flow import (
ImplementationUnavailableError,
OAuth2Session,
async_get_config_entry_implementation,
)
from homeassistant.helpers.device_registry import DeviceEntry
@@ -40,13 +42,16 @@ async def async_setup_entry(
) -> bool:
"""Set up myUplink from a config entry."""
implementation = (
await config_entry_oauth2_flow.async_get_config_entry_implementation(
hass, config_entry
)
)
session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, implementation)
auth = AsyncConfigEntryAuth(aiohttp_client.async_get_clientsession(hass), session)
try:
implementation = await async_get_config_entry_implementation(hass, config_entry)
except ImplementationUnavailableError as err:
raise ConfigEntryNotReady(
translation_domain=DOMAIN,
translation_key="implementation_unavailable",
) from err
session = OAuth2Session(hass, config_entry, implementation)
auth = AsyncConfigEntryAuth(async_get_clientsession(hass), session)
try:
await auth.async_get_access_token()

View File

@@ -56,6 +56,9 @@
"config_entry_not_ready": {
"message": "Error while loading the integration."
},
"implementation_unavailable": {
"message": "OAuth2 implementation is not available, will retry."
},
"incorrect_oauth2_scope": {
"message": "Stored permissions are invalid. Please login again to update permissions."
},

View File

@@ -2,7 +2,7 @@
import http
import time
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from aiohttp import ClientConnectionError
import pytest
@@ -12,6 +12,9 @@ from homeassistant.components.myuplink.const import DOMAIN, OAUTH2_TOKEN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.config_entry_oauth2_flow import (
ImplementationUnavailableError,
)
from homeassistant.setup import async_setup_component
from . import setup_integration
@@ -39,6 +42,24 @@ async def test_load_unload_entry(
assert entry.state is ConfigEntryState.NOT_LOADED
async def test_oauth2_implementation_unavailable(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test OAuth2 implementation unavailable error."""
mock_config_entry.add_to_hass(hass)
with patch(
f"homeassistant.components.{DOMAIN}.async_get_config_entry_implementation",
side_effect=ImplementationUnavailableError,
):
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
@pytest.mark.parametrize(
("expires_at", "status", "expected_state"),
[