hass-core/tests/components/test_history.py

146 lines
4.5 KiB
Python
Raw Normal View History

2015-04-30 21:03:01 -07:00
"""
tests.test_component_history
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests the history component.
"""
# pylint: disable=protected-access,too-many-public-methods
2015-12-27 09:39:22 -08:00
from datetime import timedelta
2015-04-30 21:03:01 -07:00
import os
import unittest
2015-09-12 22:56:49 -07:00
from unittest.mock import patch
2015-04-30 21:03:01 -07:00
2015-08-16 20:44:46 -07:00
import homeassistant.core as ha
2015-04-30 21:03:01 -07:00
import homeassistant.util.dt as dt_util
2015-07-11 00:02:52 -07:00
from homeassistant.components import history, recorder
2015-04-30 21:03:01 -07:00
from tests.common import (
2015-07-11 00:02:52 -07:00
mock_http_component, mock_state_change_event, get_test_home_assistant)
2015-04-30 21:03:01 -07:00
class TestComponentHistory(unittest.TestCase):
""" Tests homeassistant.components.history module. """
def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = get_test_home_assistant(1)
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
self.hass.stop()
2015-12-27 09:39:22 -08:00
db_path = self.hass.config.path(recorder.DB_FILE)
if os.path.isfile(db_path):
os.remove(db_path)
2015-04-30 21:03:01 -07:00
def init_recorder(self):
recorder.setup(self.hass, {})
self.hass.start()
2015-12-27 09:39:22 -08:00
self.wait_recording_done()
def wait_recording_done(self):
""" Block till recording is done. """
self.hass.pool.block_till_done()
2015-04-30 21:03:01 -07:00
recorder._INSTANCE.block_till_done()
def test_setup(self):
""" Test setup method of history. """
2015-07-11 00:02:52 -07:00
mock_http_component(self.hass)
2015-04-30 21:03:01 -07:00
self.assertTrue(history.setup(self.hass, {}))
def test_last_5_states(self):
""" Test retrieving the last 5 states. """
self.init_recorder()
states = []
entity_id = 'test.last_5_states'
for i in range(7):
self.hass.states.set(entity_id, "State {}".format(i))
2015-12-27 09:39:22 -08:00
self.wait_recording_done()
2015-04-30 21:03:01 -07:00
if i > 1:
states.append(self.hass.states.get(entity_id))
self.assertEqual(
list(reversed(states)), history.last_5_states(entity_id))
def test_get_states(self):
""" Test getting states at a specific point in time. """
self.init_recorder()
states = []
2015-12-27 09:39:22 -08:00
now = dt_util.utcnow()
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=now):
for i in range(5):
state = ha.State(
'test.point_in_time_{}'.format(i % 5),
"State {}".format(i),
{'attribute_test': i})
2015-04-30 21:03:01 -07:00
2015-12-27 09:39:22 -08:00
mock_state_change_event(self.hass, state)
2015-09-12 22:56:49 -07:00
2015-12-27 09:39:22 -08:00
states.append(state)
2015-04-30 21:03:01 -07:00
2015-12-27 09:39:22 -08:00
self.wait_recording_done()
2015-04-30 21:03:01 -07:00
2015-12-27 09:39:22 -08:00
future = now + timedelta(seconds=1)
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=future):
2015-09-12 22:56:49 -07:00
for i in range(5):
state = ha.State(
'test.point_in_time_{}'.format(i % 5),
"State {}".format(i),
{'attribute_test': i})
mock_state_change_event(self.hass, state)
2015-12-27 09:39:22 -08:00
self.wait_recording_done()
2015-09-12 22:56:49 -07:00
# Get states returns everything before POINT
self.assertEqual(states,
2015-12-27 09:39:22 -08:00
sorted(history.get_states(future),
2015-09-12 22:56:49 -07:00
key=lambda state: state.entity_id))
2015-04-30 21:03:01 -07:00
# Test get_state here because we have a DB setup
self.assertEqual(
2015-12-27 09:39:22 -08:00
states[0], history.get_state(future, states[0].entity_id))
2015-04-30 21:03:01 -07:00
def test_state_changes_during_period(self):
self.init_recorder()
entity_id = 'media_player.test'
def set_state(state):
self.hass.states.set(entity_id, state)
2015-12-27 09:39:22 -08:00
self.wait_recording_done()
2015-04-30 21:03:01 -07:00
return self.hass.states.get(entity_id)
start = dt_util.utcnow()
2015-09-12 22:56:49 -07:00
point = start + timedelta(seconds=1)
end = point + timedelta(seconds=1)
2015-12-27 09:39:22 -08:00
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=start):
set_state('idle')
set_state('YouTube')
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=point):
2015-09-12 22:56:49 -07:00
states = [
set_state('idle'),
set_state('Netflix'),
set_state('Plex'),
set_state('YouTube'),
]
2015-12-27 09:39:22 -08:00
with patch('homeassistant.components.recorder.dt_util.utcnow',
return_value=end):
2015-09-12 22:56:49 -07:00
set_state('Netflix')
set_state('Plex')
2015-04-30 21:03:01 -07:00
2015-12-27 09:39:22 -08:00
hist = history.state_changes_during_period(start, end, entity_id)
self.assertEqual(states, hist[entity_id])