Skip to content

Commit aedd48c

Browse files
authored
Improved error handling for oauth2 configuration in toon integration (home-assistant#156218)
1 parent febbb85 commit aedd48c

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

homeassistant/components/toon/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
Platform,
1212
)
1313
from homeassistant.core import CoreState, HomeAssistant
14+
from homeassistant.exceptions import ConfigEntryNotReady
1415
from homeassistant.helpers import config_validation as cv, device_registry as dr
1516
from homeassistant.helpers.config_entry_oauth2_flow import (
17+
ImplementationUnavailableError,
1618
OAuth2Session,
1719
async_get_config_entry_implementation,
1820
)
@@ -86,7 +88,13 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
8688

8789
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
8890
"""Set up Toon from a config entry."""
89-
implementation = await async_get_config_entry_implementation(hass, entry)
91+
try:
92+
implementation = await async_get_config_entry_implementation(hass, entry)
93+
except ImplementationUnavailableError as err:
94+
raise ConfigEntryNotReady(
95+
translation_domain=DOMAIN,
96+
translation_key="oauth2_implementation_unavailable",
97+
) from err
9098
session = OAuth2Session(hass, entry, implementation)
9199

92100
coordinator = ToonDataUpdateCoordinator(hass, entry, session)

homeassistant/components/toon/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
}
3333
}
3434
},
35+
"exceptions": {
36+
"oauth2_implementation_unavailable": {
37+
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
38+
}
39+
},
3540
"services": {
3641
"update": {
3742
"description": "Updates all entities with fresh data from Toon.",

tests/components/toon/test_init.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Tests for the Toon component."""
2+
3+
from unittest.mock import patch
4+
5+
from homeassistant.components.toon import DOMAIN
6+
from homeassistant.config_entries import ConfigEntryState
7+
from homeassistant.core import HomeAssistant
8+
from homeassistant.helpers.config_entry_oauth2_flow import (
9+
ImplementationUnavailableError,
10+
)
11+
12+
from tests.common import MockConfigEntry
13+
14+
15+
async def test_oauth_implementation_not_available(
16+
hass: HomeAssistant,
17+
) -> None:
18+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
19+
config_entry = MockConfigEntry(
20+
domain=DOMAIN,
21+
version=2,
22+
data={
23+
"auth_implementation": DOMAIN,
24+
"token": {
25+
"refresh_token": "mock-refresh-token",
26+
"access_token": "mock-access-token",
27+
"type": "Bearer",
28+
"expires_in": 60,
29+
},
30+
"agreement_id": "test-agreement-id",
31+
},
32+
)
33+
config_entry.add_to_hass(hass)
34+
35+
with patch(
36+
"homeassistant.components.toon.async_get_config_entry_implementation",
37+
side_effect=ImplementationUnavailableError,
38+
):
39+
await hass.config_entries.async_setup(config_entry.entry_id)
40+
await hass.async_block_till_done()
41+
42+
assert config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)