Skip to content

Commit b44aafc

Browse files
authored
Improved error handling for oauth2 configuration in neato integration (home-assistant#156300)
1 parent af1e320 commit b44aafc

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

homeassistant/components/neato/__init__.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
from homeassistant.const import CONF_TOKEN, Platform
1111
from homeassistant.core import HomeAssistant
1212
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
13-
from homeassistant.helpers import config_entry_oauth2_flow
13+
from homeassistant.helpers.config_entry_oauth2_flow import (
14+
ImplementationUnavailableError,
15+
OAuth2Session,
16+
async_get_config_entry_implementation,
17+
)
1418

1519
from . import api
1620
from .const import NEATO_DOMAIN, NEATO_LOGIN
@@ -33,13 +37,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3337
if CONF_TOKEN not in entry.data:
3438
raise ConfigEntryAuthFailed
3539

36-
implementation = (
37-
await config_entry_oauth2_flow.async_get_config_entry_implementation(
38-
hass, entry
39-
)
40-
)
41-
42-
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
40+
try:
41+
implementation = await async_get_config_entry_implementation(hass, entry)
42+
except ImplementationUnavailableError as err:
43+
raise ConfigEntryNotReady(
44+
translation_domain=NEATO_DOMAIN,
45+
translation_key="oauth2_implementation_unavailable",
46+
) from err
47+
48+
session = OAuth2Session(hass, entry, implementation)
4349
try:
4450
await session.async_ensure_token_valid()
4551
except aiohttp.ClientResponseError as ex:

homeassistant/components/neato/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
}
4747
}
4848
},
49+
"exceptions": {
50+
"oauth2_implementation_unavailable": {
51+
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
52+
}
53+
},
4954
"services": {
5055
"custom_cleaning": {
5156
"description": "Starts a custom cleaning of your house.",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Tests for the Neato component."""
2+
3+
from unittest.mock import patch
4+
5+
from homeassistant.components.neato.const import NEATO_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=NEATO_DOMAIN,
21+
data={
22+
"auth_implementation": NEATO_DOMAIN,
23+
"token": {
24+
"refresh_token": "mock-refresh-token",
25+
"access_token": "mock-access-token",
26+
"type": "Bearer",
27+
"expires_in": 60,
28+
},
29+
},
30+
)
31+
config_entry.add_to_hass(hass)
32+
33+
with patch(
34+
"homeassistant.components.neato.async_get_config_entry_implementation",
35+
side_effect=ImplementationUnavailableError,
36+
):
37+
await hass.config_entries.async_setup(config_entry.entry_id)
38+
await hass.async_block_till_done()
39+
40+
assert config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)