mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Add translated setup retry reasons to the update coordinator (#194048)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
22b7ece217
commit
e18aaa87d0
@@ -170,6 +170,12 @@
|
||||
},
|
||||
"unload_platforms_failed": {
|
||||
"message": "Failed to unload one or more platforms."
|
||||
},
|
||||
"update_failed": {
|
||||
"message": "Failed to fetch the data"
|
||||
},
|
||||
"update_failed_timeout": {
|
||||
"message": "Timed out while fetching the data"
|
||||
}
|
||||
},
|
||||
"integration_category": {
|
||||
|
||||
@@ -19,6 +19,7 @@ from homeassistant import config_entries
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import (
|
||||
CALLBACK_TYPE,
|
||||
DOMAIN as HOMEASSISTANT_DOMAIN,
|
||||
Event,
|
||||
HassJob,
|
||||
HassJobType,
|
||||
@@ -67,6 +68,26 @@ class UpdateFailedUnreachable(UpdateFailed):
|
||||
"""
|
||||
|
||||
|
||||
def _setup_retry_translation_key(cause: BaseException | None) -> str | None:
|
||||
"""Return the translation key for a setup retry caused by a generic error."""
|
||||
# Timeouts first: some timeouts are also client or request errors
|
||||
if isinstance(cause, (TimeoutError, requests.exceptions.Timeout)) or (
|
||||
isinstance(cause, urllib.error.URLError)
|
||||
and (isinstance(cause.reason, TimeoutError) or cause.reason == "timed out")
|
||||
):
|
||||
return "update_failed_timeout"
|
||||
if isinstance(
|
||||
cause,
|
||||
(
|
||||
aiohttp.ClientError,
|
||||
requests.exceptions.RequestException,
|
||||
urllib.error.URLError,
|
||||
),
|
||||
):
|
||||
return "update_failed"
|
||||
return None
|
||||
|
||||
|
||||
class BaseDataUpdateCoordinatorProtocol(Protocol):
|
||||
"""Base protocol type for DataUpdateCoordinator."""
|
||||
|
||||
@@ -393,9 +414,16 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_DataT]):
|
||||
if self.last_update_success:
|
||||
return
|
||||
cause = self.last_exception
|
||||
translation_domain = getattr(cause, "translation_domain", None)
|
||||
translation_key = getattr(cause, "translation_key", None)
|
||||
# Errors the coordinator handles itself have no translation of their own
|
||||
if translation_key is None and (
|
||||
translation_key := _setup_retry_translation_key(cause)
|
||||
):
|
||||
translation_domain = HOMEASSISTANT_DOMAIN
|
||||
ex = ConfigEntryNotReady(
|
||||
translation_domain=getattr(cause, "translation_domain", None),
|
||||
translation_key=getattr(cause, "translation_key", None),
|
||||
translation_domain=translation_domain,
|
||||
translation_key=translation_key,
|
||||
translation_placeholders=getattr(cause, "translation_placeholders", None),
|
||||
retry_after=getattr(cause, "retry_after", None),
|
||||
)
|
||||
|
||||
@@ -902,6 +902,69 @@ async def test_async_config_entry_first_refresh_not_ready(
|
||||
None,
|
||||
id="plain_exception_no_translation_attrs",
|
||||
),
|
||||
pytest.param(
|
||||
update_coordinator.UpdateFailed("boom"),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
id="update_failed_without_translation_attrs",
|
||||
),
|
||||
pytest.param(
|
||||
TimeoutError(),
|
||||
"homeassistant",
|
||||
"update_failed_timeout",
|
||||
None,
|
||||
id="timeout",
|
||||
),
|
||||
pytest.param(
|
||||
requests.exceptions.Timeout(),
|
||||
"homeassistant",
|
||||
"update_failed_timeout",
|
||||
None,
|
||||
id="requests_timeout",
|
||||
),
|
||||
pytest.param(
|
||||
urllib.error.URLError("timed out"),
|
||||
"homeassistant",
|
||||
"update_failed_timeout",
|
||||
None,
|
||||
id="urllib_timeout",
|
||||
),
|
||||
pytest.param(
|
||||
urllib.error.URLError(TimeoutError("timed out")),
|
||||
"homeassistant",
|
||||
"update_failed_timeout",
|
||||
None,
|
||||
id="urllib_wrapped_timeout",
|
||||
),
|
||||
pytest.param(
|
||||
aiohttp.ServerTimeoutError(),
|
||||
"homeassistant",
|
||||
"update_failed_timeout",
|
||||
None,
|
||||
id="aiohttp_timeout",
|
||||
),
|
||||
pytest.param(
|
||||
aiohttp.ClientError(),
|
||||
"homeassistant",
|
||||
"update_failed",
|
||||
None,
|
||||
id="aiohttp_client_error",
|
||||
),
|
||||
pytest.param(
|
||||
requests.exceptions.RequestException(),
|
||||
"homeassistant",
|
||||
"update_failed",
|
||||
None,
|
||||
id="requests_error",
|
||||
),
|
||||
pytest.param(
|
||||
urllib.error.URLError("something"),
|
||||
"homeassistant",
|
||||
"update_failed",
|
||||
None,
|
||||
id="urllib_error",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
@@ -916,7 +979,10 @@ async def test_async_config_entry_first_refresh_translation_key_propagation(
|
||||
expected_translation_placeholders: dict[str, str] | None,
|
||||
method: str,
|
||||
) -> None:
|
||||
"""Test that translation attributes from the cause are propagated to ConfigEntryNotReady."""
|
||||
"""Test the translation attributes of the ConfigEntryNotReady raised on failure.
|
||||
|
||||
They come from the cause, or from core for errors the coordinator handles.
|
||||
"""
|
||||
entry = MockConfigEntry()
|
||||
entry._async_set_state(
|
||||
hass, config_entries.ConfigEntryState.SETUP_IN_PROGRESS, None
|
||||
|
||||
Reference in New Issue
Block a user