diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 353fda28875..3a24de6b39c 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -452,6 +452,38 @@ def logarithm(value, base=math.e): return value +def sine(value): + """Filter to get sine of the value.""" + try: + return math.sin(float(value)) + except (ValueError, TypeError): + return value + + +def cosine(value): + """Filter to get cosine of the value.""" + try: + return math.cos(float(value)) + except (ValueError, TypeError): + return value + + +def tangent(value): + """Filter to get tangent of the value.""" + try: + return math.tan(float(value)) + except (ValueError, TypeError): + return value + + +def square_root(value): + """Filter to get square root of the value.""" + try: + return math.sqrt(float(value)) + except (ValueError, TypeError): + return value + + def timestamp_custom(value, date_format=DATE_STR_FORMAT, local=True): """Filter to convert given timestamp to format.""" try: @@ -571,6 +603,10 @@ ENV = TemplateEnvironment() ENV.filters['round'] = forgiving_round ENV.filters['multiply'] = multiply ENV.filters['log'] = logarithm +ENV.filters['sin'] = sine +ENV.filters['cos'] = cosine +ENV.filters['tan'] = tangent +ENV.filters['sqrt'] = square_root ENV.filters['timestamp_custom'] = timestamp_custom ENV.filters['timestamp_local'] = timestamp_local ENV.filters['timestamp_utc'] = timestamp_utc @@ -583,6 +619,13 @@ ENV.filters['regex_replace'] = regex_replace ENV.filters['regex_search'] = regex_search ENV.filters['regex_findall_index'] = regex_findall_index ENV.globals['log'] = logarithm +ENV.globals['sin'] = sine +ENV.globals['cos'] = cosine +ENV.globals['tan'] = tangent +ENV.globals['sqrt'] = square_root +ENV.globals['pi'] = math.pi +ENV.globals['tau'] = math.pi * 2 +ENV.globals['e'] = math.e ENV.globals['float'] = forgiving_float ENV.globals['now'] = dt_util.now ENV.globals['utcnow'] = dt_util.utcnow diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 650b98509d0..2dfcb2a58e5 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -149,6 +149,74 @@ class TestHelpersTemplate(unittest.TestCase): '{{ log(%s, %s) | round(1) }}' % (value, base), self.hass).render()) + def test_sine(self): + """Test sine.""" + tests = [ + (0, '0.0'), + (math.pi / 2, '1.0'), + (math.pi, '0.0'), + (math.pi * 1.5, '-1.0'), + (math.pi / 10, '0.309') + ] + + for value, expected in tests: + self.assertEqual( + expected, + template.Template( + '{{ %s | sin | round(3) }}' % value, + self.hass).render()) + + def test_cos(self): + """Test cosine.""" + tests = [ + (0, '1.0'), + (math.pi / 2, '0.0'), + (math.pi, '-1.0'), + (math.pi * 1.5, '-0.0'), + (math.pi / 10, '0.951') + ] + + for value, expected in tests: + self.assertEqual( + expected, + template.Template( + '{{ %s | cos | round(3) }}' % value, + self.hass).render()) + + def test_tan(self): + """Test tangent.""" + tests = [ + (0, '0.0'), + (math.pi, '-0.0'), + (math.pi / 180 * 45, '1.0'), + (math.pi / 180 * 90, '1.633123935319537e+16'), + (math.pi / 180 * 135, '-1.0') + ] + + for value, expected in tests: + self.assertEqual( + expected, + template.Template( + '{{ %s | tan | round(3) }}' % value, + self.hass).render()) + + def test_sqrt(self): + """Test square root.""" + tests = [ + (0, '0.0'), + (1, '1.0'), + (2, '1.414'), + (10, '3.162'), + (100, '10.0'), + ] + + for value, expected in tests: + self.assertEqual( + expected, + template.Template( + '{{ %s | sqrt | round(3) }}' % value, + self.hass).render()) + def test_strptime(self): """Test the parse timestamp method.""" tests = [