Significantly reduce overhead to filter event triggers (#99376)

* fast

* cleanups

* cleanups

* cleanups

* comment

* comment

* add more cover

* comment

* pull more examples from forums to validate cover
This commit is contained in:
J. Nick Koston 2023-08-31 13:47:01 -04:00 committed by GitHub
parent 2e7018a152
commit 0da94c20b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 138 additions and 21 deletions

View file

@ -288,7 +288,11 @@ async def test_if_fires_on_event_with_empty_data_and_context_config(
async def test_if_fires_on_event_with_nested_data(hass: HomeAssistant, calls) -> None:
"""Test the firing of events with nested data."""
"""Test the firing of events with nested data.
This test exercises the slow path of using vol.Schema to validate
matching event data.
"""
assert await async_setup_component(
hass,
automation.DOMAIN,
@ -311,6 +315,87 @@ async def test_if_fires_on_event_with_nested_data(hass: HomeAssistant, calls) ->
assert len(calls) == 1
async def test_if_fires_on_event_with_empty_data(hass: HomeAssistant, calls) -> None:
"""Test the firing of events with empty data.
This test exercises the fast path to validate matching event data.
"""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "event",
"event_type": "test_event",
"event_data": {},
},
"action": {"service": "test.automation"},
}
},
)
hass.bus.async_fire("test_event", {"any_attr": {}})
await hass.async_block_till_done()
assert len(calls) == 1
async def test_if_fires_on_sample_zha_event(hass: HomeAssistant, calls) -> None:
"""Test the firing of events with a sample zha event.
This test exercises the fast path to validate matching event data.
"""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "event",
"event_type": "zha_event",
"event_data": {
"device_ieee": "00:15:8d:00:02:93:04:11",
"command": "attribute_updated",
"args": {
"attribute_id": 0,
"attribute_name": "on_off",
"value": True,
},
},
},
"action": {"service": "test.automation"},
}
},
)
hass.bus.async_fire(
"zha_event",
{
"device_ieee": "00:15:8d:00:02:93:04:11",
"unique_id": "00:15:8d:00:02:93:04:11:1:0x0006",
"endpoint_id": 1,
"cluster_id": 6,
"command": "attribute_updated",
"args": {"attribute_id": 0, "attribute_name": "on_off", "value": True},
},
)
await hass.async_block_till_done()
assert len(calls) == 1
hass.bus.async_fire(
"zha_event",
{
"device_ieee": "00:15:8d:00:02:93:04:11",
"unique_id": "00:15:8d:00:02:93:04:11:1:0x0006",
"endpoint_id": 1,
"cluster_id": 6,
"command": "attribute_updated",
"args": {"attribute_id": 0, "attribute_name": "on_off", "value": False},
},
)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_if_not_fires_if_event_data_not_matches(
hass: HomeAssistant, calls
) -> None:
@ -362,7 +447,11 @@ async def test_if_not_fires_if_event_context_not_matches(
async def test_if_fires_on_multiple_user_ids(
hass: HomeAssistant, calls, context_with_user
) -> None:
"""Test the firing of event when the trigger has multiple user ids."""
"""Test the firing of event when the trigger has multiple user ids.
This test exercises the slow path of using vol.Schema to validate
matching event context.
"""
assert await async_setup_component(
hass,
automation.DOMAIN,