From a2f4e99994037038779802abe66d96e594d00ee6 Mon Sep 17 00:00:00 2001 From: Piotr Machowski <6118709+PiotrMachowski@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:47:56 +0100 Subject: [PATCH] Add state_translated function to jinja templates (#96906) * Add state_translated jinja function * Add tests for load_state_translations_to_cache and get_cached_translations * Cleanup state_translated template * Add tests for state_translated jinja function * Apply black formatting * Improve code quality * Apply suggestions from code review Co-authored-by: Erik Montnemery * Apply suggestions from code review * Prevent invalid components from loading translations * Refactor loading translations to cache * Adjust code issues * Update homeassistant/helpers/translation.py Co-authored-by: Erik Montnemery * Refactor listeners that trigger translation loading * Apply suggestions from code review Co-authored-by: Erik Montnemery * Apply suggestions from code review * Adjust invalid function calls, fix code styling * Adjust code quality * Extract async_translate_state function * Apply suggestions from code review Co-authored-by: Erik Montnemery * Apply suggestions from code review * Fix tests * Fix tests --------- Co-authored-by: Piotr Machowski Co-authored-by: Erik Montnemery --- homeassistant/bootstrap.py | 2 + homeassistant/helpers/template.py | 38 ++++- homeassistant/helpers/translation.py | 183 ++++++++++++++++++++-- tests/common.py | 6 + tests/helpers/test_template.py | 124 +++++++++++++++ tests/helpers/test_translation.py | 217 ++++++++++++++++++++++++++- 6 files changed, 555 insertions(+), 15 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 985108fb9f8..83aa8cb893d 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -35,6 +35,7 @@ from .helpers import ( recorder, restore_state, template, + translation, ) from .helpers.dispatcher import async_dispatcher_send from .helpers.typing import ConfigType @@ -291,6 +292,7 @@ async def async_load_base_functionality(hass: core.HomeAssistant) -> None: platform.uname().processor # pylint: disable=expression-not-assigned # Load the registries and cache the result of platform.uname().processor + translation.async_setup(hass) entity.async_setup(hass) template.async_setup(hass) await asyncio.gather( diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 63c24b0f9e9..86e3385a21b 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -80,6 +80,7 @@ from homeassistant.util.thread import ThreadWithException from . import area_registry, device_registry, entity_registry, location as loc_helper from .singleton import singleton +from .translation import async_translate_state from .typing import TemplateVarsType # mypy: allow-untyped-defs, no-check-untyped-defs @@ -894,6 +895,36 @@ class AllStates: return "