1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add max_age to statistics sensor (#8790)

* Add max_age to statistics sensor

* Allow only non-zero sampling sizes

* Fix long line

* Fix style
This commit is contained in:
Lukas Barth
2017-08-30 17:13:36 +02:00
committed by Pascal Vizeli
parent 56f9ccb877
commit 3a0e38aa73
2 changed files with 65 additions and 9 deletions

View File

@@ -4,7 +4,10 @@ import statistics
from homeassistant.setup import setup_component
from homeassistant.const import (ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS)
from homeassistant.util import dt as dt_util
from tests.common import get_test_home_assistant
from unittest.mock import patch
from datetime import datetime, timedelta
class TestStatisticsSensor(unittest.TestCase):
@@ -100,3 +103,35 @@ class TestStatisticsSensor(unittest.TestCase):
self.assertEqual(3.8, state.attributes.get('min_value'))
self.assertEqual(14, state.attributes.get('max_value'))
def test_max_age(self):
"""Test value deprecation."""
mock_data = {
'return_time': datetime(2017, 8, 2, 12, 23, tzinfo=dt_util.UTC),
}
def mock_now():
return mock_data['return_time']
with patch('homeassistant.components.sensor.statistics.dt_util.utcnow',
new=mock_now):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'max_age': {'minutes': 3}
}
})
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
# insert the next value one minute later
mock_data['return_time'] += timedelta(minutes=1)
state = self.hass.states.get('sensor.test_mean')
self.assertEqual(6, state.attributes.get('min_value'))
self.assertEqual(14, state.attributes.get('max_value'))