Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
481 changes: 225 additions & 256 deletions homeassistant/components/logbook/__init__.py

Large diffs are not rendered by default.

52 changes: 0 additions & 52 deletions homeassistant/scripts/benchmark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.helpers.entityfilter import convert_include_exclude_filter
from homeassistant.helpers.json import JSONEncoder
from homeassistant.util import dt as dt_util

# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs
# mypy: no-warn-return-any
Expand Down Expand Up @@ -224,57 +223,6 @@ def listener(*args):
return timer() - start


@benchmark
async def logbook_filtering_state(hass):
"""Filter state changes."""
return await _logbook_filtering(hass, 1, 1)


@benchmark
async def logbook_filtering_attributes(hass):
"""Filter attribute changes."""
return await _logbook_filtering(hass, 1, 2)


@benchmark
async def _logbook_filtering(hass, last_changed, last_updated):
# pylint: disable=import-outside-toplevel
from homeassistant.components import logbook

entity_id = "test.entity"

old_state = {"entity_id": entity_id, "state": "off"}

new_state = {
"entity_id": entity_id,
"state": "on",
"last_updated": last_updated,
"last_changed": last_changed,
}

event = _create_state_changed_event_from_old_new(
entity_id, dt_util.utcnow(), old_state, new_state
)

entity_attr_cache = logbook.EntityAttributeCache(hass)

entities_filter = convert_include_exclude_filter(
logbook.INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA({})
)

def yield_events(event):
for _ in range(10**5):
# pylint: disable=protected-access
if logbook._keep_event(hass, event, entities_filter):
yield event

start = timer()

list(logbook.humanify(hass, yield_events(event), entity_attr_cache, {}))

return timer() - start


@benchmark
async def filtering_entity_id(hass):
"""Run a 100k state changes through entity filter."""
Expand Down
64 changes: 29 additions & 35 deletions tests/components/alexa/test_init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""Tests for alexa."""
from homeassistant.components import logbook
from homeassistant.components.alexa.const import EVENT_ALEXA_SMART_HOME
from homeassistant.setup import async_setup_component

from tests.components.logbook.test_init import MockLazyEventPartialState
from tests.components.logbook.common import MockRow, mock_humanify


async def test_humanify_alexa_event(hass):
Expand All @@ -12,40 +11,35 @@ async def test_humanify_alexa_event(hass):
await async_setup_component(hass, "alexa", {})
await async_setup_component(hass, "logbook", {})
hass.states.async_set("light.kitchen", "on", {"friendly_name": "Kitchen Light"})
entity_attr_cache = logbook.EntityAttributeCache(hass)

results = list(
logbook.humanify(
hass,
[
MockLazyEventPartialState(
EVENT_ALEXA_SMART_HOME,
{"request": {"namespace": "Alexa.Discovery", "name": "Discover"}},
),
MockLazyEventPartialState(
EVENT_ALEXA_SMART_HOME,
{
"request": {
"namespace": "Alexa.PowerController",
"name": "TurnOn",
"entity_id": "light.kitchen",
}
},
),
MockLazyEventPartialState(
EVENT_ALEXA_SMART_HOME,
{
"request": {
"namespace": "Alexa.PowerController",
"name": "TurnOn",
"entity_id": "light.non_existing",
}
},
),
],
entity_attr_cache,
{},
)
results = mock_humanify(
hass,
[
MockRow(
EVENT_ALEXA_SMART_HOME,
{"request": {"namespace": "Alexa.Discovery", "name": "Discover"}},
),
MockRow(
EVENT_ALEXA_SMART_HOME,
{
"request": {
"namespace": "Alexa.PowerController",
"name": "TurnOn",
"entity_id": "light.kitchen",
}
},
),
MockRow(
EVENT_ALEXA_SMART_HOME,
{
"request": {
"namespace": "Alexa.PowerController",
"name": "TurnOn",
"entity_id": "light.non_existing",
}
},
),
],
)

event1, event2, event3 = results
Expand Down
40 changes: 17 additions & 23 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import pytest

from homeassistant.components import logbook
import homeassistant.components.automation as automation
from homeassistant.components.automation import (
ATTR_SOURCE,
Expand Down Expand Up @@ -53,7 +52,7 @@
async_mock_service,
mock_restore_cache,
)
from tests.components.logbook.test_init import MockLazyEventPartialState
from tests.components.logbook.common import MockRow, mock_humanify


@pytest.fixture
Expand Down Expand Up @@ -1223,28 +1222,23 @@ async def test_logbook_humanify_automation_triggered_event(hass):
hass.config.components.add("recorder")
await async_setup_component(hass, automation.DOMAIN, {})
await async_setup_component(hass, "logbook", {})
entity_attr_cache = logbook.EntityAttributeCache(hass)

event1, event2 = list(
logbook.humanify(
hass,
[
MockLazyEventPartialState(
EVENT_AUTOMATION_TRIGGERED,
{ATTR_ENTITY_ID: "automation.hello", ATTR_NAME: "Hello Automation"},
),
MockLazyEventPartialState(
EVENT_AUTOMATION_TRIGGERED,
{
ATTR_ENTITY_ID: "automation.bye",
ATTR_NAME: "Bye Automation",
ATTR_SOURCE: "source of trigger",
},
),
],
entity_attr_cache,
{},
)
event1, event2 = mock_humanify(
hass,
[
MockRow(
EVENT_AUTOMATION_TRIGGERED,
{ATTR_ENTITY_ID: "automation.hello", ATTR_NAME: "Hello Automation"},
),
MockRow(
EVENT_AUTOMATION_TRIGGERED,
{
ATTR_ENTITY_ID: "automation.bye",
ATTR_NAME: "Bye Automation",
ATTR_SOURCE: "source of trigger",
},
),
],
)

assert event1["name"] == "Hello Automation"
Expand Down
51 changes: 23 additions & 28 deletions tests/components/automation/test_logbook.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
"""Test automation logbook."""
from homeassistant.components import automation, logbook
from homeassistant.components import automation
from homeassistant.core import Context
from homeassistant.setup import async_setup_component

from tests.components.logbook.test_init import MockLazyEventPartialState
from tests.components.logbook.common import MockRow, mock_humanify


async def test_humanify_automation_trigger_event(hass):
"""Test humanifying Shelly click event."""
hass.config.components.add("recorder")
assert await async_setup_component(hass, "automation", {})
assert await async_setup_component(hass, "logbook", {})
entity_attr_cache = logbook.EntityAttributeCache(hass)
context = Context()

event1, event2 = list(
logbook.humanify(
hass,
[
MockLazyEventPartialState(
automation.EVENT_AUTOMATION_TRIGGERED,
{
"name": "Bla",
"entity_id": "automation.bla",
"source": "state change of input_boolean.yo",
},
context=context,
),
MockLazyEventPartialState(
automation.EVENT_AUTOMATION_TRIGGERED,
{
"name": "Bla",
"entity_id": "automation.bla",
},
context=context,
),
],
entity_attr_cache,
{},
)
event1, event2 = mock_humanify(
hass,
[
MockRow(
automation.EVENT_AUTOMATION_TRIGGERED,
{
"name": "Bla",
"entity_id": "automation.bla",
"source": "state change of input_boolean.yo",
},
context=context,
),
MockRow(
automation.EVENT_AUTOMATION_TRIGGERED,
{
"name": "Bla",
"entity_id": "automation.bla",
},
context=context,
),
],
)

assert event1["name"] == "Bla"
Expand Down
Loading