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

+3 -5
View File
@@ -14,6 +14,7 @@ from aiohttp import web
from homeassistant import core as ha, loader
from homeassistant.bootstrap import (
setup_component, async_prepare_setup_component)
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE
from homeassistant.util.unit_system import METRIC_SYSTEM
@@ -158,11 +159,8 @@ def mock_service(hass, domain, service):
@ha.callback
def async_fire_mqtt_message(hass, topic, payload, qos=0):
"""Fire the MQTT message."""
hass.bus.async_fire(mqtt.EVENT_MQTT_MESSAGE_RECEIVED, {
mqtt.ATTR_TOPIC: topic,
mqtt.ATTR_PAYLOAD: payload,
mqtt.ATTR_QOS: qos,
})
async_dispatcher_send(
hass, mqtt.SIGNAL_MQTT_MESSAGE_RECEIVED, topic, payload, qos)
def fire_mqtt_message(hass, topic, payload, qos=0):
+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
+41 -8
View File
@@ -28,8 +28,6 @@ class TestHelpersDispatcher(object):
calls.append(data)
dispatcher_connect(self.hass, 'test', test_funct)
self.hass.block_till_done()
dispatcher_send(self.hass, 'test', 3)
self.hass.block_till_done()
@@ -40,6 +38,47 @@ class TestHelpersDispatcher(object):
assert calls == [3, 'bla']
def test_simple_function_unsub(self):
"""Test simple function (executor) and unsub."""
calls1 = []
calls2 = []
def test_funct1(data):
"""Test function."""
calls1.append(data)
def test_funct2(data):
"""Test function."""
calls2.append(data)
dispatcher_connect(self.hass, 'test1', test_funct1)
unsub = dispatcher_connect(self.hass, 'test2', test_funct2)
dispatcher_send(self.hass, 'test1', 3)
dispatcher_send(self.hass, 'test2', 4)
self.hass.block_till_done()
assert calls1 == [3]
assert calls2 == [4]
unsub()
dispatcher_send(self.hass, 'test1', 5)
dispatcher_send(self.hass, 'test2', 6)
self.hass.block_till_done()
assert calls1 == [3, 5]
assert calls2 == [4]
# check don't kill the flow
unsub()
dispatcher_send(self.hass, 'test1', 7)
dispatcher_send(self.hass, 'test2', 8)
self.hass.block_till_done()
assert calls1 == [3, 5, 7]
assert calls2 == [4]
def test_simple_callback(self):
"""Test simple callback (async)."""
calls = []
@@ -50,8 +89,6 @@ class TestHelpersDispatcher(object):
calls.append(data)
dispatcher_connect(self.hass, 'test', test_funct)
self.hass.block_till_done()
dispatcher_send(self.hass, 'test', 3)
self.hass.block_till_done()
@@ -72,8 +109,6 @@ class TestHelpersDispatcher(object):
calls.append(data)
dispatcher_connect(self.hass, 'test', test_funct)
self.hass.block_till_done()
dispatcher_send(self.hass, 'test', 3)
self.hass.block_till_done()
@@ -95,8 +130,6 @@ class TestHelpersDispatcher(object):
calls.append(data3)
dispatcher_connect(self.hass, 'test', test_funct)
self.hass.block_till_done()
dispatcher_send(self.hass, 'test', 3, 2, 'bla')
self.hass.block_till_done()