convert to integer if rounding precision is zero (#30226)
Convert values to integer if rounding precision is zero. With that a value which is an integer before filtering can be configured to stay integer when using precision = 0. This also aligns behavior of filters to how rounding behaves in tempaltes (homeassistant/helpers/template.py, function forgiving_round).
This commit is contained in:
parent
fdfedd086b
commit
3f33fc6122
2 changed files with 9 additions and 1 deletions
|
@ -324,7 +324,8 @@ class FilterState:
|
|||
def set_precision(self, precision):
|
||||
"""Set precision of Number based states."""
|
||||
if isinstance(self.state, Number):
|
||||
self.state = round(float(self.state), precision)
|
||||
value = round(float(self.state), precision)
|
||||
self.state = int(value) if precision == 0 else value
|
||||
|
||||
def __str__(self):
|
||||
"""Return state as the string representation of FilterState."""
|
||||
|
|
|
@ -208,6 +208,13 @@ class TestFilterSensor(unittest.TestCase):
|
|||
filtered = filt.filter_state(state)
|
||||
assert 21 == filtered.state
|
||||
|
||||
def test_precision_zero(self):
|
||||
"""Test if precision of zero returns an integer."""
|
||||
filt = LowPassFilter(window_size=10, precision=0, entity=None, time_constant=10)
|
||||
for state in self.values:
|
||||
filtered = filt.filter_state(state)
|
||||
assert isinstance(filtered.state, int)
|
||||
|
||||
def test_lowpass(self):
|
||||
"""Test if lowpass filter works."""
|
||||
filt = LowPassFilter(window_size=10, precision=2, entity=None, time_constant=10)
|
||||
|
|
Loading…
Add table
Reference in a new issue