1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

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 <erik@montnemery.com>

* 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 <erik@montnemery.com>

* Refactor listeners that trigger translation loading

* Apply suggestions from code review

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* 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 <erik@montnemery.com>

* Apply suggestions from code review

* Fix tests

* Fix tests

---------

Co-authored-by: Piotr Machowski <PiotrMachowski@users.noreply.github.com>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Piotr Machowski
2024-02-10 10:47:56 +01:00
committed by GitHub
parent d1f098c11f
commit a2f4e99994
6 changed files with 555 additions and 15 deletions

View File

@@ -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 "<template AllStates>"
class StateTranslated:
"""Class to represent a translated state in a template."""
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize all states."""
self._hass = hass
def __call__(self, entity_id: str) -> str | None:
"""Retrieve translated state if available."""
state = _get_state_if_valid(self._hass, entity_id)
if state is None:
return STATE_UNKNOWN
state_value = state.state
domain = state.domain
device_class = state.attributes.get("device_class")
entry = entity_registry.async_get(self._hass).async_get(entity_id)
platform = None if entry is None else entry.platform
translation_key = None if entry is None else entry.translation_key
return async_translate_state(
self._hass, state_value, domain, platform, translation_key, device_class
)
def __repr__(self) -> str:
"""Representation of Translated state."""
return "<template StateTranslated>"
class DomainStates:
"""Class to expose a specific HA domain as attributes."""
@@ -2626,6 +2657,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
"is_state_attr",
"state_attr",
"states",
"state_translated",
"has_value",
"utcnow",
"now",
@@ -2676,6 +2708,8 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
self.filters["state_attr"] = self.globals["state_attr"]
self.globals["states"] = AllStates(hass)
self.filters["states"] = self.globals["states"]
self.globals["state_translated"] = StateTranslated(hass)
self.filters["state_translated"] = self.globals["state_translated"]
self.globals["has_value"] = hassfunction(has_value)
self.filters["has_value"] = self.globals["has_value"]
self.tests["has_value"] = hassfunction(has_value, pass_eval_context)
@@ -2688,7 +2722,9 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
def is_safe_callable(self, obj):
"""Test if callback is safe."""
return isinstance(obj, AllStates) or super().is_safe_callable(obj)
return isinstance(
obj, (AllStates, StateTranslated)
) or super().is_safe_callable(obj)
def is_safe_attribute(self, obj, attr, value):
"""Test if attribute is safe."""