2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the MQTT automation."""
|
2015-08-11 06:11:46 +00:00
|
|
|
import unittest
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
from homeassistant.bootstrap 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)
|
2015-08-11 06:11:46 +00:00
|
|
|
|
|
|
|
|
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): # pylint: disable=invalid-name
|
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()
|
2016-04-04 19:18:58 +00:00
|
|
|
self.hass.config.components.append('group')
|
2015-08-11 06:11:46 +00:00
|
|
|
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
|
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."""
|
2016-04-04 19:18:58 +00:00
|
|
|
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': {
|
2016-04-21 20:59:42 +00:00
|
|
|
'service': 'test.automation',
|
|
|
|
'data_template': {
|
|
|
|
'some': '{{ trigger.platform }} - {{ trigger.topic }}'
|
|
|
|
' - {{ trigger.payload }}'
|
|
|
|
},
|
2015-09-15 05:05:40 +00:00
|
|
|
}
|
2015-08-11 06:11:46 +00:00
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2016-04-21 20:59:42 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
2016-04-21 20:59:42 +00:00
|
|
|
self.assertEqual('mqtt - test-topic - test_payload',
|
|
|
|
self.calls[0].data['some'])
|
2015-08-11 06:11:46 +00:00
|
|
|
|
2016-08-26 06:25:57 +00:00
|
|
|
automation.turn_off(self.hass)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2016-08-26 06:25:57 +00:00
|
|
|
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."""
|
2016-04-04 19:18:58 +00:00
|
|
|
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
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-08-11 06:11:46 +00:00
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'hello')
|
2016-09-13 02:16:14 +00:00
|
|
|
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."""
|
2016-04-04 19:18:58 +00:00
|
|
|
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
|
|
|
}
|
2016-04-04 19:18:58 +00:00
|
|
|
})
|
2015-08-11 06:11:46 +00:00
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic', 'no-hello')
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:46 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|