Skip to content

Commit 9605921

Browse files
authored
Improved error handling for oauth2 configuration in twitch integration (home-assistant#156214)
1 parent 6f06eb5 commit 9605921

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

homeassistant/components/twitch/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@
1111
from homeassistant.core import HomeAssistant
1212
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
1313
from homeassistant.helpers.config_entry_oauth2_flow import (
14+
ImplementationUnavailableError,
1415
LocalOAuth2Implementation,
1516
OAuth2Session,
1617
async_get_config_entry_implementation,
1718
)
1819

19-
from .const import OAUTH_SCOPES, PLATFORMS
20+
from .const import DOMAIN, OAUTH_SCOPES, PLATFORMS
2021
from .coordinator import TwitchConfigEntry, TwitchCoordinator
2122

2223

2324
async def async_setup_entry(hass: HomeAssistant, entry: TwitchConfigEntry) -> bool:
2425
"""Set up Twitch from a config entry."""
25-
implementation = cast(
26-
LocalOAuth2Implementation,
27-
await async_get_config_entry_implementation(hass, entry),
28-
)
26+
try:
27+
implementation = cast(
28+
LocalOAuth2Implementation,
29+
await async_get_config_entry_implementation(hass, entry),
30+
)
31+
except ImplementationUnavailableError as err:
32+
raise ConfigEntryNotReady(
33+
translation_domain=DOMAIN,
34+
translation_key="oauth2_implementation_unavailable",
35+
) from err
2936
session = OAuth2Session(hass, entry, implementation)
3037
try:
3138
await session.async_ensure_token_valid()

homeassistant/components/twitch/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
}
5959
}
6060
}
61+
},
62+
"exceptions": {
63+
"oauth2_implementation_unavailable": {
64+
"message": "OAuth2 implementation unavailable, will retry"
65+
}
6166
}
6267
}

tests/components/twitch/test_init.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from homeassistant.components.twitch.const import DOMAIN, OAUTH2_TOKEN
1111
from homeassistant.config_entries import ConfigEntryState
1212
from homeassistant.core import HomeAssistant
13+
from homeassistant.helpers.config_entry_oauth2_flow import (
14+
ImplementationUnavailableError,
15+
)
1316

1417
from . import setup_integration
1518

@@ -120,3 +123,20 @@ async def test_expired_token_refresh_client_error(
120123
# Verify a transient failure has occurred
121124
entries = hass.config_entries.async_entries(DOMAIN)
122125
assert entries[0].state is ConfigEntryState.SETUP_RETRY
126+
127+
128+
async def test_oauth_implementation_not_available(
129+
hass: HomeAssistant,
130+
config_entry: MockConfigEntry,
131+
) -> None:
132+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
133+
config_entry.add_to_hass(hass)
134+
135+
with patch(
136+
"homeassistant.components.twitch.async_get_config_entry_implementation",
137+
side_effect=ImplementationUnavailableError,
138+
):
139+
await hass.config_entries.async_setup(config_entry.entry_id)
140+
await hass.async_block_till_done()
141+
142+
assert config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)