diff --git a/tests/helpers/template/extensions/test_math.py b/tests/helpers/template/extensions/test_math.py index 5a873095181..4cf26cdf517 100644 --- a/tests/helpers/template/extensions/test_math.py +++ b/tests/helpers/template/extensions/test_math.py @@ -10,10 +10,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import TemplateError from homeassistant.helpers import template - -def render(hass: HomeAssistant, template_str: str) -> str: - """Render template and return result.""" - return template.Template(template_str, hass).async_render() +from tests.helpers.template.helpers import render def test_math_constants(hass: HomeAssistant) -> None: diff --git a/tests/helpers/template/helpers.py b/tests/helpers/template/helpers.py new file mode 100644 index 00000000000..f15e63b0b09 --- /dev/null +++ b/tests/helpers/template/helpers.py @@ -0,0 +1,61 @@ +"""Helpers for tests around template rendering.""" + +from __future__ import annotations + +from collections.abc import Iterable +from typing import Any + +from homeassistant.core import HomeAssistant +from homeassistant.helpers import template +from homeassistant.helpers.typing import TemplateVarsType + + +def render( + hass: HomeAssistant, + template_str: str, + variables: TemplateVarsType | None = None, + **render_kwargs: Any, +) -> Any: + """Render template and return result.""" + return template.Template(template_str, hass).async_render( + variables, **render_kwargs + ) + + +def render_to_info( + hass: HomeAssistant, template_str: str, variables: TemplateVarsType | None = None +) -> template.RenderInfo: + """Create render info from template.""" + return template.Template(template_str, hass).async_render_to_info(variables) + + +def extract_entities( + hass: HomeAssistant, template_str: str, variables: TemplateVarsType | None = None +) -> set[str]: + """Extract entities from a template.""" + return render_to_info(hass, template_str, variables).entities + + +def assert_result_info( + info: template.RenderInfo, + result: Any, + entities: Iterable[str] | None = None, + domains: Iterable[str] | None = None, + all_states: bool = False, +) -> None: + """Check result info.""" + assert info.result() == result + assert info.all_states == all_states + assert info.filter("invalid_entity_name.somewhere") == all_states + if entities is not None: + assert info.entities == frozenset(entities) + assert all(info.filter(entity) for entity in entities) + if not all_states: + assert not info.filter("invalid_entity_name.somewhere") + else: + assert not info.entities + if domains is not None: + assert info.domains == frozenset(domains) + assert all(info.filter(domain + ".entity") for domain in domains) + else: + assert not hasattr(info, "_domains") diff --git a/tests/helpers/template/test_init.py b/tests/helpers/template/test_init.py index 44399869ef8..c4e7c058a83 100644 --- a/tests/helpers/template/test_init.py +++ b/tests/helpers/template/test_init.py @@ -52,12 +52,13 @@ from homeassistant.helpers.template.render_info import ( ALL_STATES_RATE_LIMIT, DOMAIN_STATES_RATE_LIMIT, ) -from homeassistant.helpers.typing import TemplateVarsType from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from homeassistant.util.read_only_dict import ReadOnlyDict from homeassistant.util.unit_system import UnitSystem +from .helpers import assert_result_info, render, render_to_info + from tests.common import MockConfigEntry, async_fire_time_changed @@ -77,55 +78,6 @@ def _set_up_units(hass: HomeAssistant) -> None: ) -def render( - hass: HomeAssistant, template_str: str, variables: TemplateVarsType | None = None -) -> Any: - """Create render info from template.""" - tmp = template.Template(template_str, hass) - return tmp.async_render(variables) - - -def render_to_info( - hass: HomeAssistant, template_str: str, variables: TemplateVarsType | None = None -) -> template.RenderInfo: - """Create render info from template.""" - tmp = template.Template(template_str, hass) - return tmp.async_render_to_info(variables) - - -def extract_entities( - hass: HomeAssistant, template_str: str, variables: TemplateVarsType | None = None -) -> set[str]: - """Extract entities from a template.""" - info = render_to_info(hass, template_str, variables) - return info.entities - - -def assert_result_info( - info: template.RenderInfo, - result: Any, - entities: Iterable[str] | None = None, - domains: Iterable[str] | None = None, - all_states: bool = False, -) -> None: - """Check result info.""" - assert info.result() == result - assert info.all_states == all_states - assert info.filter("invalid_entity_name.somewhere") == all_states - if entities is not None: - assert info.entities == frozenset(entities) - assert all(info.filter(entity) for entity in entities) - if not all_states: - assert not info.filter("invalid_entity_name.somewhere") - else: - assert not info.entities - if domains is not None: - assert info.domains == frozenset(domains) - assert all(info.filter(domain + ".entity") for domain in domains) - else: - assert not hasattr(info, "_domains") - - async def test_template_render_missing_hass(hass: HomeAssistant) -> None: """Test template render when hass is not set.""" hass.states.async_set("sensor.test", "23")