Add context to event trigger (#40932)

This commit is contained in:
On Freund 2020-10-01 12:59:35 +03:00 committed by GitHub
parent 78ebd1add9
commit 04f87eedf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 26 deletions

View file

@ -15,6 +15,12 @@ def calls(hass):
return async_mock_service(hass, "test", "automation")
@pytest.fixture
def context_with_user():
"""Track calls to a mock service."""
return Context(user_id="test_user_id")
@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
@ -53,8 +59,8 @@ async def test_if_fires_on_event(hass, calls):
assert len(calls) == 1
async def test_if_fires_on_event_extra_data(hass, calls):
"""Test the firing of events still matches with event data."""
async def test_if_fires_on_event_extra_data(hass, calls, context_with_user):
"""Test the firing of events still matches with event data and context."""
assert await async_setup_component(
hass,
automation.DOMAIN,
@ -65,8 +71,9 @@ async def test_if_fires_on_event_extra_data(hass, calls):
}
},
)
hass.bus.async_fire("test_event", {"extra_key": "extra_data"})
hass.bus.async_fire(
"test_event", {"extra_key": "extra_data"}, context=context_with_user
)
await hass.async_block_till_done()
assert len(calls) == 1
@ -82,8 +89,8 @@ async def test_if_fires_on_event_extra_data(hass, calls):
assert len(calls) == 1
async def test_if_fires_on_event_with_data(hass, calls):
"""Test the firing of events with data."""
async def test_if_fires_on_event_with_data_and_context(hass, calls, context_with_user):
"""Test the firing of events with data and context."""
assert await async_setup_component(
hass,
automation.DOMAIN,
@ -96,6 +103,7 @@ async def test_if_fires_on_event_with_data(hass, calls):
"some_attr": "some_value",
"second_attr": "second_value",
},
"context": {"user_id": context_with_user.user_id},
},
"action": {"service": "test.automation"},
}
@ -105,17 +113,31 @@ async def test_if_fires_on_event_with_data(hass, calls):
hass.bus.async_fire(
"test_event",
{"some_attr": "some_value", "another": "value", "second_attr": "second_value"},
context=context_with_user,
)
await hass.async_block_till_done()
assert len(calls) == 1
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
hass.bus.async_fire(
"test_event",
{"some_attr": "some_value", "another": "value"},
context=context_with_user,
)
await hass.async_block_till_done()
assert len(calls) == 1 # No new call
hass.bus.async_fire(
"test_event",
{"some_attr": "some_value", "another": "value", "second_attr": "second_value"},
)
await hass.async_block_till_done()
assert len(calls) == 1
async def test_if_fires_on_event_with_empty_data_config(hass, calls):
"""Test the firing of events with empty data config.
async def test_if_fires_on_event_with_empty_data_and_context_config(
hass, calls, context_with_user
):
"""Test the firing of events with empty data and context config.
The frontend automation editor can produce configurations with an
empty dict for event_data instead of no key.
@ -129,13 +151,18 @@ async def test_if_fires_on_event_with_empty_data_config(hass, calls):
"platform": "event",
"event_type": "test_event",
"event_data": {},
"context": {},
},
"action": {"service": "test.automation"},
}
},
)
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
hass.bus.async_fire(
"test_event",
{"some_attr": "some_value", "another": "value"},
context=context_with_user,
)
await hass.async_block_till_done()
assert len(calls) == 1
@ -165,7 +192,7 @@ async def test_if_fires_on_event_with_nested_data(hass, calls):
async def test_if_not_fires_if_event_data_not_matches(hass, calls):
"""Test firing of event if no match."""
"""Test firing of event if no data match."""
assert await async_setup_component(
hass,
automation.DOMAIN,
@ -184,3 +211,27 @@ async def test_if_not_fires_if_event_data_not_matches(hass, calls):
hass.bus.async_fire("test_event", {"some_attr": "some_other_value"})
await hass.async_block_till_done()
assert len(calls) == 0
async def test_if_not_fires_if_event_context_not_matches(
hass, calls, context_with_user
):
"""Test firing of event if no context match."""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"trigger": {
"platform": "event",
"event_type": "test_event",
"context": {"user_id": "some_user"},
},
"action": {"service": "test.automation"},
}
},
)
hass.bus.async_fire("test_event", {}, context=context_with_user)
await hass.async_block_till_done()
assert len(calls) == 0