2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the MQTT component."""
|
2017-02-18 22:17:18 +00:00
|
|
|
import asyncio
|
|
|
|
from collections import namedtuple, OrderedDict
|
2015-08-11 06:11:57 +00:00
|
|
|
import unittest
|
2015-08-11 06:34:58 +00:00
|
|
|
from unittest import mock
|
|
|
|
import socket
|
2015-08-11 06:11:57 +00:00
|
|
|
|
2016-04-07 17:52:25 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
from homeassistant.core import callback
|
2017-02-18 22:17:18 +00:00
|
|
|
from homeassistant.bootstrap import setup_component, async_setup_component
|
2015-08-11 06:11:57 +00:00
|
|
|
import homeassistant.components.mqtt as mqtt
|
2015-08-11 06:34:58 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, EVENT_HOMEASSISTANT_START,
|
|
|
|
EVENT_HOMEASSISTANT_STOP)
|
2015-08-11 06:11:57 +00:00
|
|
|
|
2015-08-11 06:34:58 +00:00
|
|
|
from tests.common import (
|
2017-02-18 22:17:18 +00:00
|
|
|
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message, mock_coro)
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def mock_mqtt_client(hass, config=None):
|
|
|
|
"""Mock the MQTT paho client."""
|
|
|
|
if config is None:
|
|
|
|
config = {
|
|
|
|
mqtt.CONF_BROKER: 'mock-broker'
|
|
|
|
}
|
|
|
|
|
|
|
|
with mock.patch('paho.mqtt.client.Client') as mock_client:
|
|
|
|
yield from async_setup_component(hass, mqtt.DOMAIN, {
|
|
|
|
mqtt.DOMAIN: config
|
|
|
|
})
|
|
|
|
return mock_client()
|
2015-08-11 06:11:57 +00:00
|
|
|
|
|
|
|
|
2017-01-03 22:17:56 +00:00
|
|
|
# pylint: disable=invalid-name
|
2015-11-23 00:04:16 +00:00
|
|
|
class TestMQTT(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the MQTT component."""
|
2015-08-11 06:11:57 +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-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2015-08-11 06:11:57 +00:00
|
|
|
mock_mqtt_component(self.hass)
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
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:57 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@callback
|
2015-08-11 06:11:57 +00:00
|
|
|
def record_calls(self, *args):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Helper for recording calls."""
|
2015-08-11 06:11:57 +00:00
|
|
|
self.calls.append(args)
|
|
|
|
|
2015-08-11 06:34:58 +00:00
|
|
|
def test_client_starts_on_home_assistant_start(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
""""Test if client start on HA launch."""
|
2015-08-11 06:34:58 +00:00
|
|
|
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertTrue(self.hass.data['mqtt'].async_start.called)
|
2015-08-11 06:34:58 +00:00
|
|
|
|
|
|
|
def test_client_stops_on_home_assistant_start(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if client stops on HA launch."""
|
2015-08-11 06:34:58 +00:00
|
|
|
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:34:58 +00:00
|
|
|
self.hass.bus.fire(EVENT_HOMEASSISTANT_STOP)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertTrue(self.hass.data['mqtt'].async_stop.called)
|
2015-08-11 06:34:58 +00:00
|
|
|
|
2017-01-03 22:17:56 +00:00
|
|
|
@mock.patch('paho.mqtt.client.Client')
|
|
|
|
def test_setup_fails_if_no_connect_broker(self, _):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test for setup failure if connection to broker is missing."""
|
2017-01-03 22:17:56 +00:00
|
|
|
test_broker_cfg = {mqtt.DOMAIN: {mqtt.CONF_BROKER: 'test-broker'}}
|
|
|
|
|
2015-08-11 06:34:58 +00:00
|
|
|
with mock.patch('homeassistant.components.mqtt.MQTT',
|
|
|
|
side_effect=socket.error()):
|
2017-02-09 18:21:57 +00:00
|
|
|
self.hass.config.components = set()
|
2017-01-03 22:17:56 +00:00
|
|
|
assert not setup_component(self.hass, mqtt.DOMAIN, test_broker_cfg)
|
2015-08-11 06:34:58 +00:00
|
|
|
|
2017-01-03 22:17:56 +00:00
|
|
|
# Ensure if we dont raise it sets up correctly
|
2017-02-09 18:21:57 +00:00
|
|
|
self.hass.config.components = set()
|
2017-01-03 22:17:56 +00:00
|
|
|
assert setup_component(self.hass, mqtt.DOMAIN, test_broker_cfg)
|
|
|
|
|
|
|
|
@mock.patch('paho.mqtt.client.Client')
|
|
|
|
def test_setup_embedded(self, _):
|
|
|
|
"""Test setting up embedded server with no config."""
|
|
|
|
client_config = ('localhost', 1883, 'user', 'pass', None, '3.1.1')
|
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
with mock.patch('homeassistant.components.mqtt.server.async_start',
|
|
|
|
return_value=mock_coro(
|
|
|
|
return_value=(True, client_config))
|
|
|
|
) as _start:
|
2017-02-09 18:21:57 +00:00
|
|
|
self.hass.config.components = set()
|
2017-01-03 22:17:56 +00:00
|
|
|
assert setup_component(self.hass, mqtt.DOMAIN,
|
|
|
|
{mqtt.DOMAIN: {}})
|
|
|
|
assert _start.call_count == 1
|
|
|
|
|
|
|
|
# Test with `embedded: None`
|
2017-02-18 22:17:18 +00:00
|
|
|
_start.return_value = mock_coro(return_value=(True, client_config))
|
2017-02-09 18:21:57 +00:00
|
|
|
self.hass.config.components = set()
|
2017-01-03 22:17:56 +00:00
|
|
|
assert setup_component(self.hass, mqtt.DOMAIN,
|
|
|
|
{mqtt.DOMAIN: {'embedded': None}})
|
|
|
|
assert _start.call_count == 2 # Another call
|
2016-04-09 16:03:41 +00:00
|
|
|
|
2015-08-11 06:11:57 +00:00
|
|
|
def test_publish_calls_service(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the publishing of call to services."""
|
2015-08-11 06:11:57 +00:00
|
|
|
self.hass.bus.listen_once(EVENT_CALL_SERVICE, self.record_calls)
|
|
|
|
|
|
|
|
mqtt.publish(self.hass, 'test-topic', 'test-payload')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:57 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(self.calls))
|
2016-02-13 13:19:11 +00:00
|
|
|
self.assertEqual(
|
2017-01-03 22:17:56 +00:00
|
|
|
'test-topic',
|
|
|
|
self.calls[0][0].data['service_data'][mqtt.ATTR_TOPIC])
|
2016-02-13 13:19:11 +00:00
|
|
|
self.assertEqual(
|
2017-01-03 22:17:56 +00:00
|
|
|
'test-payload',
|
|
|
|
self.calls[0][0].data['service_data'][mqtt.ATTR_PAYLOAD])
|
2015-08-11 06:11:57 +00:00
|
|
|
|
2016-02-09 15:41:31 +00:00
|
|
|
def test_service_call_without_topic_does_not_publish(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the service call if topic is missing."""
|
2015-08-11 06:34:58 +00:00
|
|
|
self.hass.bus.fire(EVENT_CALL_SERVICE, {
|
|
|
|
ATTR_DOMAIN: mqtt.DOMAIN,
|
|
|
|
ATTR_SERVICE: mqtt.SERVICE_PUBLISH
|
|
|
|
})
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertTrue(not self.hass.data['mqtt'].async_publish.called)
|
2015-08-11 06:34:58 +00:00
|
|
|
|
2016-02-09 15:41:31 +00:00
|
|
|
def test_service_call_with_template_payload_renders_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the service call with rendered template.
|
|
|
|
|
2016-02-09 15:41:31 +00:00
|
|
|
If 'payload_template' is provided and 'payload' is not, then render it.
|
|
|
|
"""
|
2016-02-10 22:38:33 +00:00
|
|
|
mqtt.publish_template(self.hass, "test/topic", "{{ 1+1 }}")
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertTrue(self.hass.data['mqtt'].async_publish.called)
|
|
|
|
self.assertEqual(
|
|
|
|
self.hass.data['mqtt'].async_publish.call_args[0][1], "2")
|
2016-02-09 15:41:31 +00:00
|
|
|
|
|
|
|
def test_service_call_with_payload_doesnt_render_template(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the service call with unrendered template.
|
|
|
|
|
2016-04-02 15:46:09 +00:00
|
|
|
If both 'payload' and 'payload_template' are provided then fail.
|
2016-02-09 15:41:31 +00:00
|
|
|
"""
|
|
|
|
payload = "not a template"
|
|
|
|
payload_template = "a template"
|
2016-02-10 22:38:33 +00:00
|
|
|
self.hass.services.call(mqtt.DOMAIN, mqtt.SERVICE_PUBLISH, {
|
|
|
|
mqtt.ATTR_TOPIC: "test/topic",
|
|
|
|
mqtt.ATTR_PAYLOAD: payload,
|
|
|
|
mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template
|
|
|
|
}, blocking=True)
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertFalse(self.hass.data['mqtt'].async_publish.called)
|
2016-02-09 15:41:31 +00:00
|
|
|
|
2016-04-03 07:54:05 +00:00
|
|
|
def test_service_call_with_ascii_qos_retain_flags(self):
|
|
|
|
"""Test the service call with args that can be misinterpreted.
|
|
|
|
|
|
|
|
Empty payload message and ascii formatted qos and retain flags.
|
|
|
|
"""
|
|
|
|
self.hass.services.call(mqtt.DOMAIN, mqtt.SERVICE_PUBLISH, {
|
|
|
|
mqtt.ATTR_TOPIC: "test/topic",
|
|
|
|
mqtt.ATTR_PAYLOAD: "",
|
|
|
|
mqtt.ATTR_QOS: '2',
|
|
|
|
mqtt.ATTR_RETAIN: 'no'
|
|
|
|
}, blocking=True)
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertTrue(self.hass.data['mqtt'].async_publish.called)
|
|
|
|
self.assertEqual(
|
|
|
|
self.hass.data['mqtt'].async_publish.call_args[0][2], 2)
|
|
|
|
self.assertFalse(self.hass.data['mqtt'].async_publish.call_args[0][3])
|
2016-02-09 15:41:31 +00:00
|
|
|
|
2015-08-11 06:11:57 +00:00
|
|
|
def test_subscribe_topic(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the subscription of a topic."""
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub = mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
2015-08-11 06:11: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()
|
2015-08-11 06:11:57 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
self.assertEqual('test-topic', self.calls[0][0])
|
|
|
|
self.assertEqual('test-payload', self.calls[0][1])
|
|
|
|
|
2016-08-26 06:25:35 +00:00
|
|
|
unsub()
|
|
|
|
|
|
|
|
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:35 +00:00
|
|
|
self.assertEqual(1, len(self.calls))
|
|
|
|
|
2015-08-11 06:11:57 +00:00
|
|
|
def test_subscribe_topic_not_match(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if subscribed topic is not a match."""
|
2015-08-11 06:11:57 +00:00
|
|
|
mqtt.subscribe(self.hass, 'test-topic', self.record_calls)
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'another-test-topic', 'test-payload')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:57 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_subscribe_topic_level_wildcard(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the subscription of wildcard topics."""
|
2015-08-11 06:11:57 +00:00
|
|
|
mqtt.subscribe(self.hass, 'test-topic/+/on', self.record_calls)
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic/bier/on', 'test-payload')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:57 +00:00
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the subscription of wildcard topics."""
|
2015-08-11 06:11:57 +00:00
|
|
|
mqtt.subscribe(self.hass, 'test-topic/+/on', self.record_calls)
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic/bier', 'test-payload')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:57 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
|
|
|
|
|
|
|
def test_subscribe_topic_subtree_wildcard_subtree_topic(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the subscription of wildcard topics."""
|
2015-08-11 06:11:57 +00:00
|
|
|
mqtt.subscribe(self.hass, 'test-topic/#', self.record_calls)
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'test-topic/bier/on', 'test-payload')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:57 +00:00
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the subscription of wildcard topics."""
|
2015-08-11 06:11:57 +00:00
|
|
|
mqtt.subscribe(self.hass, 'test-topic/#', self.record_calls)
|
|
|
|
|
|
|
|
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:57 +00:00
|
|
|
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):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the subscription of wildcard topics."""
|
2015-08-11 06:11:57 +00:00
|
|
|
mqtt.subscribe(self.hass, 'test-topic/#', self.record_calls)
|
|
|
|
|
|
|
|
fire_mqtt_message(self.hass, 'another-test-topic', 'test-payload')
|
|
|
|
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-08-11 06:11:57 +00:00
|
|
|
self.assertEqual(0, len(self.calls))
|
2015-11-23 00:04:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMQTTCallbacks(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the MQTT callbacks."""
|
2015-11-23 00:04:16 +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-10-31 15:47:29 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2016-01-18 05:39:25 +00:00
|
|
|
|
|
|
|
with mock.patch('paho.mqtt.client.Client'):
|
2017-02-09 18:21:57 +00:00
|
|
|
self.hass.config.components = set()
|
2016-10-27 07:16:23 +00:00
|
|
|
assert setup_component(self.hass, mqtt.DOMAIN, {
|
2016-01-18 05:39:25 +00:00
|
|
|
mqtt.DOMAIN: {
|
|
|
|
mqtt.CONF_BROKER: 'mock-broker',
|
|
|
|
}
|
|
|
|
})
|
2015-11-23 00:04:16 +00:00
|
|
|
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Stop everything that was started."""
|
2015-11-23 00:04:16 +00:00
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_receiving_mqtt_message_fires_hass_event(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if receiving triggers an event."""
|
2015-11-23 00:04:16 +00:00
|
|
|
calls = []
|
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@callback
|
2015-11-23 00:04:16 +00:00
|
|
|
def record(event):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Helper to record calls."""
|
2015-11-23 00:04:16 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen_once(mqtt.EVENT_MQTT_MESSAGE_RECEIVED, record)
|
|
|
|
|
|
|
|
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
|
|
|
message = MQTTMessage('test_topic', 1, 'Hello World!'.encode('utf-8'))
|
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt']._mqtt_on_message(
|
|
|
|
None, {'hass': self.hass}, message)
|
2016-09-13 02:16:14 +00:00
|
|
|
self.hass.block_till_done()
|
2015-11-23 00:04:16 +00:00
|
|
|
|
|
|
|
self.assertEqual(1, len(calls))
|
|
|
|
last_event = calls[0]
|
|
|
|
self.assertEqual('Hello World!', last_event.data['payload'])
|
|
|
|
self.assertEqual(message.topic, last_event.data['topic'])
|
|
|
|
self.assertEqual(message.qos, last_event.data['qos'])
|
|
|
|
|
|
|
|
def test_mqtt_failed_connection_results_in_disconnect(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if connection failure leads to disconnect."""
|
2015-11-23 00:04:16 +00:00
|
|
|
for result_code in range(1, 6):
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt']._mqttc = mock.MagicMock()
|
|
|
|
self.hass.data['mqtt']._mqtt_on_connect(
|
|
|
|
None, {'topics': {}}, 0, result_code)
|
|
|
|
self.assertTrue(self.hass.data['mqtt']._mqttc.disconnect.called)
|
2016-11-15 06:18:33 +00:00
|
|
|
|
2015-11-23 00:04:16 +00:00
|
|
|
def test_mqtt_disconnect_tries_no_reconnect_on_stop(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the disconnect tries."""
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt']._mqtt_on_disconnect(None, None, 0)
|
|
|
|
self.assertFalse(self.hass.data['mqtt']._mqttc.reconnect.called)
|
2015-11-23 00:04:16 +00:00
|
|
|
|
|
|
|
@mock.patch('homeassistant.components.mqtt.time.sleep')
|
|
|
|
def test_mqtt_disconnect_tries_reconnect(self, mock_sleep):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the re-connect tries."""
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt'].topics = {
|
2016-01-18 05:39:25 +00:00
|
|
|
'test/topic': 1,
|
|
|
|
'test/progress': None
|
|
|
|
}
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt'].progress = {
|
2016-01-18 05:39:25 +00:00
|
|
|
1: 'test/progress'
|
|
|
|
}
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt']._mqttc.reconnect.side_effect = [1, 1, 1, 0]
|
|
|
|
self.hass.data['mqtt']._mqtt_on_disconnect(None, None, 1)
|
|
|
|
self.assertTrue(self.hass.data['mqtt']._mqttc.reconnect.called)
|
|
|
|
self.assertEqual(
|
|
|
|
4, len(self.hass.data['mqtt']._mqttc.reconnect.mock_calls))
|
2015-11-23 00:04:16 +00:00
|
|
|
self.assertEqual([1, 2, 4],
|
|
|
|
[call[1][0] for call in mock_sleep.mock_calls])
|
2016-01-18 05:39:25 +00:00
|
|
|
|
2017-02-18 22:17:18 +00:00
|
|
|
self.assertEqual({'test/topic': 1}, self.hass.data['mqtt'].topics)
|
|
|
|
self.assertEqual({}, self.hass.data['mqtt'].progress)
|
2016-04-07 17:52:25 +00:00
|
|
|
|
|
|
|
def test_invalid_mqtt_topics(self):
|
2016-12-02 05:45:19 +00:00
|
|
|
"""Test invalid topics."""
|
2016-04-07 17:52:25 +00:00
|
|
|
self.assertRaises(vol.Invalid, mqtt.valid_publish_topic, 'bad+topic')
|
|
|
|
self.assertRaises(vol.Invalid, mqtt.valid_subscribe_topic, 'bad\0one')
|
2016-10-15 04:08:44 +00:00
|
|
|
|
|
|
|
def test_receiving_non_utf8_message_gets_logged(self):
|
|
|
|
"""Test receiving a non utf8 encoded message."""
|
|
|
|
calls = []
|
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@callback
|
2016-10-15 04:08:44 +00:00
|
|
|
def record(event):
|
|
|
|
"""Helper to record calls."""
|
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
payload = 0x9a
|
|
|
|
topic = 'test_topic'
|
|
|
|
self.hass.bus.listen_once(mqtt.EVENT_MQTT_MESSAGE_RECEIVED, record)
|
|
|
|
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
|
|
|
message = MQTTMessage(topic, 1, payload)
|
|
|
|
with self.assertLogs(level='ERROR') as test_handle:
|
2017-02-18 22:17:18 +00:00
|
|
|
self.hass.data['mqtt']._mqtt_on_message(
|
2016-10-15 04:08:44 +00:00
|
|
|
None,
|
|
|
|
{'hass': self.hass},
|
|
|
|
message)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
self.assertIn(
|
|
|
|
"ERROR:homeassistant.components.mqtt:Illegal utf-8 unicode "
|
|
|
|
"payload from MQTT topic: %s, Payload: " % topic,
|
|
|
|
test_handle.output[0])
|
2017-02-18 22:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_birth_message(hass):
|
|
|
|
"""Test sending birth message."""
|
|
|
|
mqtt_client = yield from mock_mqtt_client(hass, {
|
|
|
|
mqtt.CONF_BROKER: 'mock-broker',
|
|
|
|
mqtt.CONF_BIRTH_MESSAGE: {mqtt.ATTR_TOPIC: 'birth',
|
|
|
|
mqtt.ATTR_PAYLOAD: 'birth'}
|
|
|
|
})
|
|
|
|
calls = []
|
|
|
|
mqtt_client.publish = lambda *args: calls.append(args)
|
|
|
|
hass.data['mqtt']._mqtt_on_connect(None, None, 0, 0)
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert calls[-1] == ('birth', 'birth', 0, False)
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_mqtt_subscribes_topics_on_connect(hass):
|
|
|
|
"""Test subscription to topic on connect."""
|
|
|
|
mqtt_client = yield from mock_mqtt_client(hass)
|
|
|
|
|
|
|
|
prev_topics = OrderedDict()
|
|
|
|
prev_topics['topic/test'] = 1,
|
|
|
|
prev_topics['home/sensor'] = 2,
|
|
|
|
prev_topics['still/pending'] = None
|
|
|
|
|
|
|
|
hass.data['mqtt'].topics = prev_topics
|
|
|
|
hass.data['mqtt'].progress = {1: 'still/pending'}
|
|
|
|
|
|
|
|
# Return values for subscribe calls (rc, mid)
|
|
|
|
mqtt_client.subscribe.side_effect = ((0, 2), (0, 3))
|
|
|
|
|
|
|
|
hass.add_job = mock.MagicMock()
|
|
|
|
hass.data['mqtt']._mqtt_on_connect(None, None, 0, 0)
|
|
|
|
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert not mqtt_client.disconnect.called
|
|
|
|
|
|
|
|
expected = [(topic, qos) for topic, qos in prev_topics.items()
|
|
|
|
if qos is not None]
|
|
|
|
|
|
|
|
assert [call[1][1:] for call in hass.add_job.mock_calls] == expected
|