Skip to content

Commit febbb85

Browse files
authored
Improved error handling for oauth2 configuration in netatmo integration (home-assistant#156207)
1 parent af67a35 commit febbb85

File tree

7 files changed

+52
-27
lines changed

7 files changed

+52
-27
lines changed

homeassistant/components/netatmo/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
from homeassistant.const import CONF_WEBHOOK_ID, EVENT_HOMEASSISTANT_STOP
2121
from homeassistant.core import HomeAssistant
2222
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
23-
from homeassistant.helpers import (
24-
aiohttp_client,
25-
config_entry_oauth2_flow,
26-
config_validation as cv,
23+
from homeassistant.helpers import aiohttp_client, config_validation as cv
24+
from homeassistant.helpers.config_entry_oauth2_flow import (
25+
ImplementationUnavailableError,
26+
OAuth2Session,
27+
async_get_config_entry_implementation,
2728
)
2829
from homeassistant.helpers.device_registry import DeviceEntry
2930
from homeassistant.helpers.dispatcher import async_dispatcher_send
@@ -73,17 +74,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
7374

7475
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
7576
"""Set up Netatmo from a config entry."""
76-
implementation = (
77-
await config_entry_oauth2_flow.async_get_config_entry_implementation(
78-
hass, entry
79-
)
80-
)
77+
try:
78+
implementation = await async_get_config_entry_implementation(hass, entry)
79+
except ImplementationUnavailableError as err:
80+
raise ConfigEntryNotReady(
81+
translation_domain=DOMAIN,
82+
translation_key="oauth2_implementation_unavailable",
83+
) from err
8184

8285
# Set unique id if non was set (migration)
8386
if not entry.unique_id:
8487
hass.config_entries.async_update_entry(entry, unique_id=DOMAIN)
8588

86-
session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation)
89+
session = OAuth2Session(hass, entry, implementation)
8790
try:
8891
await session.async_ensure_token_valid()
8992
except aiohttp.ClientResponseError as ex:

homeassistant/components/netatmo/strings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@
143143
}
144144
}
145145
},
146+
"exceptions": {
147+
"oauth2_implementation_unavailable": {
148+
"message": "[%key:common::exceptions::oauth2_implementation_unavailable::message%]"
149+
}
150+
},
146151
"options": {
147152
"step": {
148153
"public_weather": {

tests/components/netatmo/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def selected_platforms(platforms: list[Platform]) -> Iterator[None]:
119119
with (
120120
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", platforms),
121121
patch(
122-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
122+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
123123
),
124124
patch(
125125
"homeassistant.components.netatmo.webhook_generate_url",

tests/components/netatmo/test_camera.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ async def fake_post(*args: Any, **kwargs: Any):
416416
) as mock_auth,
417417
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", ["camera"]),
418418
patch(
419-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
419+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
420420
),
421421
patch(
422422
"homeassistant.components.netatmo.webhook_generate_url",
@@ -515,7 +515,7 @@ async def fake_post_no_data(*args, **kwargs):
515515
) as mock_auth,
516516
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", ["camera"]),
517517
patch(
518-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
518+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
519519
),
520520
patch(
521521
"homeassistant.components.netatmo.webhook_generate_url",
@@ -558,7 +558,7 @@ async def fake_post(*args: Any, **kwargs: Any):
558558
) as mock_auth,
559559
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", ["camera"]),
560560
patch(
561-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
561+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
562562
),
563563
patch(
564564
"homeassistant.components.netatmo.webhook_generate_url",

tests/components/netatmo/test_diagnostics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async def test_entry_diagnostics(
2828
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth",
2929
) as mock_auth,
3030
patch(
31-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
31+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
3232
),
3333
patch(
3434
"homeassistant.components.netatmo.webhook_generate_url",

tests/components/netatmo/test_init.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from homeassistant.const import CONF_WEBHOOK_ID, Platform
1717
from homeassistant.core import CoreState, HomeAssistant
1818
from homeassistant.helpers import device_registry as dr, entity_registry as er
19+
from homeassistant.helpers.config_entry_oauth2_flow import (
20+
ImplementationUnavailableError,
21+
)
1922
from homeassistant.setup import async_setup_component
2023
from homeassistant.util import dt as dt_util
2124

@@ -65,7 +68,7 @@ async def test_setup_component(
6568
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth",
6669
) as mock_auth,
6770
patch(
68-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
71+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
6972
) as mock_impl,
7073
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
7174
):
@@ -108,7 +111,7 @@ async def fake_post(*args, **kwargs):
108111

109112
with (
110113
patch(
111-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
114+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
112115
) as mock_impl,
113116
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
114117
patch(
@@ -181,7 +184,7 @@ async def test_setup_without_https(
181184
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth"
182185
) as mock_auth,
183186
patch(
184-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
187+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
185188
),
186189
patch(
187190
"homeassistant.components.netatmo.webhook_generate_url"
@@ -225,7 +228,7 @@ async def test_setup_with_cloud(
225228
) as mock_auth,
226229
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", []),
227230
patch(
228-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
231+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
229232
),
230233
patch(
231234
"homeassistant.components.netatmo.webhook_generate_url",
@@ -295,7 +298,7 @@ async def test_setup_with_cloudhook(hass: HomeAssistant) -> None:
295298
) as mock_auth,
296299
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", []),
297300
patch(
298-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
301+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
299302
),
300303
patch(
301304
"homeassistant.components.netatmo.webhook_generate_url",
@@ -340,7 +343,7 @@ async def test_setup_component_with_delay(
340343
"pyatmo.AbstractAsyncAuth.async_dropwebhook", side_effect=AsyncMock()
341344
) as mock_dropwebhook,
342345
patch(
343-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
346+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
344347
) as mock_impl,
345348
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
346349
patch(
@@ -410,7 +413,7 @@ async def test_setup_component_invalid_token_scope(hass: HomeAssistant) -> None:
410413
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth",
411414
) as mock_auth,
412415
patch(
413-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
416+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
414417
) as mock_impl,
415418
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
416419
):
@@ -459,12 +462,10 @@ async def fake_ensure_valid_token(*args, **kwargs):
459462
"homeassistant.components.netatmo.api.AsyncConfigEntryNetatmoAuth",
460463
) as mock_auth,
461464
patch(
462-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
465+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
463466
) as mock_impl,
464467
patch("homeassistant.components.netatmo.webhook_generate_url") as mock_webhook,
465-
patch(
466-
"homeassistant.helpers.config_entry_oauth2_flow.OAuth2Session"
467-
) as mock_session,
468+
patch("homeassistant.components.netatmo.OAuth2Session") as mock_session,
468469
):
469470
mock_auth.return_value.async_post_api_request.side_effect = partial(
470471
fake_post_request, hass
@@ -557,3 +558,19 @@ async def test_device_remove_devices(
557558
)
558559
response = await client.remove_device(dead_device_entry.id, config_entry.entry_id)
559560
assert response["success"]
561+
562+
563+
async def test_oauth_implementation_not_available(
564+
hass: HomeAssistant, config_entry: MockConfigEntry
565+
) -> None:
566+
"""Test that unavailable OAuth implementation raises ConfigEntryNotReady."""
567+
config_entry.add_to_hass(hass)
568+
569+
with patch(
570+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
571+
side_effect=ImplementationUnavailableError,
572+
):
573+
await hass.config_entries.async_setup(config_entry.entry_id)
574+
await hass.async_block_till_done()
575+
576+
assert config_entry.state is ConfigEntryState.SETUP_RETRY

tests/components/netatmo/test_light.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async def fake_post_request_no_data(*args, **kwargs):
129129
) as mock_auth,
130130
patch("homeassistant.components.netatmo.data_handler.PLATFORMS", ["light"]),
131131
patch(
132-
"homeassistant.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
132+
"homeassistant.components.netatmo.async_get_config_entry_implementation",
133133
),
134134
patch(
135135
"homeassistant.components.netatmo.webhook_generate_url",

0 commit comments

Comments
 (0)