Skip to content

Commit 603d4bc

Browse files
authored
Improved error handling for oauth2 configuration in weheat integration (home-assistant#156217)
1 parent 2dadc1f commit 603d4bc

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

homeassistant/components/weheat/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
1515
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1616
from homeassistant.helpers.config_entry_oauth2_flow import (
17+
ImplementationUnavailableError,
1718
OAuth2Session,
1819
async_get_config_entry_implementation,
1920
)
2021

21-
from .const import API_URL, LOGGER
22+
from .const import API_URL, DOMAIN, LOGGER
2223
from .coordinator import (
2324
HeatPumpInfo,
2425
WeheatConfigEntry,
@@ -32,7 +33,13 @@
3233

3334
async def async_setup_entry(hass: HomeAssistant, entry: WeheatConfigEntry) -> bool:
3435
"""Set up Weheat from a config entry."""
35-
implementation = await async_get_config_entry_implementation(hass, entry)
36+
try:
37+
implementation = await async_get_config_entry_implementation(hass, entry)
38+
except ImplementationUnavailableError as err:
39+
raise ConfigEntryNotReady(
40+
translation_domain=DOMAIN,
41+
translation_key="oauth2_implementation_unavailable",
42+
) from err
3643

3744
session = OAuth2Session(hass, entry, implementation)
3845

homeassistant/components/weheat/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,10 @@
124124
"name": "Water outlet temperature"
125125
}
126126
}
127+
},
128+
"exceptions": {
129+
"oauth2_implementation_unavailable": {
130+
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
131+
}
127132
}
128133
}

tests/components/weheat/test_init.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from homeassistant.components.weheat import UnauthorizedException
1010
from homeassistant.config_entries import ConfigEntryState
1111
from homeassistant.core import HomeAssistant
12+
from homeassistant.helpers.config_entry_oauth2_flow import (
13+
ImplementationUnavailableError,
14+
)
1215

1316
from . import setup_integration
1417

@@ -83,3 +86,21 @@ async def test_setup_fail_discover(
8386
await setup_integration(hass, mock_config_entry)
8487

8588
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
89+
90+
91+
@pytest.mark.usefixtures("setup_credentials")
92+
async def test_oauth_implementation_not_available(
93+
hass: HomeAssistant,
94+
mock_config_entry: MockConfigEntry,
95+
) -> None:
96+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
97+
mock_config_entry.add_to_hass(hass)
98+
99+
with patch(
100+
"homeassistant.components.weheat.async_get_config_entry_implementation",
101+
side_effect=ImplementationUnavailableError,
102+
):
103+
await hass.config_entries.async_setup(mock_config_entry.entry_id)
104+
await hass.async_block_till_done()
105+
106+
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY

0 commit comments

Comments
 (0)