From 982c8f8f4a08fa1f473b236bfca120ee1941ef96 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 5 Mar 2024 18:46:24 -1000 Subject: [PATCH] Fix incorrect scope on checking files to load in translations (#112457) discovered in https://github.com/home-assistant/core/pull/112295#discussion_r1513505710 We only checked if the last language had files to load instead of all of them. The checks for each language are the same because the only reason we would skip a language is a missing/broken integration or the integration is a single file. Both of these loop conditions are always the same reguardless of the language so the check worked --- homeassistant/helpers/translation.py | 12 ++++++----- tests/helpers/test_translation.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/homeassistant/helpers/translation.py b/homeassistant/helpers/translation.py index ca79ec443ab..b799469e2d3 100644 --- a/homeassistant/helpers/translation.py +++ b/homeassistant/helpers/translation.py @@ -162,13 +162,14 @@ async def _async_get_component_strings( translations_by_language: dict[str, dict[str, Any]] = {} # Determine paths of missing components/platforms files_to_load_by_language: dict[str, dict[str, str]] = {} + has_files_to_load = False for language in languages: files_to_load: dict[str, str] = {} files_to_load_by_language[language] = files_to_load translations_by_language[language] = {} - for loaded in components: - domain, _, platform = loaded.partition(".") + for comp in components: + domain, _, platform = comp.partition(".") if not (integration := integrations.get(domain)): continue @@ -179,10 +180,11 @@ async def _async_get_component_strings( # them. continue - if path := component_translation_path(loaded, language, integration): - files_to_load[loaded] = path + if path := component_translation_path(comp, language, integration): + files_to_load[comp] = path + has_files_to_load = True - if not files_to_load: + if not has_files_to_load: return translations_by_language # Load files diff --git a/tests/helpers/test_translation.py b/tests/helpers/test_translation.py index 69d7ef274ae..839607cc8b8 100644 --- a/tests/helpers/test_translation.py +++ b/tests/helpers/test_translation.py @@ -541,6 +541,37 @@ async def test_ensure_translations_still_load_if_one_integration_fails( assert translations == sensor_translations +async def test_load_translations_all_integrations_broken( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Ensure we do not try to load translations again if the integration is broken.""" + hass.config.components.add("broken") + hass.config.components.add("broken2") + + with patch( + "homeassistant.helpers.translation.async_get_integrations", + return_value={ + "broken2": Exception("unhandled failure"), + "broken": Exception("unhandled failure"), + }, + ): + translations = await translation.async_get_translations( + hass, "en", "entity_component", integrations={"broken", "broken2"} + ) + assert "Failed to load integration for translation" in caplog.text + assert "broken" in caplog.text + assert "broken2" in caplog.text + assert not translations + caplog.clear() + + translations = await translation.async_get_translations( + hass, "en", "entity_component", integrations={"broken", "broken2"} + ) + assert not translations + # Ensure we do not try again + assert "Failed to load integration for translation" not in caplog.text + + async def test_caching(hass: HomeAssistant) -> None: """Test we cache data.""" hass.config.components.add("sensor")