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

Add has_value function/test to Jinja2 template (#79550)

This commit is contained in:
ehendrix23
2023-03-28 09:04:29 -06:00
committed by GitHub
parent 048d30904e
commit e45eab600f
2 changed files with 64 additions and 1 deletions

View File

@@ -21,6 +21,7 @@ from homeassistant.const import (
LENGTH_MILLIMETERS,
MASS_GRAMS,
STATE_ON,
STATE_UNAVAILABLE,
TEMP_CELSIUS,
VOLUME_LITERS,
UnitOfPressure,
@@ -1607,6 +1608,44 @@ def test_states_function(hass: HomeAssistant) -> None:
assert tpl.async_render() == "available"
def test_has_value(hass):
"""Test has_value method."""
hass.states.async_set("test.value1", 1)
hass.states.async_set("test.unavailable", STATE_UNAVAILABLE)
tpl = template.Template(
"""
{{ has_value("test.value1") }}
""",
hass,
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{{ has_value("test.unavailable") }}
""",
hass,
)
assert tpl.async_render() is False
tpl = template.Template(
"""
{{ has_value("test.unknown") }}
""",
hass,
)
assert tpl.async_render() is False
tpl = template.Template(
"""
{% if "test.value1" is has_value %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
@patch(
"homeassistant.helpers.template.TemplateEnvironment.is_safe_callable",
return_value=True,