From edd93e989e325d33f7f730123c19afbe03cebfc0 Mon Sep 17 00:00:00 2001
From: "J. Nick Koston" <nick@koston.org>
Date: Mon, 3 Apr 2023 19:38:15 -1000
Subject: [PATCH] Add render count to templates repr (#90753)

---
 homeassistant/helpers/template.py | 11 ++++++++++-
 tests/helpers/test_template.py    |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py
index fb693d6957d..f6c0ceee89b 100644
--- a/homeassistant/helpers/template.py
+++ b/homeassistant/helpers/template.py
@@ -436,6 +436,7 @@ class Template:
         "_limited",
         "_strict",
         "_hash_cache",
+        "_renders",
     )
 
     def __init__(self, template: str, hass: HomeAssistant | None = None) -> None:
@@ -452,6 +453,7 @@ class Template:
         self._limited: bool | None = None
         self._strict: bool | None = None
         self._hash_cache: int = hash(self.template)
+        self._renders: int = 0
 
     @property
     def _env(self) -> TemplateEnvironment:
@@ -521,6 +523,8 @@ class Template:
         If limited is True, the template is not allowed to access any function
         or filter depending on hass or the state machine.
         """
+        self._renders += 1
+
         if self.is_static:
             if not parse_result or self.hass and self.hass.config.legacy_templates:
                 return self.template
@@ -596,6 +600,8 @@ class Template:
 
         This method must be run in the event loop.
         """
+        self._renders += 1
+
         if self.is_static:
             return False
 
@@ -638,6 +644,7 @@ class Template:
         self, variables: TemplateVarsType = None, strict: bool = False, **kwargs: Any
     ) -> RenderInfo:
         """Render the template and collect an entity filter."""
+        self._renders += 1
         assert self.hass and _RENDER_INFO not in self.hass.data
 
         render_info = RenderInfo(self)
@@ -687,6 +694,8 @@ class Template:
 
         This method must be run in the event loop.
         """
+        self._renders += 1
+
         if self.is_static:
             return self.template
 
@@ -750,7 +759,7 @@ class Template:
 
     def __repr__(self) -> str:
         """Representation of Template."""
-        return 'Template("' + self.template + '")'
+        return f"Template<template=({self.template}) renders={self._renders}>"
 
 
 @cache
diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py
index 04bda00177f..214a349d603 100644
--- a/tests/helpers/test_template.py
+++ b/tests/helpers/test_template.py
@@ -121,7 +121,7 @@ def test_template_equality() -> None:
     assert hash(template_one) == hash(template_one_1)
     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):
         template.Template(["{{ template_one }}"])
-- 
GitLab