mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Add precision argument to the Range Filter (#25874)
* add precision argument * add precision testing to range_filter
This commit is contained in:
parent
46b5b0cac7
commit
9df2c3f8c9
@ -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.Required(CONF_FILTER_NAME): FILTER_NAME_RANGE,
|
||||||
vol.Optional(CONF_FILTER_LOWER_BOUND): vol.Coerce(float),
|
vol.Optional(CONF_FILTER_LOWER_BOUND): vol.Coerce(float),
|
||||||
@ -406,6 +406,7 @@ class RangeFilter(Filter):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
entity,
|
entity,
|
||||||
|
precision: Optional[int] = DEFAULT_PRECISION,
|
||||||
lower_bound: Optional[float] = None,
|
lower_bound: Optional[float] = None,
|
||||||
upper_bound: Optional[float] = None,
|
upper_bound: Optional[float] = None,
|
||||||
):
|
):
|
||||||
@ -414,7 +415,7 @@ class RangeFilter(Filter):
|
|||||||
:param upper_bound: band upper bound
|
:param upper_bound: band upper bound
|
||||||
:param lower_bound: band lower 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._lower_bound = lower_bound
|
||||||
self._upper_bound = upper_bound
|
self._upper_bound = upper_bound
|
||||||
self._stats_internal = Counter()
|
self._stats_internal = Counter()
|
||||||
|
@ -217,7 +217,9 @@ class TestFilterSensor(unittest.TestCase):
|
|||||||
"""Test if range filter works."""
|
"""Test if range filter works."""
|
||||||
lower = 10
|
lower = 10
|
||||||
upper = 20
|
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:
|
for unf_state in self.values:
|
||||||
unf = float(unf_state.state)
|
unf = float(unf_state.state)
|
||||||
filtered = filt.filter_state(unf_state)
|
filtered = filt.filter_state(unf_state)
|
||||||
@ -232,7 +234,9 @@ class TestFilterSensor(unittest.TestCase):
|
|||||||
"""Test if range filter works with zeroes as bounds."""
|
"""Test if range filter works with zeroes as bounds."""
|
||||||
lower = 0
|
lower = 0
|
||||||
upper = 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:
|
for unf_state in self.values:
|
||||||
unf = float(unf_state.state)
|
unf = float(unf_state.state)
|
||||||
filtered = filt.filter_state(unf_state)
|
filtered = filt.filter_state(unf_state)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user