Adjust template rounding tests
parent
6ac54b20c7
commit
9ad2cf7b7a
|
@ -148,17 +148,17 @@ class LocationHelpers(object):
|
||||||
|
|
||||||
|
|
||||||
def forgiving_round(value, precision=0):
|
def forgiving_round(value, precision=0):
|
||||||
""" Rounding method that accepts strings. """
|
"""Rounding filter that accepts strings."""
|
||||||
try:
|
try:
|
||||||
value = round(float(value), precision)
|
value = round(float(value), precision)
|
||||||
return int(value) if precision == 0 else value
|
return int(value) if precision == 0 else value
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
# If value can't be converted to float
|
# If value can't be converted to float
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def multiply(value, amount):
|
def multiply(value, amount):
|
||||||
""" Converts to float and multiplies value. """
|
"""Filter to convert value to float and multiply it."""
|
||||||
try:
|
try:
|
||||||
return float(value) * amount
|
return float(value) * amount
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -167,9 +167,10 @@ def multiply(value, amount):
|
||||||
|
|
||||||
|
|
||||||
class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
||||||
""" Home Assistant template environment. """
|
"""Home Assistant template environment."""
|
||||||
|
|
||||||
def is_safe_callable(self, obj):
|
def is_safe_callable(self, obj):
|
||||||
|
"""Test if callback is safe."""
|
||||||
return isinstance(obj, AllStates) or super().is_safe_callable(obj)
|
return isinstance(obj, AllStates) or super().is_safe_callable(obj)
|
||||||
|
|
||||||
ENV = TemplateEnvironment()
|
ENV = TemplateEnvironment()
|
||||||
|
|
|
@ -62,9 +62,6 @@ class TestUtilTemplate(unittest.TestCase):
|
||||||
self.hass,
|
self.hass,
|
||||||
'{{ states.sensor.temperature.state | round(1) }}'))
|
'{{ states.sensor.temperature.state | round(1) }}'))
|
||||||
|
|
||||||
def test_rounding_value2(self):
|
|
||||||
self.hass.states.set('sensor.temperature', 12.78)
|
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'128',
|
'128',
|
||||||
template.render(
|
template.render(
|
||||||
|
@ -72,6 +69,21 @@ class TestUtilTemplate(unittest.TestCase):
|
||||||
'{{ states.sensor.temperature.state | multiply(10) | round }}'
|
'{{ 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):
|
def test_passing_vars_as_keywords(self):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'127', template.render(self.hass, '{{ hello }}', hello=127))
|
'127', template.render(self.hass, '{{ hello }}', hello=127))
|
||||||
|
|
Loading…
Reference in New Issue