Logbook context (#16937)
* Convert logbook to use attr * Add context to events * Enhance logbook * Lint * Fix logbook entry * Don't use intermediary classes for logbook entries
This commit is contained in:
1 parent
2e6346ca43
commit
fbc1c41673
4 files changed
+133
-80
No files matched your search
@@ -11,6 +11,7 @@ from homeassistant.const import (
|
||||
ATTR_HIDDEN, STATE_NOT_HOME, STATE_ON, STATE_OFF)
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components import logbook, recorder
|
||||
from homeassistant.components.alexa.smart_home import EVENT_ALEXA_SMART_HOME
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
|
||||
from tests.common import (
|
||||
@@ -99,7 +100,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
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)))
|
||||
entries = list(logbook.humanify(self.hass, (eventA, eventB, eventC)))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -116,7 +117,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
eventA = self.create_state_changed_event(
|
||||
pointA, entity_id, 10, attributes)
|
||||
|
||||
entries = list(logbook.humanify((eventA,)))
|
||||
entries = list(logbook.humanify(self.hass, (eventA,)))
|
||||
|
||||
self.assertEqual(0, len(entries))
|
||||
|
||||
@@ -133,7 +134,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
|
||||
events = logbook._exclude_events((ha.Event(EVENT_HOMEASSISTANT_STOP),
|
||||
eventA, eventB), {})
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -155,7 +156,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
|
||||
events = logbook._exclude_events((ha.Event(EVENT_HOMEASSISTANT_STOP),
|
||||
eventA, eventB), {})
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -177,7 +178,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
|
||||
events = logbook._exclude_events((ha.Event(EVENT_HOMEASSISTANT_STOP),
|
||||
eventA, eventB), {})
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -203,7 +204,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = logbook._exclude_events(
|
||||
(ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB),
|
||||
config[logbook.DOMAIN])
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -229,7 +230,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = logbook._exclude_events(
|
||||
(ha.Event(EVENT_HOMEASSISTANT_START), eventA, eventB),
|
||||
config[logbook.DOMAIN])
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(entries[0], name='Home Assistant', message='started',
|
||||
@@ -266,7 +267,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = logbook._exclude_events(
|
||||
(ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB),
|
||||
config[logbook.DOMAIN])
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -292,7 +293,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = logbook._exclude_events(
|
||||
(ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB),
|
||||
config[logbook.DOMAIN])
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(
|
||||
@@ -318,7 +319,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = logbook._exclude_events(
|
||||
(ha.Event(EVENT_HOMEASSISTANT_START), eventA, eventB),
|
||||
config[logbook.DOMAIN])
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(2, len(entries))
|
||||
self.assert_entry(entries[0], name='Home Assistant', message='started',
|
||||
@@ -352,7 +353,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = logbook._exclude_events(
|
||||
(ha.Event(EVENT_HOMEASSISTANT_START), eventA1, eventA2, eventA3,
|
||||
eventB1, eventB2), config[logbook.DOMAIN])
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(3, len(entries))
|
||||
self.assert_entry(entries[0], name='Home Assistant', message='started',
|
||||
@@ -373,7 +374,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
{'auto': True})
|
||||
|
||||
events = logbook._exclude_events((eventA, eventB), {})
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(1, len(entries))
|
||||
self.assert_entry(entries[0], pointA, 'bla', domain='switch',
|
||||
@@ -391,29 +392,18 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
pointA, entity_id2, 20, last_changed=pointA, last_updated=pointB)
|
||||
|
||||
events = logbook._exclude_events((eventA, eventB), {})
|
||||
entries = list(logbook.humanify(events))
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
self.assertEqual(1, len(entries))
|
||||
self.assert_entry(entries[0], pointA, 'bla', domain='switch',
|
||||
entity_id=entity_id)
|
||||
|
||||
def test_entry_to_dict(self):
|
||||
"""Test conversion of entry to dict."""
|
||||
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))
|
||||
|
||||
def test_home_assistant_start_stop_grouped(self):
|
||||
"""Test if HA start and stop events are grouped.
|
||||
|
||||
Events that are occurring in the same minute.
|
||||
"""
|
||||
entries = list(logbook.humanify((
|
||||
entries = list(logbook.humanify(self.hass, (
|
||||
ha.Event(EVENT_HOMEASSISTANT_STOP),
|
||||
ha.Event(EVENT_HOMEASSISTANT_START),
|
||||
)))
|
||||
@@ -428,7 +418,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
entity_id = 'switch.bla'
|
||||
pointA = dt_util.utcnow()
|
||||
|
||||
entries = list(logbook.humanify((
|
||||
entries = list(logbook.humanify(self.hass, (
|
||||
ha.Event(EVENT_HOMEASSISTANT_START),
|
||||
self.create_state_changed_event(pointA, entity_id, 10)
|
||||
)))
|
||||
@@ -509,7 +499,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
message = 'has a custom entry'
|
||||
entity_id = 'sun.sun'
|
||||
|
||||
entries = list(logbook.humanify((
|
||||
entries = list(logbook.humanify(self.hass, (
|
||||
ha.Event(logbook.EVENT_LOGBOOK_ENTRY, {
|
||||
logbook.ATTR_NAME: name,
|
||||
logbook.ATTR_MESSAGE: message,
|
||||
@@ -526,19 +516,19 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
domain=None, entity_id=None):
|
||||
"""Assert an entry is what is expected."""
|
||||
if when:
|
||||
self.assertEqual(when, entry.when)
|
||||
self.assertEqual(when, entry['when'])
|
||||
|
||||
if name:
|
||||
self.assertEqual(name, entry.name)
|
||||
self.assertEqual(name, entry['name'])
|
||||
|
||||
if message:
|
||||
self.assertEqual(message, entry.message)
|
||||
self.assertEqual(message, entry['message'])
|
||||
|
||||
if domain:
|
||||
self.assertEqual(domain, entry.domain)
|
||||
self.assertEqual(domain, entry['domain'])
|
||||
|
||||
if entity_id:
|
||||
self.assertEqual(entity_id, entry.entity_id)
|
||||
self.assertEqual(entity_id, entry['entity_id'])
|
||||
|
||||
def create_state_changed_event(self, event_time_fired, entity_id, state,
|
||||
attributes=None, last_changed=None,
|
||||
@@ -566,3 +556,44 @@ async def test_logbook_view(hass, aiohttp_client):
|
||||
response = await client.get(
|
||||
'/api/logbook/{}'.format(dt_util.utcnow().isoformat()))
|
||||
assert response.status == 200
|
||||
|
||||
|
||||
async def test_humanify_alexa_event(hass):
|
||||
"""Test humanifying Alexa event."""
|
||||
hass.states.async_set('light.kitchen', 'on', {
|
||||
'friendly_name': 'Kitchen Light'
|
||||
})
|
||||
|
||||
results = list(logbook.humanify(hass, [
|
||||
ha.Event(EVENT_ALEXA_SMART_HOME, {
|
||||
'namespace': 'Alexa.Discovery',
|
||||
'name': 'Discover',
|
||||
}),
|
||||
ha.Event(EVENT_ALEXA_SMART_HOME, {
|
||||
'namespace': 'Alexa.PowerController',
|
||||
'name': 'TurnOn',
|
||||
'entity_id': 'light.kitchen'
|
||||
}),
|
||||
ha.Event(EVENT_ALEXA_SMART_HOME, {
|
||||
'namespace': 'Alexa.PowerController',
|
||||
'name': 'TurnOn',
|
||||
'entity_id': 'light.non_existing'
|
||||
}),
|
||||
|
||||
]))
|
||||
|
||||
event1, event2, event3 = results
|
||||
|
||||
assert event1['name'] == 'Amazon Alexa'
|
||||
assert event1['message'] == 'send command Alexa.Discovery/Discover'
|
||||
assert event1['entity_id'] is None
|
||||
|
||||
assert event2['name'] == 'Amazon Alexa'
|
||||
assert event2['message'] == \
|
||||
'send command Alexa.PowerController/TurnOn for Kitchen Light'
|
||||
assert event2['entity_id'] == 'light.kitchen'
|
||||
|
||||
assert event3['name'] == 'Amazon Alexa'
|
||||
assert event3['message'] == \
|
||||
'send command Alexa.PowerController/TurnOn for light.non_existing'
|
||||
assert event3['entity_id'] == 'light.non_existing'
|
||||
Reference in new issue
Block a user