Avoid trying to load translations for integrations that have none (#112683)

pull/108127/head
J. Nick Koston 2024-03-08 14:21:44 -10:00 committed by GitHub
parent cf5b11576b
commit 08416974c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 14 deletions

View File

@ -163,6 +163,7 @@ 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]] = {}
loaded_translations_by_language: dict[str, dict[str, Any]] = {}
has_files_to_load = False
for language in languages:
files_to_load: dict[str, str] = {}
@ -171,7 +172,10 @@ async def _async_get_component_strings(
for comp in components:
domain, _, platform = comp.partition(".")
if not (integration := integrations.get(domain)):
if (
not (integration := integrations.get(domain))
or not integration.has_translations
):
continue
if platform and integration.is_built_in:
@ -185,22 +189,23 @@ async def _async_get_component_strings(
files_to_load[comp] = path
has_files_to_load = True
if not has_files_to_load:
return translations_by_language
if has_files_to_load:
loaded_translations_by_language = await hass.async_add_executor_job(
_load_translations_files_by_language, files_to_load_by_language
)
# Load files
loaded_translations_by_language = await hass.async_add_executor_job(
_load_translations_files_by_language, files_to_load_by_language
)
# Translations that miss "title" will get integration put in.
for language, loaded_translations in loaded_translations_by_language.items():
for loaded, loaded_translation in loaded_translations.items():
if "." in loaded:
for language in languages:
loaded_translations = loaded_translations_by_language.setdefault(language, {})
for comp in components:
if "." in comp:
continue
if "title" not in loaded_translation:
loaded_translation["title"] = integrations[loaded].name
# Translations that miss "title" will get integration put in.
component_translations = loaded_translations.setdefault(comp, {})
if "title" not in component_translations and (
integration := integrations.get(comp)
):
component_translations["title"] = integration.name
translations_by_language[language].update(loaded_translations)

View File

@ -825,6 +825,11 @@ class Integration:
# In the future, we want to default to True for all integrations.
return self.manifest.get("import_executor", self.is_built_in)
@cached_property
def has_translations(self) -> bool:
"""Return if the integration has translations."""
return "translations" in self._top_level_files
@property
def mqtt(self) -> list[str] | None:
"""Return Integration MQTT entries."""

View File

@ -832,3 +832,35 @@ async def test_translate_state(hass: HomeAssistant):
]
)
assert result == "on"
async def test_get_translations_still_has_title_without_translations_files(
hass: HomeAssistant, mock_config_flows
) -> None:
"""Test the title still gets added in if there are no translation files."""
mock_config_flows["integration"].append("component1")
integration = Mock(file_path=pathlib.Path(__file__))
integration.name = "Component 1"
with patch(
"homeassistant.helpers.translation.component_translation_path",
return_value="bla.json",
), patch(
"homeassistant.helpers.translation._load_translations_files_by_language",
return_value={},
), patch(
"homeassistant.helpers.translation.async_get_integrations",
return_value={"component1": integration},
):
translations = await translation.async_get_translations(
hass, "en", "title", config_flow=True
)
translations_again = await translation.async_get_translations(
hass, "en", "title", config_flow=True
)
assert translations == translations_again
assert translations == {
"component.component1.title": "Component 1",
}