From e18aaa87d025e98cdb252479669b4289e7a89ed9 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Tue, 24 Nov 2026 07:44:18 +0100 Subject: [PATCH] Add translated setup retry reasons to the update coordinator (#194048) Co-authored-by: Claude Opus 5.5 --- .../components/homeassistant/strings.json | 6 ++ homeassistant/helpers/update_coordinator.py | 32 ++++++++- tests/helpers/test_update_coordinator.py | 68 ++++++++++++++++++- 3 files changed, 103 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/homeassistant/strings.json b/homeassistant/components/homeassistant/strings.json index 578072ad0c74..c49f3ab2aed7 100644 --- a/homeassistant/components/homeassistant/strings.json +++ b/homeassistant/components/homeassistant/strings.json @@ -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": { diff --git a/homeassistant/helpers/update_coordinator.py b/homeassistant/helpers/update_coordinator.py index d1b239f83c9f..e78b43de475d 100644 --- a/homeassistant/helpers/update_coordinator.py +++ b/homeassistant/helpers/update_coordinator.py @@ -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), ) diff --git a/tests/helpers/test_update_coordinator.py b/tests/helpers/test_update_coordinator.py index 66a799fd0b1f..2d3d66f12634 100644 --- a/tests/helpers/test_update_coordinator.py +++ b/tests/helpers/test_update_coordinator.py @@ -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