mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 23:37:18 +00:00
Add render count to templates repr (#90753)
This commit is contained in:
parent
a4bf71b655
commit
edd93e989e
@ -436,6 +436,7 @@ class Template:
|
|||||||
"_limited",
|
"_limited",
|
||||||
"_strict",
|
"_strict",
|
||||||
"_hash_cache",
|
"_hash_cache",
|
||||||
|
"_renders",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, template: str, hass: HomeAssistant | None = None) -> None:
|
def __init__(self, template: str, hass: HomeAssistant | None = None) -> None:
|
||||||
@ -452,6 +453,7 @@ class Template:
|
|||||||
self._limited: bool | None = None
|
self._limited: bool | None = None
|
||||||
self._strict: bool | None = None
|
self._strict: bool | None = None
|
||||||
self._hash_cache: int = hash(self.template)
|
self._hash_cache: int = hash(self.template)
|
||||||
|
self._renders: int = 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _env(self) -> TemplateEnvironment:
|
def _env(self) -> TemplateEnvironment:
|
||||||
@ -521,6 +523,8 @@ class Template:
|
|||||||
If limited is True, the template is not allowed to access any function
|
If limited is True, the template is not allowed to access any function
|
||||||
or filter depending on hass or the state machine.
|
or filter depending on hass or the state machine.
|
||||||
"""
|
"""
|
||||||
|
self._renders += 1
|
||||||
|
|
||||||
if self.is_static:
|
if self.is_static:
|
||||||
if not parse_result or self.hass and self.hass.config.legacy_templates:
|
if not parse_result or self.hass and self.hass.config.legacy_templates:
|
||||||
return self.template
|
return self.template
|
||||||
@ -596,6 +600,8 @@ class Template:
|
|||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
|
self._renders += 1
|
||||||
|
|
||||||
if self.is_static:
|
if self.is_static:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -638,6 +644,7 @@ class Template:
|
|||||||
self, variables: TemplateVarsType = None, strict: bool = False, **kwargs: Any
|
self, variables: TemplateVarsType = None, strict: bool = False, **kwargs: Any
|
||||||
) -> RenderInfo:
|
) -> RenderInfo:
|
||||||
"""Render the template and collect an entity filter."""
|
"""Render the template and collect an entity filter."""
|
||||||
|
self._renders += 1
|
||||||
assert self.hass and _RENDER_INFO not in self.hass.data
|
assert self.hass and _RENDER_INFO not in self.hass.data
|
||||||
|
|
||||||
render_info = RenderInfo(self)
|
render_info = RenderInfo(self)
|
||||||
@ -687,6 +694,8 @@ class Template:
|
|||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
|
self._renders += 1
|
||||||
|
|
||||||
if self.is_static:
|
if self.is_static:
|
||||||
return self.template
|
return self.template
|
||||||
|
|
||||||
@ -750,7 +759,7 @@ class Template:
|
|||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
"""Representation of Template."""
|
"""Representation of Template."""
|
||||||
return 'Template("' + self.template + '")'
|
return f"Template<template=({self.template}) renders={self._renders}>"
|
||||||
|
|
||||||
|
|
||||||
@cache
|
@cache
|
||||||
|
@ -121,7 +121,7 @@ def test_template_equality() -> None:
|
|||||||
assert hash(template_one) == hash(template_one_1)
|
assert hash(template_one) == hash(template_one_1)
|
||||||
assert hash(template_one) != hash(template_two)
|
assert hash(template_one) != hash(template_two)
|
||||||
|
|
||||||
assert str(template_one_1) == 'Template("{{ template_one }}")'
|
assert str(template_one_1) == "Template<template=({{ template_one }}) renders=0>"
|
||||||
|
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
template.Template(["{{ template_one }}"])
|
template.Template(["{{ template_one }}"])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user