mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Renamed to range filter
This commit is contained in:
parent
734a83c657
commit
a0ab356936
@ -29,7 +29,7 @@ import homeassistant.util.dt as dt_util
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
FILTER_NAME_BANDPASS = 'bandpass'
|
FILTER_NAME_RANGE = 'range'
|
||||||
FILTER_NAME_LOWPASS = 'lowpass'
|
FILTER_NAME_LOWPASS = 'lowpass'
|
||||||
FILTER_NAME_OUTLIER = 'outlier'
|
FILTER_NAME_OUTLIER = 'outlier'
|
||||||
FILTER_NAME_THROTTLE = 'throttle'
|
FILTER_NAME_THROTTLE = 'throttle'
|
||||||
@ -83,8 +83,8 @@ FILTER_LOWPASS_SCHEMA = FILTER_SCHEMA.extend({
|
|||||||
default=DEFAULT_FILTER_TIME_CONSTANT): vol.Coerce(int),
|
default=DEFAULT_FILTER_TIME_CONSTANT): vol.Coerce(int),
|
||||||
})
|
})
|
||||||
|
|
||||||
FILTER_BANDPASS_SCHEMA = FILTER_SCHEMA.extend({
|
FILTER_RANGE_SCHEMA = FILTER_SCHEMA.extend({
|
||||||
vol.Required(CONF_FILTER_NAME): FILTER_NAME_BANDPASS,
|
vol.Required(CONF_FILTER_NAME): FILTER_NAME_RANGE,
|
||||||
vol.Optional(CONF_FILTER_WINDOW_SIZE,
|
vol.Optional(CONF_FILTER_WINDOW_SIZE,
|
||||||
default=DEFAULT_WINDOW_SIZE): vol.Coerce(int),
|
default=DEFAULT_WINDOW_SIZE): vol.Coerce(int),
|
||||||
vol.Optional(CONF_FILTER_LOWER_BOUND,
|
vol.Optional(CONF_FILTER_LOWER_BOUND,
|
||||||
@ -117,7 +117,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
FILTER_LOWPASS_SCHEMA,
|
FILTER_LOWPASS_SCHEMA,
|
||||||
FILTER_TIME_SMA_SCHEMA,
|
FILTER_TIME_SMA_SCHEMA,
|
||||||
FILTER_THROTTLE_SCHEMA,
|
FILTER_THROTTLE_SCHEMA,
|
||||||
FILTER_BANDPASS_SCHEMA)])
|
FILTER_RANGE_SCHEMA)])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -342,11 +342,11 @@ class Filter(object):
|
|||||||
return new_state
|
return new_state
|
||||||
|
|
||||||
|
|
||||||
@FILTERS.register(FILTER_NAME_BANDPASS)
|
@FILTERS.register(FILTER_NAME_RANGE)
|
||||||
class BandPassFilter(Filter):
|
class RangeFilter(Filter):
|
||||||
"""Band pass filter.
|
"""Range filter.
|
||||||
|
|
||||||
Determines if new state is in a band between upper_bound and lower_bound.
|
Determines if new state is in the range of upper_bound and lower_bound.
|
||||||
If not inside, lower or upper bound is returned instead.
|
If not inside, lower or upper bound is returned instead.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -357,13 +357,13 @@ class BandPassFilter(Filter):
|
|||||||
def __init__(self, window_size, precision, entity,
|
def __init__(self, window_size, precision, entity,
|
||||||
lower_bound, upper_bound):
|
lower_bound, upper_bound):
|
||||||
"""Initialize Filter."""
|
"""Initialize Filter."""
|
||||||
super().__init__(FILTER_NAME_OUTLIER, window_size, precision, entity)
|
super().__init__(FILTER_NAME_RANGE, window_size, precision, 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()
|
||||||
|
|
||||||
def _filter_state(self, new_state):
|
def _filter_state(self, new_state):
|
||||||
"""Implement the band-pass filter."""
|
"""Implement the range filter."""
|
||||||
new_state = float(new_state)
|
new_state = float(new_state)
|
||||||
|
|
||||||
if new_state > self._upper_bound:
|
if new_state > self._upper_bound:
|
||||||
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from homeassistant.components.sensor.filter import (
|
from homeassistant.components.sensor.filter import (
|
||||||
LowPassFilter, OutlierFilter, ThrottleFilter, TimeSMAFilter,
|
LowPassFilter, OutlierFilter, ThrottleFilter, TimeSMAFilter,
|
||||||
BandPassFilter)
|
RangeFilter)
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import setup_component
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
@ -132,11 +132,11 @@ class TestFilterSensor(unittest.TestCase):
|
|||||||
filtered = filt.filter_state(state)
|
filtered = filt.filter_state(state)
|
||||||
self.assertEqual(18.05, filtered.state)
|
self.assertEqual(18.05, filtered.state)
|
||||||
|
|
||||||
def test_bandpass(self):
|
def test_range(self):
|
||||||
"""Test if bandpass filter works."""
|
"""Test if range filter works."""
|
||||||
lower = 10
|
lower = 10
|
||||||
upper = 20
|
upper = 20
|
||||||
filt = BandPassFilter(1, None,
|
filt = RangeFilter(1, None,
|
||||||
entity=None,
|
entity=None,
|
||||||
lower_bound=lower,
|
lower_bound=lower,
|
||||||
upper_bound=upper)
|
upper_bound=upper)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user