From 7bc159a9173c213f14b9455e981844ffaadd2709 Mon Sep 17 00:00:00 2001 From: Mahasri Kalavala Date: Fri, 2 Nov 2018 13:05:21 -0400 Subject: [PATCH 1/3] added additional filters Added base64_encode, base64_decode and ordinal filters. --- homeassistant/helpers/template.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 66f289724befe6..970640648d7847 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -4,6 +4,7 @@ import logging import math import random +import base64 import re import jinja2 @@ -602,6 +603,23 @@ def bitwise_or(first_value, second_value): return first_value | second_value +def base64_encode(value): + """Perform base64 encode.""" + return base64.b64encode(value.encode('utf-8')).decode('utf-8') + + +def base64_decode(value): + """Perform base64 denode.""" + return base64.b64decode(value).decode('utf-8') + + +def ordinal(value): + """Perform ordinal conversion.""" + return str(value) + (list(['th', 'st', 'nd', 'rd'] + ['th'] * 6) + [(int(str(value)[-1])) % 10] if not + int(str(value)[-2:]) % 100 in range(11, 14) else 'th') + + @contextfilter def random_every_time(context, values): """Choose a random value. From fac33efa33a87c39d8d4e98a176e35046afbc58d Mon Sep 17 00:00:00 2001 From: Mahasri Kalavala Date: Fri, 2 Nov 2018 13:11:35 -0400 Subject: [PATCH 2/3] added test cases added test cases for base64_encode, base64_decode and ordinal filters. --- tests/helpers/test_template.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 573a9f78b72f72..02331c400d3677 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -274,6 +274,37 @@ def test_max(self): template.Template('{{ [1, 2, 3] | max }}', self.hass).render() + def test_base64_encode(self): + """Test the base64_encode filter.""" + self.assertEqual( + 'aG9tZWFzc2lzdGFudA==', + template.Template('{{ "homeassistant" | base64_encode }}', + self.hass).render()) + + def test_base64_decode(self): + """Test the base64_decode filter.""" + self.assertEqual( + 'homeassistant', + template.Template('{{ "aG9tZWFzc2lzdGFudA==" | base64_decode }}', + self.hass).render()) + + def test_ordinal(self): + """Test the ordinal filter.""" + tests = [ + (1, '1st'), + (2, '2nd'), + (3, '3rd'), + (4, '4th'), + (5, '5th'), + ] + + for value, expected in tests: + self.assertEqual( + expected, + template.Template( + '{{ %s | ordinal }}' % value, + self.hass).render()) + def test_timestamp_utc(self): """Test the timestamps to local filter.""" now = dt_util.utcnow() From 66d6db7934db1af0c560ccffd92cf4a114ef5841 Mon Sep 17 00:00:00 2001 From: Mahasri Kalavala Date: Fri, 2 Nov 2018 13:40:05 -0400 Subject: [PATCH 3/3] forgot to add filters :) --- homeassistant/helpers/template.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 970640648d7847..6d6fb1ed2000a6 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -658,6 +658,9 @@ def is_safe_attribute(self, obj, attr, value): ENV.filters['max'] = max ENV.filters['min'] = min ENV.filters['random'] = random_every_time +ENV.filters['base64_encode'] = base64_encode +ENV.filters['base64_decode'] = base64_decode +ENV.filters['ordinal'] = ordinal ENV.filters['regex_match'] = regex_match ENV.filters['regex_replace'] = regex_replace ENV.filters['regex_search'] = regex_search