mirror of
https://github.com/home-assistant/core.git
synced 2025-07-12 15:57:06 +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:
parent
1d7e0e7fe4
commit
a67113a95a
@ -727,6 +727,7 @@ class Template:
|
|||||||
value: Any,
|
value: Any,
|
||||||
error_value: Any = _SENTINEL,
|
error_value: Any = _SENTINEL,
|
||||||
variables: dict[str, Any] | None = None,
|
variables: dict[str, Any] | None = None,
|
||||||
|
parse_result: bool = False,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Render template with value exposed.
|
"""Render template with value exposed.
|
||||||
|
|
||||||
@ -748,7 +749,9 @@ class Template:
|
|||||||
variables["value_json"] = json_loads(value)
|
variables["value_json"] = json_loads(value)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return _render_with_context(self.template, compiled, **variables).strip()
|
render_result = _render_with_context(
|
||||||
|
self.template, compiled, **variables
|
||||||
|
).strip()
|
||||||
except jinja2.TemplateError as ex:
|
except jinja2.TemplateError as ex:
|
||||||
if error_value is _SENTINEL:
|
if error_value is _SENTINEL:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
@ -759,6 +762,11 @@ class Template:
|
|||||||
)
|
)
|
||||||
return value if error_value is _SENTINEL else error_value
|
return value if error_value is _SENTINEL else error_value
|
||||||
|
|
||||||
|
if not parse_result or self.hass and self.hass.config.legacy_templates:
|
||||||
|
return render_result
|
||||||
|
|
||||||
|
return self._parse_result(render_result)
|
||||||
|
|
||||||
def _ensure_compiled(
|
def _ensure_compiled(
|
||||||
self,
|
self,
|
||||||
limited: bool = False,
|
limited: bool = False,
|
||||||
|
@ -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
|
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:
|
def test_if_state_exists(hass: HomeAssistant) -> None:
|
||||||
"""Test if state exists works."""
|
"""Test if state exists works."""
|
||||||
hass.states.async_set("test.object", "available")
|
hass.states.async_set("test.object", "available")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user