Skip to content

Commit 44e2fa6

Browse files
authored
Improve handling of OAuth2 implementation unavailable in SENZ (home-assistant#156381)
1 parent d0ff617 commit 44e2fa6

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

homeassistant/components/senz/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from homeassistant.const import Platform
1313
from homeassistant.core import HomeAssistant
1414
from homeassistant.exceptions import ConfigEntryNotReady
15-
from homeassistant.helpers import (
16-
config_entry_oauth2_flow,
17-
config_validation as cv,
18-
httpx_client,
15+
from homeassistant.helpers import config_validation as cv, httpx_client
16+
from homeassistant.helpers.config_entry_oauth2_flow import (
17+
ImplementationUnavailableError,
18+
OAuth2Session,
19+
async_get_config_entry_implementation,
1920
)
2021
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
2122

@@ -35,12 +36,14 @@
3536

3637
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3738
"""Set up SENZ from a config entry."""
38-
implementation = (
39-
await config_entry_oauth2_flow.async_get_config_entry_implementation(
40-
hass, entry
41-
)
42-
)
43-
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
39+
try:
40+
implementation = await async_get_config_entry_implementation(hass, entry)
41+
except ImplementationUnavailableError as err:
42+
raise ConfigEntryNotReady(
43+
translation_domain=DOMAIN,
44+
translation_key="oauth2_implementation_unavailable",
45+
) from err
46+
session = OAuth2Session(hass, entry, implementation)
4447
auth = SENZConfigEntryAuth(httpx_client.get_async_client(hass), session)
4548
senz_api = SENZAPI(auth)
4649

homeassistant/components/senz/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@
2525
"title": "[%key:common::config_flow::title::oauth2_pick_implementation%]"
2626
}
2727
}
28+
},
29+
"exceptions": {
30+
"oauth2_implementation_unavailable": {
31+
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
32+
}
2833
}
2934
}

tests/components/senz/test_init.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"""Test init of senz integration."""
22

3-
from unittest.mock import MagicMock
3+
from unittest.mock import MagicMock, patch
44

55
from homeassistant.config_entries import ConfigEntryState
66
from homeassistant.core import HomeAssistant
7+
from homeassistant.helpers.config_entry_oauth2_flow import (
8+
ImplementationUnavailableError,
9+
)
710

811
from . import setup_integration
912

@@ -25,3 +28,18 @@ async def test_load_unload_entry(
2528
await hass.async_block_till_done()
2629

2730
assert entry.state is ConfigEntryState.NOT_LOADED
31+
32+
33+
async def test_oauth_implementation_not_available(
34+
hass: HomeAssistant, mock_config_entry: MockConfigEntry
35+
) -> None:
36+
"""Test that an unavailable OAuth implementation raises ConfigEntryNotReady."""
37+
38+
with patch(
39+
"homeassistant.components.senz.async_get_config_entry_implementation",
40+
side_effect=ImplementationUnavailableError,
41+
):
42+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
43+
await hass.async_block_till_done()
44+
45+
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)