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

Initializing statistics sensor with data from database (#9753)

* Initializing statistics sensor with data from database

* fixed broken test case

* usage of recorder component is now optional, thx to @andrey-git

* added test case for initialize_from_database
This commit is contained in:
ChristianKuehnel
2017-10-08 23:45:12 +02:00
committed by Fabian Affolter
parent b80f00900d
commit b620c433c0
2 changed files with 67 additions and 8 deletions

View File

@@ -8,6 +8,8 @@ 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
from tests.common import init_recorder_component
from homeassistant.components import recorder
class TestStatisticsSensor(unittest.TestCase):
@@ -135,3 +137,28 @@ class TestStatisticsSensor(unittest.TestCase):
self.assertEqual(6, state.attributes.get('min_value'))
self.assertEqual(14, state.attributes.get('max_value'))
def test_initialize_from_database(self):
"""Test initializing the statistics from the database."""
# enable the recorder
init_recorder_component(self.hass)
# store some values
for value in self.values:
self.hass.states.set('sensor.test_monitored', value,
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
self.hass.block_till_done()
# wait for the recorder to really store the data
self.hass.data[recorder.DATA_INSTANCE].block_till_done()
# only now create the statistics component, so that it must read the
# data from the database
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'statistics',
'name': 'test',
'entity_id': 'sensor.test_monitored',
'sampling_size': 100,
}
})
# check if the result is as in test_sensor_source()
state = self.hass.states.get('sensor.test_mean')
self.assertEqual(str(self.mean), state.state)