Convert automation tests to async (#17794)
* Convert automation tests to async * Fix 8 last tests * Lint
This commit is contained in:
parent
3f4798b5c3
commit
e276e899cf
11 changed files with 3544 additions and 3514 deletions
|
@ -1,102 +1,94 @@
|
|||
"""The tests for the MQTT automation."""
|
||||
import unittest
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
from tests.common import (
|
||||
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant,
|
||||
mock_component)
|
||||
async_fire_mqtt_message,
|
||||
mock_component, async_mock_service, async_mock_mqtt_component)
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
class TestAutomationMQTT(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')
|
||||
mock_mqtt_component(self.hass)
|
||||
self.calls = []
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Record calls."""
|
||||
self.calls.append(service)
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, 'group')
|
||||
hass.loop.run_until_complete(async_mock_mqtt_component(hass))
|
||||
|
||||
self.hass.services.register('test', 'automation', record_call)
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_if_fires_on_topic_match(self):
|
||||
"""Test if message is fired on topic match."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'mqtt',
|
||||
'topic': 'test-topic'
|
||||
async def test_if_fires_on_topic_match(hass, calls):
|
||||
"""Test if message is fired on topic match."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'mqtt',
|
||||
'topic': 'test-topic'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'some': '{{ trigger.platform }} - {{ trigger.topic }}'
|
||||
' - {{ trigger.payload }} - '
|
||||
'{{ trigger.payload_json.hello }}'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'some': '{{ trigger.platform }} - {{ trigger.topic }}'
|
||||
' - {{ trigger.payload }} - '
|
||||
'{{ trigger.payload_json.hello }}'
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', '{ "hello": "world" }')
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
assert 'mqtt - test-topic - { "hello": "world" } - world' == \
|
||||
self.calls[0].data['some']
|
||||
async_fire_mqtt_message(hass, 'test-topic', '{ "hello": "world" }')
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
assert 'mqtt - test-topic - { "hello": "world" } - world' == \
|
||||
calls[0].data['some']
|
||||
|
||||
common.turn_off(self.hass)
|
||||
self.hass.block_till_done()
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, 'test-topic', 'test_payload')
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_if_fires_on_topic_and_payload_match(self):
|
||||
"""Test if message is fired on topic and payload match."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'mqtt',
|
||||
'topic': 'test-topic',
|
||||
'payload': 'hello'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
|
||||
async def test_if_fires_on_topic_and_payload_match(hass, calls):
|
||||
"""Test if message is fired on topic and payload match."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'mqtt',
|
||||
'topic': 'test-topic',
|
||||
'payload': 'hello'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'hello')
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
async_fire_mqtt_message(hass, 'test-topic', 'hello')
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
def test_if_not_fires_on_topic_but_no_payload_match(self):
|
||||
"""Test if message is not fired on topic but no payload."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'mqtt',
|
||||
'topic': 'test-topic',
|
||||
'payload': 'hello'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
|
||||
async def test_if_not_fires_on_topic_but_no_payload_match(hass, calls):
|
||||
"""Test if message is not fired on topic but no payload."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'mqtt',
|
||||
'topic': 'test-topic',
|
||||
'payload': 'hello'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'no-hello')
|
||||
self.hass.block_till_done()
|
||||
assert 0 == len(self.calls)
|
||||
async_fire_mqtt_message(hass, 'test-topic', 'no-hello')
|
||||
await hass.async_block_till_done()
|
||||
assert 0 == len(calls)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue