* Add event loop to the core * Add block_till_done to HA core object * Fix some tests * Linting core * Fix statemachine tests * Core test fixes * fix block_till_done to wait for loop and queue to empty * fix test_core for passing, and correct start/stop/block_till_done * Fix remote tests * Fix tests: block_till_done * Fix linting * Fix more tests * Fix final linting * Fix remote test * remove unnecessary import * reduce sleep to avoid slowing down the tests excessively * fix remaining tests to wait for non-threadsafe operations * Add async_ doc strings for event loop / coroutine info * Fix command line test to block for the right timeout * Fix py3.4.2 loop var access * Fix SERVICE_CALL_LIMIT being in effect for other tests * Fix lint errors * Fix lint error with proper placement * Fix slave start to not start a timer * Add asyncio compatible listeners. * Increase min Python version to 3.4.2 * Move async backports to util * Add backported async tests * Fix linting * Simplify Python version check * Fix lint * Remove unneeded try/except and queue listener appproriately. * Fix tuple vs. list unorderable error on version compare. * Fix version tests
95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
"""The tests for the MQTT automation."""
|
|
import unittest
|
|
|
|
from homeassistant.bootstrap import _setup_component
|
|
import homeassistant.components.automation as automation
|
|
from tests.common import (
|
|
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant)
|
|
|
|
|
|
class TestAutomationMQTT(unittest.TestCase):
|
|
"""Test the event automation."""
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
"""Setup things to be run when tests are started."""
|
|
self.hass = get_test_home_assistant()
|
|
self.hass.config.components.append('group')
|
|
mock_mqtt_component(self.hass)
|
|
self.calls = []
|
|
|
|
def record_call(service):
|
|
self.calls.append(service)
|
|
|
|
self.hass.services.register('test', 'automation', record_call)
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
"""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'
|
|
},
|
|
'action': {
|
|
'service': 'test.automation',
|
|
'data_template': {
|
|
'some': '{{ trigger.platform }} - {{ trigger.topic }}'
|
|
' - {{ trigger.payload }}'
|
|
},
|
|
}
|
|
}
|
|
})
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
|
|
self.hass.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
self.assertEqual('mqtt - test-topic - test_payload',
|
|
self.calls[0].data['some'])
|
|
|
|
automation.turn_off(self.hass)
|
|
self.hass.block_till_done()
|
|
fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
|
|
self.hass.block_till_done()
|
|
self.assertEqual(1, len(self.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'
|
|
}
|
|
}
|
|
})
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'hello')
|
|
self.hass.block_till_done()
|
|
self.assertEqual(1, len(self.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'
|
|
}
|
|
}
|
|
})
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'no-hello')
|
|
self.hass.block_till_done()
|
|
self.assertEqual(0, len(self.calls))
|