Skip to content

Commit 6ee71da

Browse files
authored
Improved error handling for oauth2 configuration in smartthings integration (home-assistant#156203)
1 parent 9605921 commit 6ee71da

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

homeassistant/components/smartthings/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from homeassistant.helpers import device_registry as dr, entity_registry as er
4747
from homeassistant.helpers.aiohttp_client import async_get_clientsession
4848
from homeassistant.helpers.config_entry_oauth2_flow import (
49+
ImplementationUnavailableError,
4950
OAuth2Session,
5051
async_get_config_entry_implementation,
5152
)
@@ -115,7 +116,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmartThingsConfigEntry)
115116
# after migration but still require reauthentication
116117
if CONF_TOKEN not in entry.data:
117118
raise ConfigEntryAuthFailed("Config entry missing token")
118-
implementation = await async_get_config_entry_implementation(hass, entry)
119+
try:
120+
implementation = await async_get_config_entry_implementation(hass, entry)
121+
except ImplementationUnavailableError as err:
122+
raise ConfigEntryNotReady(
123+
translation_domain=DOMAIN,
124+
translation_key="oauth2_implementation_unavailable",
125+
) from err
119126
session = OAuth2Session(hass, entry, implementation)
120127

121128
try:

homeassistant/components/smartthings/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@
661661
}
662662
}
663663
},
664+
"exceptions": {
665+
"oauth2_implementation_unavailable": {
666+
"message": "OAuth2 implementation unavailable, will retry"
667+
}
668+
},
664669
"issues": {
665670
"deprecated_binary_fridge_door": {
666671
"description": "The refrigerator door binary sensor {entity_name} (`{entity_id}`) is deprecated and will be removed in the future. Separate entities for cooler and freezer door are available and should be used going forward. Please update your dashboards, templates accordingly and disable the entity to fix this issue.",

tests/components/smartthings/test_init.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
3636
from homeassistant.core import Event, HomeAssistant
3737
from homeassistant.helpers import device_registry as dr, entity_registry as er
38+
from homeassistant.helpers.config_entry_oauth2_flow import (
39+
ImplementationUnavailableError,
40+
)
3841

3942
from . import setup_integration, trigger_update
4043

@@ -730,3 +733,20 @@ async def test_entity_unique_id_migration_machine_state(
730733
entry = entity_registry.async_get(entry.entity_id)
731734

732735
assert entry.unique_id == new_unique_id
736+
737+
738+
async def test_oauth_implementation_not_available(
739+
hass: HomeAssistant,
740+
mock_config_entry: MockConfigEntry,
741+
) -> None:
742+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
743+
mock_config_entry.add_to_hass(hass)
744+
745+
with patch(
746+
"homeassistant.components.smartthings.async_get_config_entry_implementation",
747+
side_effect=ImplementationUnavailableError,
748+
):
749+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
750+
await hass.async_block_till_done()
751+
752+
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)