From f02608371215650295eaba41d592b48a3b06e734 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 24 May 2024 17:42:55 -1000 Subject: [PATCH] Speed up is_template_string by avoiding regex engine (#118076) --- homeassistant/helpers/template.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 541626cf86dc7..8c972a26cc509 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -99,7 +99,6 @@ ) _HASS_LOADER = "template.hass_loader" -_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{|\{#") # Match "simple" ints and floats. -1.0, 1, +5, 5.0 _IS_NUMERIC = re.compile(r"^[+-]?(?!0\d)\d*(?:\.\d*)?$") @@ -261,7 +260,9 @@ def is_complex(value: Any) -> bool: def is_template_string(maybe_template: str) -> bool: """Check if the input is a Jinja2 template.""" - return _RE_JINJA_DELIMITERS.search(maybe_template) is not None + return "{" in maybe_template and ( + "{%" in maybe_template or "{{" in maybe_template or "{#" in maybe_template + ) class ResultWrapper: