Allow teaching logbook about events (#32444)
* Allow teaching logbook about events * Use async_add_executor_job * Fix tests
This commit is contained in:
1 parent
7c51318861
commit
8aea538662
10 files changed
+175
-105
No files matched your search
@@ -0,0 +1,63 @@
|
||||
"""Tests for alexa."""
|
||||
from homeassistant.components import logbook
|
||||
from homeassistant.components.alexa.const import EVENT_ALEXA_SMART_HOME
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
async def test_humanify_alexa_event(hass):
|
||||
"""Test humanifying Alexa event."""
|
||||
await async_setup_component(hass, "alexa", {})
|
||||
hass.states.async_set("light.kitchen", "on", {"friendly_name": "Kitchen Light"})
|
||||
|
||||
results = list(
|
||||
logbook.humanify(
|
||||
hass,
|
||||
[
|
||||
ha.Event(
|
||||
EVENT_ALEXA_SMART_HOME,
|
||||
{"request": {"namespace": "Alexa.Discovery", "name": "Discover"}},
|
||||
),
|
||||
ha.Event(
|
||||
EVENT_ALEXA_SMART_HOME,
|
||||
{
|
||||
"request": {
|
||||
"namespace": "Alexa.PowerController",
|
||||
"name": "TurnOn",
|
||||
"entity_id": "light.kitchen",
|
||||
}
|
||||
},
|
||||
),
|
||||
ha.Event(
|
||||
EVENT_ALEXA_SMART_HOME,
|
||||
{
|
||||
"request": {
|
||||
"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"
|
||||
@@ -37,7 +37,8 @@ def alexa_client(loop, hass, hass_client):
|
||||
alexa.DOMAIN,
|
||||
{
|
||||
# Key is here to verify we allow other keys in config too
|
||||
"homeassistant": {}
|
||||
"homeassistant": {},
|
||||
"alexa": {},
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ from datetime import datetime, timedelta
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from asynctest import patch
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -169,7 +170,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -196,7 +197,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -224,7 +225,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -258,7 +259,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -300,7 +301,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
eventA,
|
||||
eventB,
|
||||
)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -341,7 +342,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -380,7 +381,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -412,7 +413,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
events = [
|
||||
e
|
||||
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -426,6 +427,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
|
||||
def test_include_events_domain(self):
|
||||
"""Test if events are filtered if domain is included in config."""
|
||||
assert setup_component(self.hass, "alexa", {})
|
||||
entity_id = "switch.bla"
|
||||
entity_id2 = "sensor.blu"
|
||||
pointA = dt_util.utcnow()
|
||||
@@ -467,7 +469,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
eventA,
|
||||
eventB,
|
||||
)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -521,7 +523,7 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
eventB1,
|
||||
eventB2,
|
||||
)
|
||||
if logbook._keep_event(e, entities_filter)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -553,7 +555,9 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
|
||||
entities_filter = logbook._generate_filter_from_config({})
|
||||
events = [
|
||||
e for e in (eventA, eventB) if logbook._keep_event(e, entities_filter)
|
||||
e
|
||||
for e in (eventA, eventB)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -576,7 +580,9 @@ class TestComponentLogbook(unittest.TestCase):
|
||||
|
||||
entities_filter = logbook._generate_filter_from_config({})
|
||||
events = [
|
||||
e for e in (eventA, eventB) if logbook._keep_event(e, entities_filter)
|
||||
e
|
||||
for e in (eventA, eventB)
|
||||
if logbook._keep_event(self.hass, e, entities_filter)
|
||||
]
|
||||
entries = list(logbook.humanify(self.hass, events))
|
||||
|
||||
@@ -1333,63 +1339,6 @@ async def test_logbook_view_period_entity(hass, hass_client):
|
||||
assert json[0]["entity_id"] == entity_id_test
|
||||
|
||||
|
||||
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,
|
||||
{"request": {"namespace": "Alexa.Discovery", "name": "Discover"}},
|
||||
),
|
||||
ha.Event(
|
||||
EVENT_ALEXA_SMART_HOME,
|
||||
{
|
||||
"request": {
|
||||
"namespace": "Alexa.PowerController",
|
||||
"name": "TurnOn",
|
||||
"entity_id": "light.kitchen",
|
||||
}
|
||||
},
|
||||
),
|
||||
ha.Event(
|
||||
EVENT_ALEXA_SMART_HOME,
|
||||
{
|
||||
"request": {
|
||||
"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"
|
||||
|
||||
|
||||
async def test_humanify_homekit_changed_event(hass):
|
||||
"""Test humanifying HomeKit changed event."""
|
||||
event1, event2 = list(
|
||||
@@ -1517,3 +1466,33 @@ async def test_humanify_same_state(hass):
|
||||
)
|
||||
|
||||
assert len(events) == 1
|
||||
|
||||
|
||||
async def test_logbook_describe_event(hass, hass_client):
|
||||
"""Test teaching logbook about a new event."""
|
||||
await hass.async_add_executor_job(init_recorder_component, hass)
|
||||
assert await async_setup_component(hass, "logbook", {})
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=dt_util.utcnow() - timedelta(seconds=5),
|
||||
):
|
||||
hass.bus.async_fire("some_event")
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_add_executor_job(
|
||||
hass.data[recorder.DATA_INSTANCE].block_till_done
|
||||
)
|
||||
|
||||
def _describe(event):
|
||||
"""Describe an event."""
|
||||
return {"name": "Test Name", "message": "tested a message"}
|
||||
|
||||
hass.components.logbook.async_describe_event("test_domain", "some_event", _describe)
|
||||
|
||||
client = await hass_client()
|
||||
response = await client.get("/api/logbook")
|
||||
results = await response.json()
|
||||
assert len(results) == 1
|
||||
event = results[0]
|
||||
assert event["name"] == "Test Name"
|
||||
assert event["message"] == "tested a message"
|
||||
assert event["domain"] == "test_domain"
|
||||
Reference in new issue
Block a user