From a6100760016f4aaa12cbdeeab9eccd4b6c998b92 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 12 Apr 2021 10:02:04 +0200 Subject: [PATCH] Support min()/max() as template function (#48996) --- homeassistant/helpers/template.py | 3 +++ tests/helpers/test_template.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index ea338e22b84..83c347c7cb2 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -1457,6 +1457,9 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): self.globals["timedelta"] = timedelta self.globals["strptime"] = strptime self.globals["urlencode"] = urlencode + self.globals["max"] = max + self.globals["min"] = min + if hass is None: return diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 06b313218ca..0e8b2f76843 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -567,11 +567,15 @@ def test_from_json(hass): def test_min(hass): """Test the min filter.""" assert template.Template("{{ [1, 2, 3] | min }}", hass).async_render() == 1 + assert template.Template("{{ min([1, 2, 3]) }}", hass).async_render() == 1 + assert template.Template("{{ min(1, 2, 3) }}", hass).async_render() == 1 def test_max(hass): """Test the max filter.""" assert template.Template("{{ [1, 2, 3] | max }}", hass).async_render() == 3 + assert template.Template("{{ max([1, 2, 3]) }}", hass).async_render() == 3 + assert template.Template("{{ max(1, 2, 3) }}", hass).async_render() == 3 def test_ord(hass):