Fix template rounding
This commit is contained in:
parent
47b5fbfaf3
commit
0dcc69d800
2 changed files with 6 additions and 5 deletions
|
@ -13,7 +13,8 @@ ENV = SandboxedEnvironment()
|
||||||
def forgiving_round(value, precision=0):
|
def forgiving_round(value, precision=0):
|
||||||
""" Rounding method that accepts strings. """
|
""" Rounding method that accepts strings. """
|
||||||
try:
|
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:
|
except ValueError:
|
||||||
# If value can't be converted to float
|
# If value can't be converted to float
|
||||||
return value
|
return value
|
||||||
|
|
|
@ -48,19 +48,19 @@ class TestUtilTemplate(unittest.TestCase):
|
||||||
'{% for state in states.sensor %}{{ state.state }}{% endfor %}'))
|
'{% for state in states.sensor %}{{ state.state }}{% endfor %}'))
|
||||||
|
|
||||||
def test_rounding_value(self):
|
def test_rounding_value(self):
|
||||||
self.hass.states.set('sensor.temperature', 12.34)
|
self.hass.states.set('sensor.temperature', 12.78)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'12.3',
|
'12.8',
|
||||||
template.render(
|
template.render(
|
||||||
self.hass,
|
self.hass,
|
||||||
'{{ states.sensor.temperature.state | round(1) }}'))
|
'{{ states.sensor.temperature.state | round(1) }}'))
|
||||||
|
|
||||||
def test_rounding_value2(self):
|
def test_rounding_value2(self):
|
||||||
self.hass.states.set('sensor.temperature', 12.34)
|
self.hass.states.set('sensor.temperature', 12.72)
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
'123',
|
'127',
|
||||||
template.render(
|
template.render(
|
||||||
self.hass,
|
self.hass,
|
||||||
'{{ states.sensor.temperature.state | multiply(10) | round }}'))
|
'{{ states.sensor.temperature.state | multiply(10) | round }}'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue