Add precision argument to the Range Filter (#25874)

* add precision argument

* add precision testing to range_filter
pull/24585/head
Diogo Gomes 2019-08-31 18:24:17 +01:00 committed by Martin Hjelmare
parent 46b5b0cac7
commit 9df2c3f8c9
2 changed files with 9 additions and 4 deletions

View File

@ -89,7 +89,7 @@ FILTER_LOWPASS_SCHEMA = FILTER_SCHEMA.extend(
}
)
FILTER_RANGE_SCHEMA = vol.Schema(
FILTER_RANGE_SCHEMA = FILTER_SCHEMA.extend(
{
vol.Required(CONF_FILTER_NAME): FILTER_NAME_RANGE,
vol.Optional(CONF_FILTER_LOWER_BOUND): vol.Coerce(float),
@ -406,6 +406,7 @@ class RangeFilter(Filter):
def __init__(
self,
entity,
precision: Optional[int] = DEFAULT_PRECISION,
lower_bound: Optional[float] = None,
upper_bound: Optional[float] = None,
):
@ -414,7 +415,7 @@ class RangeFilter(Filter):
:param upper_bound: band upper bound
:param lower_bound: band lower bound
"""
super().__init__(FILTER_NAME_RANGE, entity=entity)
super().__init__(FILTER_NAME_RANGE, precision=precision, entity=entity)
self._lower_bound = lower_bound
self._upper_bound = upper_bound
self._stats_internal = Counter()

View File

@ -217,7 +217,9 @@ class TestFilterSensor(unittest.TestCase):
"""Test if range filter works."""
lower = 10
upper = 20
filt = RangeFilter(entity=None, lower_bound=lower, upper_bound=upper)
filt = RangeFilter(
entity=None, precision=2, lower_bound=lower, upper_bound=upper
)
for unf_state in self.values:
unf = float(unf_state.state)
filtered = filt.filter_state(unf_state)
@ -232,7 +234,9 @@ class TestFilterSensor(unittest.TestCase):
"""Test if range filter works with zeroes as bounds."""
lower = 0
upper = 0
filt = RangeFilter(entity=None, lower_bound=lower, upper_bound=upper)
filt = RangeFilter(
entity=None, precision=2, lower_bound=lower, upper_bound=upper
)
for unf_state in self.values:
unf = float(unf_state.state)
filtered = filt.filter_state(unf_state)