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

Parse template result in async_render_with_possible_json_value (#99670)

* Optionally parse templates rendered with possible json

* Remove duplicate strip

* Add tests for parsing template result
This commit is contained in:
Daniel
2024-01-24 12:12:28 +01:00
committed by GitHub
parent 1d7e0e7fe4
commit a67113a95a
2 changed files with 29 additions and 1 deletions

View File

@@ -1728,6 +1728,26 @@ def test_render_with_possible_json_value_non_string_value(hass: HomeAssistant) -
assert tpl.async_render_with_possible_json_value(value) == expected
def test_render_with_possible_json_value_and_parse_result(hass: HomeAssistant) -> None:
"""Render with possible JSON value with valid JSON."""
tpl = template.Template("{{ value_json.hello }}", hass)
result = tpl.async_render_with_possible_json_value(
"""{"hello": {"world": "value1"}}""", parse_result=True
)
assert isinstance(result, dict)
def test_render_with_possible_json_value_and_dont_parse_result(
hass: HomeAssistant,
) -> None:
"""Render with possible JSON value with valid JSON."""
tpl = template.Template("{{ value_json.hello }}", hass)
result = tpl.async_render_with_possible_json_value(
"""{"hello": {"world": "value1"}}""", parse_result=False
)
assert isinstance(result, str)
def test_if_state_exists(hass: HomeAssistant) -> None:
"""Test if state exists works."""
hass.states.async_set("test.object", "available")