Skip to content

Commit 4fc125c

Browse files
authored
Improve Nest error message wording in test before setup (home-assistant#157465)
1 parent 8c59196 commit 4fc125c

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

homeassistant/components/nest/__init__.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
ConfigurationException,
2020
DecodeException,
2121
SubscriberException,
22+
SubscriberTimeoutException,
2223
)
2324
from google_nest_sdm.traits import TraitType
2425
import voluptuous as vol
@@ -203,10 +204,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: NestConfigEntry) -> bool
203204
await auth.async_get_access_token()
204205
except ClientResponseError as err:
205206
if 400 <= err.status < 500:
206-
raise ConfigEntryAuthFailed from err
207-
raise ConfigEntryNotReady from err
207+
raise ConfigEntryAuthFailed(
208+
translation_domain=DOMAIN, translation_key="reauth_required"
209+
) from err
210+
raise ConfigEntryNotReady(
211+
translation_domain=DOMAIN, translation_key="auth_server_error"
212+
) from err
208213
except ClientError as err:
209-
raise ConfigEntryNotReady from err
214+
raise ConfigEntryNotReady(
215+
translation_domain=DOMAIN, translation_key="auth_client_error"
216+
) from err
210217

211218
subscriber = await api.new_subscriber(hass, entry, auth)
212219
if not subscriber:
@@ -227,19 +234,32 @@ async def async_config_reload() -> None:
227234
unsub = await subscriber.start_async()
228235
except AuthException as err:
229236
raise ConfigEntryAuthFailed(
230-
f"Subscriber authentication error: {err!s}"
237+
translation_domain=DOMAIN,
238+
translation_key="reauth_required",
231239
) from err
232240
except ConfigurationException as err:
233241
_LOGGER.error("Configuration error: %s", err)
234242
return False
243+
except SubscriberTimeoutException as err:
244+
raise ConfigEntryNotReady(
245+
translation_domain=DOMAIN,
246+
translation_key="subscriber_timeout",
247+
) from err
235248
except SubscriberException as err:
236-
raise ConfigEntryNotReady(f"Subscriber error: {err!s}") from err
249+
_LOGGER.error("Subscriber error: %s", err)
250+
raise ConfigEntryNotReady(
251+
translation_domain=DOMAIN,
252+
translation_key="subscriber_error",
253+
) from err
237254

238255
try:
239256
device_manager = await subscriber.async_get_device_manager()
240257
except ApiException as err:
241258
unsub()
242-
raise ConfigEntryNotReady(f"Device manager error: {err!s}") from err
259+
raise ConfigEntryNotReady(
260+
translation_domain=DOMAIN,
261+
translation_key="device_api_error",
262+
) from err
243263

244264
@callback
245265
def on_hass_stop(_: Event) -> None:

homeassistant/components/nest/quality_scale.yaml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ rules:
2323
entity-unique-id: done
2424
docs-installation-instructions: done
2525
docs-removal-instructions: todo
26-
test-before-setup:
27-
status: todo
28-
comment: |
29-
The integration does tests on setup, however the most common issues
30-
observed are related to ipv6 misconfigurations and the error messages
31-
are not self explanatory and can be improved.
26+
test-before-setup: done
3227
docs-high-level-description: done
3328
config-flow-test-coverage: done
3429
docs-actions: done

homeassistant/components/nest/strings.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@
131131
}
132132
}
133133
},
134+
"exceptions": {
135+
"auth_client_error": {
136+
"message": "Client error during authentication, please check your network connection."
137+
},
138+
"auth_server_error": {
139+
"message": "Error response from authentication server, please see logs for details."
140+
},
141+
"device_api_error": {
142+
"message": "Error communicating with the Device Access API, please see logs for details."
143+
},
144+
"reauth_required": {
145+
"message": "Reauthentication is required, please follow the instructions in the UI to reauthenticate your account."
146+
},
147+
"subscriber_error": {
148+
"message": "Subscriber failed to connect to Google, please see logs for details."
149+
},
150+
"subscriber_timeout": {
151+
"message": "Subscriber timed out while attempting to connect to Google. Please check your network connection and IPv6 configuration if applicable."
152+
}
153+
},
134154
"selector": {
135155
"subscription_name": {
136156
"options": {

tests/components/nest/test_init.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
AuthException,
2121
ConfigurationException,
2222
SubscriberException,
23+
SubscriberTimeoutException,
2324
)
2425
import pytest
2526

@@ -110,20 +111,28 @@ async def test_setup_configuration_failure(
110111
assert "Subscription misconfigured. Expected subscriber_id" in caplog.text
111112

112113

113-
@pytest.mark.parametrize("subscriber_side_effect", [SubscriberException()])
114+
@pytest.mark.parametrize(
115+
("subscriber_side_effect", "expected_log_message"),
116+
[
117+
(SubscriberException(), "Subscriber error:"),
118+
(SubscriberTimeoutException(), "Subscriber timed out"),
119+
],
120+
)
114121
async def test_setup_subscriber_failure(
115122
hass: HomeAssistant,
116123
caplog: pytest.LogCaptureFixture,
117124
setup_base_platform,
125+
expected_log_message: str,
118126
) -> None:
119-
"""Test configuration error."""
127+
"""Test subscriber error handling (SubscriberException and SubscriberTimeoutException)."""
120128
await setup_base_platform()
121-
assert "Subscriber error:" in caplog.text
122129

123130
entries = hass.config_entries.async_entries(DOMAIN)
124131
assert len(entries) == 1
125132
assert entries[0].state is ConfigEntryState.SETUP_RETRY
126133

134+
assert expected_log_message in caplog.text
135+
127136

128137
async def test_setup_device_manager_failure(
129138
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, setup_base_platform
@@ -137,7 +146,7 @@ async def test_setup_device_manager_failure(
137146
):
138147
await setup_base_platform()
139148

140-
assert "Device manager error:" in caplog.text
149+
assert "Error communicating with the Device Access API" in caplog.text
141150

142151
entries = hass.config_entries.async_entries(DOMAIN)
143152
assert len(entries) == 1
@@ -211,7 +220,7 @@ async def test_subscriber_auth_failure(
211220
assert flows[0]["step_id"] == "reauth_confirm"
212221

213222

214-
@pytest.mark.parametrize("subscriber_side_effect", [(ConfigurationException())])
223+
@pytest.mark.parametrize("subscriber_side_effect", [ConfigurationException()])
215224
async def test_subscriber_configuration_failure(
216225
hass: HomeAssistant,
217226
error_caplog: pytest.LogCaptureFixture,

0 commit comments

Comments
 (0)