Add round to half template method (#28948)

* added round to half method

Signed-off-by: Tobias Perschon <tobias@perschon.at>

* testcase for new round method

Signed-off-by: Tobias Perschon <tobias@perschon.at>
pull/28958/head
Tobias Perschon 2019-11-22 19:08:41 +01:00 committed by Paulus Schoutsen
parent 6d77c15f28
commit d92f48646a
2 changed files with 9 additions and 0 deletions

View File

@ -669,6 +669,8 @@ def forgiving_round(value, precision=0, method="common"):
value = math.ceil(float(value) * multiplier) / multiplier
elif method == "floor":
value = math.floor(float(value) * multiplier) / multiplier
elif method == "half":
value = round(float(value) * 2) / 2
else:
# if method is common or something else, use common rounding
value = round(float(value), precision)

View File

@ -227,6 +227,13 @@ def test_rounding_value(hass):
== "12.8"
)
assert (
template.Template(
'{{ states.sensor.temperature.state | round(1, "half") }}', hass
).async_render()
== "13.0"
)
def test_rounding_value_get_original_value_on_error(hass):
"""Test rounding value get original value on error."""