mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Evaluate the translated state template filters at render time (#190336)
This commit is contained in:
@@ -41,8 +41,8 @@ class StateExtension(BaseTemplateExtension):
|
||||
def __init__(self, environment: TemplateEnvironment) -> None:
|
||||
"""Initialize the state extension."""
|
||||
# Build the class-based instance functions only when hass is available.
|
||||
# These use pass_context=False because they are callable class instances
|
||||
# that should not be wrapped by _pass_context.
|
||||
# The globals use pass_context=False because they are callable class
|
||||
# instances that should not be wrapped by _pass_context.
|
||||
class_functions: list[TemplateFunction] = []
|
||||
if (hass := environment.hass) is not None:
|
||||
class_functions = [
|
||||
@@ -50,19 +50,31 @@ class StateExtension(BaseTemplateExtension):
|
||||
"state_attr_translated",
|
||||
StateAttrTranslated(hass),
|
||||
as_global=True,
|
||||
requires_hass=True,
|
||||
limited_ok=False,
|
||||
pass_context=False,
|
||||
),
|
||||
TemplateFunction(
|
||||
"state_attr_translated",
|
||||
StateAttrTranslated(hass).__call__,
|
||||
as_filter=True,
|
||||
requires_hass=True,
|
||||
limited_ok=False,
|
||||
),
|
||||
TemplateFunction(
|
||||
"state_translated",
|
||||
StateTranslated(hass),
|
||||
as_global=True,
|
||||
requires_hass=True,
|
||||
limited_ok=False,
|
||||
pass_context=False,
|
||||
),
|
||||
TemplateFunction(
|
||||
"state_translated",
|
||||
StateTranslated(hass),
|
||||
as_global=True,
|
||||
StateTranslated(hass).__call__,
|
||||
as_filter=True,
|
||||
requires_hass=True,
|
||||
limited_ok=False,
|
||||
pass_context=False,
|
||||
),
|
||||
TemplateFunction(
|
||||
"states",
|
||||
|
||||
@@ -283,6 +283,71 @@ def test_states_filter_limited(hass: HomeAssistant) -> None:
|
||||
tmpl.async_render(limited=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("template_string", "expected"),
|
||||
[
|
||||
pytest.param(
|
||||
"{{ state_translated('sensor.test') }}",
|
||||
["unknown", "first", "second", "unknown"],
|
||||
id="state_translated_function",
|
||||
),
|
||||
pytest.param(
|
||||
"{{ 'sensor.test' | state_translated }}",
|
||||
["unknown", "first", "second", "unknown"],
|
||||
id="state_translated_filter",
|
||||
),
|
||||
pytest.param(
|
||||
"{{ state_attr_translated('sensor.test', 'mode') }}",
|
||||
[None, "eco", "boost", None],
|
||||
id="state_attr_translated_function",
|
||||
),
|
||||
pytest.param(
|
||||
"{{ 'sensor.test' | state_attr_translated('mode') }}",
|
||||
[None, "eco", "boost", None],
|
||||
id="state_attr_translated_filter",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_translated_states_track_changes(
|
||||
hass: HomeAssistant, template_string: str, expected: list[str | None]
|
||||
) -> None:
|
||||
"""Read current translated states and collect dependencies on every render."""
|
||||
tmpl = template.Template(template_string, hass)
|
||||
assert_result_info(
|
||||
tmpl.async_render_to_info(), expected[0], entities=["sensor.test"]
|
||||
)
|
||||
hass.states.async_set("sensor.test", "first", {"mode": "eco"})
|
||||
assert_result_info(
|
||||
tmpl.async_render_to_info(), expected[1], entities=["sensor.test"]
|
||||
)
|
||||
hass.states.async_set("sensor.test", "second", {"mode": "boost"})
|
||||
assert_result_info(
|
||||
tmpl.async_render_to_info(), expected[2], entities=["sensor.test"]
|
||||
)
|
||||
hass.states.async_remove("sensor.test")
|
||||
assert_result_info(
|
||||
tmpl.async_render_to_info(), expected[3], entities=["sensor.test"]
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"template_string",
|
||||
[
|
||||
pytest.param("{{ 'sensor.test' | state_translated }}", id="state_translated"),
|
||||
pytest.param(
|
||||
"{{ 'sensor.test' | state_attr_translated('mode') }}",
|
||||
id="state_attr_translated",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_translated_filters_limited(hass: HomeAssistant, template_string: str) -> None:
|
||||
"""The translated filters must not expose states in limited templates."""
|
||||
hass.states.async_set("sensor.test", "first", {"mode": "eco"})
|
||||
tmpl = template.Template(template_string, hass)
|
||||
with pytest.raises(TemplateError, match="not supported in limited templates"):
|
||||
tmpl.async_render(limited=True)
|
||||
|
||||
|
||||
async def test_state_translated(
|
||||
hass: HomeAssistant, entity_registry: er.EntityRegistry
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user