Skip to content

Commit

Permalink
Add support for logarithm in templates (home-assistant#10824)
Browse files Browse the repository at this point in the history
* Add support for logarithm in templates

This adds a 'log' filter that takes the logarithm of the given value,
with an optional base number. The base defaults to 'e' - the natural
logarithm

* Remove usage of log10 in template filter 'log'

* Add logarithm as a global

This makes it possible to write:
'{{ log(4, 2) }}'
  • Loading branch information
odinuge authored and balloob committed Nov 28, 2017
1 parent 27270b4 commit 0668fba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import random
import re
import math

import jinja2
from jinja2 import contextfilter
Expand Down Expand Up @@ -423,6 +424,14 @@ def multiply(value, amount):
return value


def logarithm(value, base=math.e):
"""Filter to get logarithm of the value with a spesific base."""
try:
return math.log(float(value), float(base))
except (ValueError, TypeError):
return value


def timestamp_custom(value, date_format=DATE_STR_FORMAT, local=True):
"""Filter to convert given timestamp to format."""
try:
Expand Down Expand Up @@ -508,13 +517,15 @@ def is_safe_callable(self, obj):
ENV = TemplateEnvironment()
ENV.filters['round'] = forgiving_round
ENV.filters['multiply'] = multiply
ENV.filters['log'] = logarithm
ENV.filters['timestamp_custom'] = timestamp_custom
ENV.filters['timestamp_local'] = timestamp_local
ENV.filters['timestamp_utc'] = timestamp_utc
ENV.filters['is_defined'] = fail_when_undefined
ENV.filters['max'] = max
ENV.filters['min'] = min
ENV.filters['random'] = random_every_time
ENV.globals['log'] = logarithm
ENV.globals['float'] = forgiving_float
ENV.globals['now'] = dt_util.now
ENV.globals['utcnow'] = dt_util.utcnow
Expand Down
24 changes: 24 additions & 0 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
import unittest
import random
import math
from unittest.mock import patch

from homeassistant.components import group
Expand Down Expand Up @@ -125,6 +126,29 @@ def test_multiply(self):
template.Template('{{ %s | multiply(10) | round }}' % inp,
self.hass).render())

def test_logarithm(self):
"""Test logarithm."""
tests = [
(4, 2, '2.0'),
(1000, 10, '3.0'),
(math.e, '', '1.0'),
('"invalid"', '_', 'invalid'),
(10, '"invalid"', '10.0'),
]

for value, base, expected in tests:
self.assertEqual(
expected,
template.Template(
'{{ %s | log(%s) | round(1) }}' % (value, base),
self.hass).render())

self.assertEqual(
expected,
template.Template(
'{{ log(%s, %s) | round(1) }}' % (value, base),
self.hass).render())

def test_strptime(self):
"""Test the parse timestamp method."""
tests = [
Expand Down

0 comments on commit 0668fba

Please sign in to comment.