mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Convert automation tests to async (#17794)
* Convert automation tests to async * Fix 8 last tests * Lint
This commit is contained in:
@@ -1,175 +1,172 @@
|
||||
"""The tests for the Event automation."""
|
||||
import unittest
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
from tests.common import get_test_home_assistant, mock_component
|
||||
from tests.common import mock_component
|
||||
from tests.components.automation import common
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
class TestAutomationEvent(unittest.TestCase):
|
||||
"""Test the event automation."""
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock serivce."""
|
||||
return async_mock_service(hass, 'test', 'automation')
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
mock_component(self.hass, 'group')
|
||||
self.calls = []
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Record the call."""
|
||||
self.calls.append(service)
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, 'group')
|
||||
|
||||
self.hass.services.register('test', 'automation', record_call)
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
async def test_if_fires_on_event(hass, calls):
|
||||
"""Test the firing of events."""
|
||||
context = Context()
|
||||
|
||||
def test_if_fires_on_event(self):
|
||||
"""Test the firing of events."""
|
||||
context = Context()
|
||||
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event', context=context)
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
assert self.calls[0].context is context
|
||||
hass.bus.async_fire('test_event', context=context)
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
assert calls[0].context is context
|
||||
|
||||
common.turn_off(self.hass)
|
||||
self.hass.block_till_done()
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_if_fires_on_event_extra_data(self):
|
||||
"""Test the firing of events still matches with event data."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
|
||||
async def test_if_fires_on_event_extra_data(hass, calls):
|
||||
"""Test the firing of events still matches with event data."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event', {'extra_key': 'extra_data'})
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
hass.bus.async_fire('test_event', {'extra_key': 'extra_data'})
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
common.turn_off(self.hass)
|
||||
self.hass.block_till_done()
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_if_fires_on_event_with_data(self):
|
||||
"""Test the firing of events with data."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {'some_attr': 'some_value'}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
|
||||
async def test_if_fires_on_event_with_data(hass, calls):
|
||||
"""Test the firing of events with data."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {'some_attr': 'some_value'}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event', {'some_attr': 'some_value',
|
||||
'another': 'value'})
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
hass.bus.async_fire('test_event', {'some_attr': 'some_value',
|
||||
'another': 'value'})
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_if_fires_on_event_with_empty_data_config(self):
|
||||
"""Test the firing of events with empty data config.
|
||||
|
||||
The frontend automation editor can produce configurations with an
|
||||
empty dict for event_data instead of no key.
|
||||
"""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
async def test_if_fires_on_event_with_empty_data_config(hass, calls):
|
||||
"""Test the firing of events with empty data config.
|
||||
|
||||
The frontend automation editor can produce configurations with an
|
||||
empty dict for event_data instead of no key.
|
||||
"""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event', {'some_attr': 'some_value',
|
||||
'another': 'value'})
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
hass.bus.async_fire('test_event', {'some_attr': 'some_value',
|
||||
'another': 'value'})
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_if_fires_on_event_with_nested_data(self):
|
||||
"""Test the firing of events with nested data."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {
|
||||
'parent_attr': {
|
||||
'some_attr': 'some_value'
|
||||
}
|
||||
|
||||
async def test_if_fires_on_event_with_nested_data(hass, calls):
|
||||
"""Test the firing of events with nested data."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {
|
||||
'parent_attr': {
|
||||
'some_attr': 'some_value'
|
||||
}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event', {
|
||||
'parent_attr': {
|
||||
'some_attr': 'some_value',
|
||||
'another': 'value'
|
||||
hass.bus.async_fire('test_event', {
|
||||
'parent_attr': {
|
||||
'some_attr': 'some_value',
|
||||
'another': 'value'
|
||||
}
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_not_fires_if_event_data_not_matches(hass, calls):
|
||||
"""Test firing of event if no match."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {'some_attr': 'some_value'}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
}
|
||||
})
|
||||
|
||||
def test_if_not_fires_if_event_data_not_matches(self):
|
||||
"""Test firing of event if no match."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
'event_data': {'some_attr': 'some_value'}
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event', {'some_attr': 'some_other_value'})
|
||||
self.hass.block_till_done()
|
||||
assert 0 == len(self.calls)
|
||||
hass.bus.async_fire('test_event', {'some_attr': 'some_other_value'})
|
||||
await hass.async_block_till_done()
|
||||
assert 0 == len(calls)
|
||||
|
||||
Reference in New Issue
Block a user