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
This commit is contained in:
J. Nick Koston
2024-03-05 18:46:24 -10:00
committed by GitHub
parent f5700aa318
commit 982c8f8f4a
2 changed files with 38 additions and 5 deletions

View File

@@ -162,13 +162,14 @@ async def _async_get_component_strings(
translations_by_language: dict[str, dict[str, Any]] = {} translations_by_language: dict[str, dict[str, Any]] = {}
# Determine paths of missing components/platforms # Determine paths of missing components/platforms
files_to_load_by_language: dict[str, dict[str, str]] = {} files_to_load_by_language: dict[str, dict[str, str]] = {}
has_files_to_load = False
for language in languages: for language in languages:
files_to_load: dict[str, str] = {} files_to_load: dict[str, str] = {}
files_to_load_by_language[language] = files_to_load files_to_load_by_language[language] = files_to_load
translations_by_language[language] = {} translations_by_language[language] = {}
for loaded in components: for comp in components:
domain, _, platform = loaded.partition(".") domain, _, platform = comp.partition(".")
if not (integration := integrations.get(domain)): if not (integration := integrations.get(domain)):
continue continue
@@ -179,10 +180,11 @@ async def _async_get_component_strings(
# them. # them.
continue continue
if path := component_translation_path(loaded, language, integration): if path := component_translation_path(comp, language, integration):
files_to_load[loaded] = path 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 return translations_by_language
# Load files # Load files

View File

@@ -541,6 +541,37 @@ async def test_ensure_translations_still_load_if_one_integration_fails(
assert translations == sensor_translations 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: async def test_caching(hass: HomeAssistant) -> None:
"""Test we cache data.""" """Test we cache data."""
hass.config.components.add("sensor") hass.config.components.add("sensor")