core/tests/components/automation/test_mqtt.py

124 lines
3.9 KiB
Python
Raw Normal View History

2015-08-11 06:11:46 +00:00
"""
2015-09-15 07:02:46 +00:00
tests.components.automation.test_mqtt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-08-11 06:11:46 +00:00
2015-09-15 07:02:46 +00:00
Tests mqtt automation.
2015-08-11 06:11:46 +00:00
"""
import unittest
2015-08-17 03:44:46 +00:00
import homeassistant.core as ha
2015-08-11 06:11:46 +00:00
import homeassistant.components.automation as automation
from tests.common import mock_mqtt_component, fire_mqtt_message
2015-10-03 06:57:26 +00:00
class TestAutomationMQTT(unittest.TestCase):
2015-08-11 06:11:46 +00:00
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
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 down stuff we started. """
self.hass.stop()
2015-09-15 05:05:40 +00:00
def test_old_config_if_fires_on_topic_match(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'platform': 'mqtt',
'mqtt_topic': 'test-topic',
'execute_service': 'test.automation'
}
}))
fire_mqtt_message(self.hass, 'test-topic', '')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_old_config_if_fires_on_topic_and_payload_match(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'platform': 'mqtt',
'mqtt_topic': 'test-topic',
'mqtt_payload': 'hello',
'execute_service': 'test.automation'
}
}))
fire_mqtt_message(self.hass, 'test-topic', 'hello')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_old_config_if_not_fires_on_topic_but_no_payload_match(self):
self.assertTrue(automation.setup(self.hass, {
2015-08-11 06:11:46 +00:00
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'platform': 'mqtt',
'mqtt_topic': 'test-topic',
'mqtt_payload': 'hello',
'execute_service': 'test.automation'
2015-08-11 06:11:46 +00:00
}
}))
2015-09-15 05:05:40 +00:00
fire_mqtt_message(self.hass, 'test-topic', 'no-hello')
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
2015-08-11 06:11:46 +00:00
def test_if_fires_on_topic_match(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
2015-09-15 05:05:40 +00:00
'trigger': {
'platform': 'mqtt',
'topic': 'test-topic'
},
'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
}
}))
fire_mqtt_message(self.hass, 'test-topic', '')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_fires_on_topic_and_payload_match(self):
self.assertTrue(automation.setup(self.hass, {
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
}
}))
fire_mqtt_message(self.hass, 'test-topic', 'hello')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_if_not_fires_on_topic_but_no_payload_match(self):
self.assertTrue(automation.setup(self.hass, {
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
}
}))
fire_mqtt_message(self.hass, 'test-topic', 'no-hello')
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))