Initialise filter_sensor with historical values (#13075)
* Initialise filter with historical values Added get_last_state_changes() * fix test * Major changes to accommodate history + time_SMA # Conflicts: # homeassistant/components/sensor/filter.py * hail the hound! * lint fixed * less debug * ups * get state from the proper entity * sensible default * No defaults in get_last_state_changes * list_reverseiterator instead of list * prev_state to state * Initialise filter with historical values Added get_last_state_changes() * fix test * Major changes to accommodate history + time_SMA # Conflicts: # homeassistant/components/sensor/filter.py * hail the hound! * lint fixed * less debug * ups * get state from the proper entity * sensible default * No defaults in get_last_state_changes * list_reverseiterator instead of list * prev_state to state * update * added window_unit * replace isinstance with window_unit
This commit is contained in:
1 parent
fdf93d1829
commit
286476f0d6
4 files changed
+229
-54
No files matched your search
@@ -7,7 +7,9 @@ from homeassistant.components.sensor.filter import (
|
||||
LowPassFilter, OutlierFilter, ThrottleFilter, TimeSMAFilter)
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.setup import setup_component
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
import homeassistant.core as ha
|
||||
from tests.common import (get_test_home_assistant, assert_setup_component,
|
||||
init_recorder_component)
|
||||
|
||||
|
||||
class TestFilterSensor(unittest.TestCase):
|
||||
@@ -16,12 +18,24 @@ class TestFilterSensor(unittest.TestCase):
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.values = [20, 19, 18, 21, 22, 0]
|
||||
raw_values = [20, 19, 18, 21, 22, 0]
|
||||
self.values = []
|
||||
|
||||
timestamp = dt_util.utcnow()
|
||||
for val in raw_values:
|
||||
self.values.append(ha.State('sensor.test_monitored',
|
||||
val, last_updated=timestamp))
|
||||
timestamp += timedelta(minutes=1)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def init_recorder(self):
|
||||
"""Initialize the recorder."""
|
||||
init_recorder_component(self.hass)
|
||||
self.hass.start()
|
||||
|
||||
def test_setup_fail(self):
|
||||
"""Test if filter doesn't exist."""
|
||||
config = {
|
||||
@@ -36,31 +50,52 @@ class TestFilterSensor(unittest.TestCase):
|
||||
|
||||
def test_chain(self):
|
||||
"""Test if filter chaining works."""
|
||||
self.init_recorder()
|
||||
config = {
|
||||
'history': {
|
||||
},
|
||||
'sensor': {
|
||||
'platform': 'filter',
|
||||
'name': 'test',
|
||||
'entity_id': 'sensor.test_monitored',
|
||||
'history_period': '00:05',
|
||||
'filters': [{
|
||||
'filter': 'outlier',
|
||||
'window_size': 10,
|
||||
'radius': 4.0
|
||||
}, {
|
||||
'filter': 'lowpass',
|
||||
'window_size': 4,
|
||||
'time_constant': 10,
|
||||
'precision': 2
|
||||
}]
|
||||
}
|
||||
}
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, 'sensor', config)
|
||||
t_0 = dt_util.utcnow() - timedelta(minutes=1)
|
||||
t_1 = dt_util.utcnow() - timedelta(minutes=2)
|
||||
t_2 = dt_util.utcnow() - timedelta(minutes=3)
|
||||
|
||||
for value in self.values:
|
||||
self.hass.states.set(config['sensor']['entity_id'], value)
|
||||
self.hass.block_till_done()
|
||||
fake_states = {
|
||||
'sensor.test_monitored': [
|
||||
ha.State('sensor.test_monitored', 18.0, last_changed=t_0),
|
||||
ha.State('sensor.test_monitored', 19.0, last_changed=t_1),
|
||||
ha.State('sensor.test_monitored', 18.2, last_changed=t_2),
|
||||
]
|
||||
}
|
||||
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('20.25', state.state)
|
||||
with patch('homeassistant.components.history.'
|
||||
'state_changes_during_period', return_value=fake_states):
|
||||
with patch('homeassistant.components.history.'
|
||||
'get_last_state_changes', return_value=fake_states):
|
||||
with assert_setup_component(1, 'sensor'):
|
||||
assert setup_component(self.hass, 'sensor', config)
|
||||
|
||||
for value in self.values:
|
||||
self.hass.states.set(
|
||||
config['sensor']['entity_id'], value.state)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('19.25', state.state)
|
||||
|
||||
def test_outlier(self):
|
||||
"""Test if outlier filter works."""
|
||||
@@ -70,7 +105,7 @@ class TestFilterSensor(unittest.TestCase):
|
||||
radius=4.0)
|
||||
for state in self.values:
|
||||
filtered = filt.filter_state(state)
|
||||
self.assertEqual(22, filtered)
|
||||
self.assertEqual(22, filtered.state)
|
||||
|
||||
def test_lowpass(self):
|
||||
"""Test if lowpass filter works."""
|
||||
@@ -80,7 +115,7 @@ class TestFilterSensor(unittest.TestCase):
|
||||
time_constant=10)
|
||||
for state in self.values:
|
||||
filtered = filt.filter_state(state)
|
||||
self.assertEqual(18.05, filtered)
|
||||
self.assertEqual(18.05, filtered.state)
|
||||
|
||||
def test_throttle(self):
|
||||
"""Test if lowpass filter works."""
|
||||
@@ -92,7 +127,7 @@ class TestFilterSensor(unittest.TestCase):
|
||||
new_state = filt.filter_state(state)
|
||||
if not filt.skip_processing:
|
||||
filtered.append(new_state)
|
||||
self.assertEqual([20, 21], filtered)
|
||||
self.assertEqual([20, 21], [f.state for f in filtered])
|
||||
|
||||
def test_time_sma(self):
|
||||
"""Test if time_sma filter works."""
|
||||
@@ -100,9 +135,6 @@ class TestFilterSensor(unittest.TestCase):
|
||||
precision=2,
|
||||
entity=None,
|
||||
type='last')
|
||||
past = dt_util.utcnow() - timedelta(minutes=5)
|
||||
for state in self.values:
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=past):
|
||||
filtered = filt.filter_state(state)
|
||||
past += timedelta(minutes=1)
|
||||
self.assertEqual(21.5, filtered)
|
||||
filtered = filt.filter_state(state)
|
||||
self.assertEqual(21.5, filtered.state)
|
||||
Reference in new issue
Block a user