hass-core/tests/components/automation/test_mqtt.py

95 lines
2.9 KiB
Python
Raw Normal View History

2016-03-09 10:25:50 +01:00
"""The tests for the MQTT automation."""
import pytest
2015-08-10 23:11:46 -07:00
from homeassistant.setup import async_setup_component
2015-08-10 23:11:46 -07:00
import homeassistant.components.automation as automation
2016-02-14 15:08:23 -08:00
from tests.common import (
async_fire_mqtt_message,
mock_component, async_mock_service, async_mock_mqtt_component)
from tests.components.automation import common
2015-08-10 23:11:46 -07:00
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
return async_mock_service(hass, 'test', 'automation')
2015-08-10 23:11:46 -07:00
@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
mock_component(hass, 'group')
hass.loop.run_until_complete(async_mock_mqtt_component(hass))
2015-08-10 23:11:46 -07:00
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 }}'
2015-09-14 22:05:40 -07:00
},
2015-08-10 23:11:46 -07:00
}
}
})
2015-08-10 23:11:46 -07:00
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']
2015-08-10 23:11:46 -07:00
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)
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'
2015-08-10 23:11:46 -07:00
}
}
})
2015-08-10 23:11:46 -07:00
async_fire_mqtt_message(hass, 'test-topic', 'hello')
await hass.async_block_till_done()
assert 1 == len(calls)
2015-08-10 23:11:46 -07:00
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'
2015-08-10 23:11:46 -07:00
}
}
})
2015-08-10 23:11:46 -07:00
async_fire_mqtt_message(hass, 'test-topic', 'no-hello')
await hass.async_block_till_done()
assert 0 == len(calls)