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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user