From 9ad2cf7b7a470ae724a0b97ee8b04b36f96c128d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 20 Feb 2016 21:59:16 -0800 Subject: [PATCH] Adjust template rounding tests --- homeassistant/util/template.py | 9 +++++---- tests/util/test_template.py | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/homeassistant/util/template.py b/homeassistant/util/template.py index 3006a426d43..0cbddb2aa16 100644 --- a/homeassistant/util/template.py +++ b/homeassistant/util/template.py @@ -148,17 +148,17 @@ class LocationHelpers(object): def forgiving_round(value, precision=0): - """ Rounding method that accepts strings. """ + """Rounding filter that accepts strings.""" try: value = round(float(value), precision) return int(value) if precision == 0 else value - except ValueError: + except (ValueError, TypeError): # If value can't be converted to float return value def multiply(value, amount): - """ Converts to float and multiplies value. """ + """Filter to convert value to float and multiply it.""" try: return float(value) * amount except ValueError: @@ -167,9 +167,10 @@ def multiply(value, amount): class TemplateEnvironment(ImmutableSandboxedEnvironment): - """ Home Assistant template environment. """ + """Home Assistant template environment.""" def is_safe_callable(self, obj): + """Test if callback is safe.""" return isinstance(obj, AllStates) or super().is_safe_callable(obj) ENV = TemplateEnvironment() diff --git a/tests/util/test_template.py b/tests/util/test_template.py index cac5978393d..737bb87fce9 100644 --- a/tests/util/test_template.py +++ b/tests/util/test_template.py @@ -62,9 +62,6 @@ class TestUtilTemplate(unittest.TestCase): self.hass, '{{ states.sensor.temperature.state | round(1) }}')) - def test_rounding_value2(self): - self.hass.states.set('sensor.temperature', 12.78) - self.assertEqual( '128', template.render( @@ -72,6 +69,21 @@ class TestUtilTemplate(unittest.TestCase): '{{ states.sensor.temperature.state | multiply(10) | round }}' )) + def test_rounding_value_get_original_value_on_error(self): + self.assertEqual( + 'None', + template.render( + self.hass, + '{{ None | round }}' + )) + + self.assertEqual( + 'no_number', + template.render( + self.hass, + '{{ "no_number" | round }}' + )) + def test_passing_vars_as_keywords(self): self.assertEqual( '127', template.render(self.hass, '{{ hello }}', hello=127))