Fix template rounding

This commit is contained in:
Paulus Schoutsen 2015-12-10 20:46:15 -08:00
parent 47b5fbfaf3
commit 0dcc69d800
2 changed files with 6 additions and 5 deletions

View File

@ -13,7 +13,8 @@ ENV = SandboxedEnvironment()
def forgiving_round(value, precision=0):
""" Rounding method that accepts strings. """
try:
return int(value) if precision == 0 else round(float(value), precision)
return int(float(value)) if precision == 0 else round(float(value),
precision)
except ValueError:
# If value can't be converted to float
return value

View File

@ -48,19 +48,19 @@ class TestUtilTemplate(unittest.TestCase):
'{% for state in states.sensor %}{{ state.state }}{% endfor %}'))
def test_rounding_value(self):
self.hass.states.set('sensor.temperature', 12.34)
self.hass.states.set('sensor.temperature', 12.78)
self.assertEqual(
'12.3',
'12.8',
template.render(
self.hass,
'{{ states.sensor.temperature.state | round(1) }}'))
def test_rounding_value2(self):
self.hass.states.set('sensor.temperature', 12.34)
self.hass.states.set('sensor.temperature', 12.72)
self.assertEqual(
'123',
'127',
template.render(
self.hass,
'{{ states.sensor.temperature.state | multiply(10) | round }}'))