core/tests/components/automation/test_mqtt.py

102 lines
3.4 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the MQTT automation."""
2015-08-11 06:11:46 +00:00
import unittest
from homeassistant.core import callback
from homeassistant.setup import setup_component
2015-08-11 06:11:46 +00:00
import homeassistant.components.automation as automation
2016-02-14 23:08:23 +00:00
from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant,
mock_component)
2015-08-11 06:11:46 +00:00
# pylint: disable=invalid-name
2015-10-03 06:57:26 +00:00
class TestAutomationMQTT(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the event automation."""
2015-08-11 06:11:46 +00:00
def setUp(self):
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
mock_component(self.hass, 'group')
2015-08-11 06:11:46 +00:00
mock_mqtt_component(self.hass)
self.calls = []
@callback
2015-08-11 06:11:46 +00:00
def record_call(service):
"""Helper to record calls."""
2015-08-11 06:11:46 +00:00
self.calls.append(service)
self.hass.services.register('test', 'automation', record_call)
def tearDown(self):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2015-08-11 06:11:46 +00:00
self.hass.stop()
def test_if_fires_on_topic_match(self):
2016-03-09 09:25:50 +00:00
"""Test if message is fired on topic match."""
assert setup_component(self.hass, automation.DOMAIN, {
2015-08-11 06:11:46 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'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-15 05:05:40 +00:00
}
2015-08-11 06:11:46 +00:00
}
})
2015-08-11 06:11:46 +00:00
fire_mqtt_message(self.hass, 'test-topic', '{ "hello": "world" }')
self.hass.block_till_done()
2015-08-11 06:11:46 +00:00
self.assertEqual(1, len(self.calls))
self.assertEqual('mqtt - test-topic - { "hello": "world" } - world',
self.calls[0].data['some'])
2015-08-11 06:11:46 +00:00
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))
2015-08-11 06:11:46 +00:00
def test_if_fires_on_topic_and_payload_match(self):
2016-03-09 09:25:50 +00:00
"""Test if message is fired on topic and payload match."""
assert setup_component(self.hass, automation.DOMAIN, {
2015-08-11 06:11:46 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'trigger': {
'platform': 'mqtt',
'topic': 'test-topic',
'payload': 'hello'
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
2015-09-15 05:05:40 +00:00
}
2015-08-11 06:11:46 +00:00
}
})
2015-08-11 06:11:46 +00:00
fire_mqtt_message(self.hass, 'test-topic', 'hello')
self.hass.block_till_done()
2015-08-11 06:11:46 +00:00
self.assertEqual(1, len(self.calls))
def test_if_not_fires_on_topic_but_no_payload_match(self):
2016-03-09 09:25:50 +00:00
"""Test if message is not fired on topic but no payload."""
assert setup_component(self.hass, automation.DOMAIN, {
2015-08-11 06:11:46 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'trigger': {
'platform': 'mqtt',
'topic': 'test-topic',
'payload': 'hello'
},
'action': {
2015-09-19 15:43:56 +00:00
'service': 'test.automation'
2015-09-15 05:05:40 +00:00
}
2015-08-11 06:11:46 +00:00
}
})
2015-08-11 06:11:46 +00:00
fire_mqtt_message(self.hass, 'test-topic', 'no-hello')
self.hass.block_till_done()
2015-08-11 06:11:46 +00:00
self.assertEqual(0, len(self.calls))