Add MQTT component tests
This commit is contained in:
parent
291cc62381
commit
65a4b3c9f8
2 changed files with 104 additions and 2 deletions
|
@ -16,8 +16,6 @@ omit =
|
||||||
homeassistant/components/modbus.py
|
homeassistant/components/modbus.py
|
||||||
homeassistant/components/*/modbus.py
|
homeassistant/components/*/modbus.py
|
||||||
|
|
||||||
homeassistant/components/mqtt.py
|
|
||||||
|
|
||||||
homeassistant/components/wink.py
|
homeassistant/components/wink.py
|
||||||
homeassistant/components/*/wink.py
|
homeassistant/components/*/wink.py
|
||||||
|
|
||||||
|
|
104
tests/components/test_mqtt.py
Normal file
104
tests/components/test_mqtt.py
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
"""
|
||||||
|
tests.test_component_mqtt
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Tests MQTT component.
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import homeassistant as ha
|
||||||
|
import homeassistant.components.mqtt as mqtt
|
||||||
|
from homeassistant.const import EVENT_CALL_SERVICE
|
||||||
|
|
||||||
|
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||||
|
|
||||||
|
|
||||||
|
class TestDemo(unittest.TestCase):
|
||||||
|
""" Test the demo module. """
|
||||||
|
|
||||||
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
|
self.hass = ha.HomeAssistant()
|
||||||
|
mock_mqtt_component(self.hass)
|
||||||
|
self.calls = []
|
||||||
|
|
||||||
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
|
""" Stop down stuff we started. """
|
||||||
|
self.hass.stop()
|
||||||
|
|
||||||
|
def record_calls(self, *args):
|
||||||
|
self.calls.append(args)
|
||||||
|
|
||||||
|
def test_publish_calls_service(self):
|
||||||
|
self.hass.bus.listen_once(EVENT_CALL_SERVICE, self.record_calls)
|
||||||
|
|
||||||
|
mqtt.publish(self.hass, 'test-topic', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
self.assertEqual('test-topic', self.calls[0][0].data[mqtt.ATTR_TOPIC])
|
||||||
|
self.assertEqual('test-payload', self.calls[0][0].data[mqtt.ATTR_PAYLOAD])
|
||||||
|
|
||||||
|
def test_subscribe_topic(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
self.assertEqual('test-topic', self.calls[0][0])
|
||||||
|
self.assertEqual('test-payload', self.calls[0][1])
|
||||||
|
|
||||||
|
def test_subscribe_topic_not_match(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'another-test-topic', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
|
def test_subscribe_topic_level_wildcard(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic/+/on', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic/bier/on', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
self.assertEqual('test-topic/bier/on', self.calls[0][0])
|
||||||
|
self.assertEqual('test-payload', self.calls[0][1])
|
||||||
|
|
||||||
|
def test_subscribe_topic_level_wildcard_no_subtree_match(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic/+/on', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic/bier', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
||||||
|
|
||||||
|
def test_subscribe_topic_subtree_wildcard_subtree_topic(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic/#', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic/bier/on', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
self.assertEqual('test-topic/bier/on', self.calls[0][0])
|
||||||
|
self.assertEqual('test-payload', self.calls[0][1])
|
||||||
|
|
||||||
|
def test_subscribe_topic_subtree_wildcard_root_topic(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic/#', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'test-topic', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(self.calls))
|
||||||
|
self.assertEqual('test-topic', self.calls[0][0])
|
||||||
|
self.assertEqual('test-payload', self.calls[0][1])
|
||||||
|
|
||||||
|
def test_subscribe_topic_subtree_wildcard_no_match(self):
|
||||||
|
mqtt.subscribe(self.hass, 'test-topic/#', self.record_calls)
|
||||||
|
|
||||||
|
fire_mqtt_message(self.hass, 'another-test-topic', 'test-payload')
|
||||||
|
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(0, len(self.calls))
|
Loading…
Add table
Reference in a new issue