core/tests/components/test_logbook.py

162 lines
5.7 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the logbook component."""
2015-05-01 04:47:20 +00:00
# pylint: disable=protected-access,too-many-public-methods
import unittest
from datetime import timedelta
2015-08-17 03:44:46 +00:00
import homeassistant.core as ha
2015-05-01 04:47:20 +00:00
from homeassistant.const import (
EVENT_STATE_CHANGED, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP)
import homeassistant.util.dt as dt_util
2015-07-11 07:02:52 +00:00
from homeassistant.components import logbook
2015-05-01 04:47:20 +00:00
2016-02-14 23:08:23 +00:00
from tests.common import mock_http_component, get_test_home_assistant
2015-05-01 04:47:20 +00:00
2016-05-14 07:58:36 +00:00
class TestComponentLogbook(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the History component."""
2015-05-01 04:47:20 +00:00
2016-01-30 21:14:10 +00:00
def setUp(self):
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
2016-01-30 21:14:10 +00:00
mock_http_component(self.hass)
self.assertTrue(logbook.setup(self.hass, {}))
def tearDown(self):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2016-01-30 21:14:10 +00:00
self.hass.stop()
def test_service_call_create_logbook_entry(self):
2016-03-09 09:25:50 +00:00
"""Test if service call create log book entry."""
2016-01-30 21:14:10 +00:00
calls = []
def event_listener(event):
calls.append(event)
self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
self.hass.services.call(logbook.DOMAIN, 'log', {
logbook.ATTR_NAME: 'Alarm',
logbook.ATTR_MESSAGE: 'is triggered',
logbook.ATTR_DOMAIN: 'switch',
logbook.ATTR_ENTITY_ID: 'switch.test_switch'
2016-01-30 21:14:10 +00:00
}, True)
self.hass.pool.block_till_done()
self.assertEqual(1, len(calls))
last_call = calls[-1]
self.assertEqual('Alarm', last_call.data.get(logbook.ATTR_NAME))
self.assertEqual('is triggered', last_call.data.get(
logbook.ATTR_MESSAGE))
self.assertEqual('switch', last_call.data.get(logbook.ATTR_DOMAIN))
self.assertEqual('switch.test_switch', last_call.data.get(
2016-01-30 21:14:10 +00:00
logbook.ATTR_ENTITY_ID))
def test_service_call_create_log_book_entry_no_message(self):
2016-03-09 09:25:50 +00:00
"""Test if service call create log book entry without message."""
2016-01-30 21:14:10 +00:00
calls = []
def event_listener(event):
calls.append(event)
self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
self.hass.services.call(logbook.DOMAIN, 'log', {}, True)
self.hass.pool.block_till_done()
self.assertEqual(0, len(calls))
2015-05-01 04:47:20 +00:00
def test_humanify_filter_sensor(self):
2016-03-09 09:25:50 +00:00
"""Test humanify filter too frequent sensor values."""
2015-05-01 04:47:20 +00:00
entity_id = 'sensor.bla'
2016-04-16 07:55:35 +00:00
pointA = dt_util.utcnow().replace(minute=2)
2015-05-01 04:47:20 +00:00
pointB = pointA.replace(minute=5)
pointC = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
eventA = self.create_state_changed_event(pointA, entity_id, 10)
eventB = self.create_state_changed_event(pointB, entity_id, 20)
eventC = self.create_state_changed_event(pointC, entity_id, 30)
entries = list(logbook.humanify((eventA, eventB, eventC)))
self.assertEqual(2, len(entries))
self.assert_entry(
entries[0], pointB, 'bla', domain='sensor', entity_id=entity_id)
self.assert_entry(
entries[1], pointC, 'bla', domain='sensor', entity_id=entity_id)
2016-01-30 21:14:10 +00:00
def test_entry_to_dict(self):
2016-03-09 09:25:50 +00:00
"""Test conversion of entry to dict."""
2016-01-30 21:14:10 +00:00
entry = logbook.Entry(
dt_util.utcnow(), 'Alarm', 'is triggered', 'switch', 'test_switch'
)
data = entry.as_dict()
self.assertEqual('Alarm', data.get(logbook.ATTR_NAME))
self.assertEqual('is triggered', data.get(logbook.ATTR_MESSAGE))
self.assertEqual('switch', data.get(logbook.ATTR_DOMAIN))
self.assertEqual('test_switch', data.get(logbook.ATTR_ENTITY_ID))
2015-05-01 04:47:20 +00:00
def test_home_assistant_start_stop_grouped(self):
2016-03-09 09:25:50 +00:00
"""Test if HA start and stop events are grouped.
Events that are occuring in the same minute.
"""
2015-05-01 04:47:20 +00:00
entries = list(logbook.humanify((
ha.Event(EVENT_HOMEASSISTANT_STOP),
ha.Event(EVENT_HOMEASSISTANT_START),
)))
self.assertEqual(1, len(entries))
self.assert_entry(
entries[0], name='Home Assistant', message='restarted',
domain=ha.DOMAIN)
2015-09-14 01:30:44 +00:00
def test_process_custom_logbook_entries(self):
2016-03-09 09:25:50 +00:00
"""Test if custom log book entries get added as an entry."""
2015-09-14 01:30:44 +00:00
name = 'Nice name'
message = 'has a custom entry'
entity_id = 'sun.sun'
entries = list(logbook.humanify((
ha.Event(logbook.EVENT_LOGBOOK_ENTRY, {
logbook.ATTR_NAME: name,
logbook.ATTR_MESSAGE: message,
logbook.ATTR_ENTITY_ID: entity_id,
}),
)))
self.assertEqual(1, len(entries))
self.assert_entry(
entries[0], name=name, message=message,
domain='sun', entity_id=entity_id)
2015-05-01 04:47:20 +00:00
def assert_entry(self, entry, when=None, name=None, message=None,
domain=None, entity_id=None):
2016-03-09 09:25:50 +00:00
"""Assert an entry is what is expected."""
2015-05-01 04:47:20 +00:00
if when:
self.assertEqual(when, entry.when)
if name:
self.assertEqual(name, entry.name)
if message:
self.assertEqual(message, entry.message)
if domain:
self.assertEqual(domain, entry.domain)
if entity_id:
self.assertEqual(entity_id, entry.entity_id)
def create_state_changed_event(self, event_time_fired, entity_id, state):
2016-03-09 09:25:50 +00:00
"""Create state changed event."""
2015-05-01 04:47:20 +00:00
# Logbook only cares about state change events that
# contain an old state but will not actually act on it.
state = ha.State(entity_id, state).as_dict()
return ha.Event(EVENT_STATE_CHANGED, {
'entity_id': entity_id,
'old_state': state,
'new_state': state,
}, time_fired=event_time_fired)