Move mqtt from eventbus to dispatcher / add unsub for dispatcher (#6206)

* Move mqtt from eventbus to dispatcher / add unsub for dispatcher

* Fix lint

* Fix test

* Fix lint v2

* fix dispatcher_send
This commit is contained in:
Pascal Vizeli authored and Paulus Schoutsen committed 2017-02-24 17:11:50 -08:00
1 parent d6818c7015
commit 81ca978413
7 files changed
+105 -91

No files matched your search

+23 -9
View File
@@ -13,6 +13,7 @@ import homeassistant.components.mqtt as mqtt
from homeassistant.const import (
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from tests.common import (
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message, mock_coro)
@@ -237,11 +238,17 @@ class TestMQTTCallbacks(unittest.TestCase):
calls = []
@callback
def record(event):
def record(topic, payload, qos):
"""Helper to record calls."""
calls.append(event)
data = {
'topic': topic,
'payload': payload,
'qos': qos,
}
calls.append(data)
self.hass.bus.listen_once(mqtt.EVENT_MQTT_MESSAGE_RECEIVED, record)
async_dispatcher_connect(
self.hass, mqtt.SIGNAL_MQTT_MESSAGE_RECEIVED, record)
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
message = MQTTMessage('test_topic', 1, 'Hello World!'.encode('utf-8'))
@@ -252,9 +259,9 @@ class TestMQTTCallbacks(unittest.TestCase):
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'])
self.assertEqual('Hello World!', last_event['payload'])
self.assertEqual(message.topic, last_event['topic'])
self.assertEqual(message.qos, last_event['qos'])
def test_mqtt_failed_connection_results_in_disconnect(self):
"""Test if connection failure leads to disconnect."""
@@ -300,13 +307,20 @@ class TestMQTTCallbacks(unittest.TestCase):
calls = []
@callback
def record(event):
def record(topic, payload, qos):
"""Helper to record calls."""
calls.append(event)
data = {
'topic': topic,
'payload': payload,
'qos': qos,
}
calls.append(data)
async_dispatcher_connect(
self.hass, mqtt.SIGNAL_MQTT_MESSAGE_RECEIVED, record)
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:
-44
View File
@@ -1,11 +1,9 @@
"""The tests for the MQTT eventstream component."""
from collections import namedtuple
import json
from unittest.mock import ANY, patch
from homeassistant.bootstrap import setup_component
import homeassistant.components.mqtt_eventstream as eventstream
import homeassistant.components.mqtt as mqtt
from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.core import State, callback
from homeassistant.remote import JSONEncoder
@@ -146,45 +144,3 @@ class TestMqttEventStream(object):
self.hass.block_till_done()
assert 1 == len(calls)
@patch('homeassistant.components.mqtt.async_publish')
def test_mqtt_received_event(self, mock_pub):
"""Don't filter events from the mqtt component about received message.
Mqtt component sends an event if a message is received. Also
messages that originate from an incoming eventstream.
Broadcasting these messages result in an infinite loop if two HA
instances are crossconfigured for the same mqtt topics.
"""
SUB_TOPIC = 'from_slaves'
assert self.add_eventstream(
pub_topic='bar',
sub_topic=SUB_TOPIC)
self.hass.block_till_done()
# Reset the mock because it will have already gotten calls for the
# mqtt_eventstream state change on initialization, etc.
mock_pub.reset_mock()
# Use MQTT component message handler to simulate firing message
# received event.
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
message = MQTTMessage(
SUB_TOPIC, 1, '{"test": "Hello World!"}'.encode('utf-8'))
mqtt.MQTT._mqtt_on_message(self, None, {'hass': self.hass}, message)
self.hass.block_till_done()
# 'normal' incoming mqtt messages should be broadcasted
assert mock_pub.call_count == 0
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
message = MQTTMessage(
'test_topic', 1, '{"test": "Hello World!"}'.encode('utf-8'))
mqtt.MQTT._mqtt_on_message(self, None, {'hass': self.hass}, message)
self.hass.block_till_done()
# but event from the event stream not
assert mock_pub.call_count == 1