MQTT convert to async (#6064)
* Migrate mqtt to async * address paulus comment / convert it complet async * adress paulus comment / remove future * Automation triggers should be async * Fix MQTT async calls * Show that event helpers are callbacks * Fix tests * Lint
This commit is contained in:
1 parent
fa2c1dafdf
commit
e1cbd6b4c0
25 files changed
+356
-231
No files matched your search
@@ -111,7 +111,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
alarm_control_panel.alarm_arm_home(self.hass)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(('alarm/command', 'ARM_HOME', 0, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
|
||||
def test_arm_home_not_publishes_mqtt_with_invalid_code(self):
|
||||
"""Test not publishing of MQTT messages with invalid code."""
|
||||
@@ -146,7 +146,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
alarm_control_panel.alarm_arm_away(self.hass)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(('alarm/command', 'ARM_AWAY', 0, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
|
||||
def test_arm_away_not_publishes_mqtt_with_invalid_code(self):
|
||||
"""Test not publishing of MQTT messages with invalid code."""
|
||||
@@ -181,7 +181,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase):
|
||||
alarm_control_panel.alarm_disarm(self.hass)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(('alarm/command', 'DISARM', 0, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
|
||||
def test_disarm_not_publishes_mqtt_with_invalid_code(self):
|
||||
"""Test not publishing of MQTT messages with invalid code."""
|
||||
|
||||
@@ -118,7 +118,7 @@ class TestCoverMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'OPEN', 0, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('cover.test')
|
||||
self.assertEqual(STATE_OPEN, state.state)
|
||||
|
||||
@@ -126,7 +126,7 @@ class TestCoverMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'CLOSE', 0, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('cover.test')
|
||||
self.assertEqual(STATE_CLOSED, state.state)
|
||||
|
||||
@@ -150,7 +150,7 @@ class TestCoverMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'OPEN', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('cover.test')
|
||||
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||
|
||||
@@ -174,7 +174,7 @@ class TestCoverMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'CLOSE', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('cover.test')
|
||||
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||
|
||||
@@ -198,7 +198,7 @@ class TestCoverMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'STOP', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('cover.test')
|
||||
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ light:
|
||||
|
||||
"""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ASSUMED_STATE
|
||||
@@ -328,7 +329,7 @@ class TestLightMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', 'on', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
|
||||
@@ -336,27 +337,20 @@ class TestLightMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', 'off', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
|
||||
self.mock_publish.reset_mock()
|
||||
light.turn_on(self.hass, 'light.test', rgb_color=[75, 75, 75],
|
||||
brightness=50)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Calls are threaded so we need to reorder them
|
||||
bright_call, rgb_call, state_call = \
|
||||
sorted((call[1] for call in self.mock_publish.mock_calls[-3:]),
|
||||
key=lambda call: call[0])
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', 'on', 2, False),
|
||||
state_call)
|
||||
|
||||
self.assertEqual(('test_light_rgb/rgb/set', '75,75,75', 2, False),
|
||||
rgb_call)
|
||||
|
||||
self.assertEqual(('test_light_rgb/brightness/set', 50, 2, False),
|
||||
bright_call)
|
||||
self.mock_publish().async_publish.assert_has_calls([
|
||||
mock.call('test_light_rgb/set', 'on', 2, False),
|
||||
mock.call('test_light_rgb/rgb/set', '75,75,75', 2, False),
|
||||
mock.call('test_light_rgb/brightness/set', 50, 2, False),
|
||||
], any_order=True)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
|
||||
@@ -172,7 +172,7 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', '{"state": "ON"}', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
|
||||
@@ -180,7 +180,7 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', '{"state": "OFF"}', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
|
||||
@@ -189,11 +189,11 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(2, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
# Get the sent message
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
|
||||
self.assertEqual(50, message_json["brightness"])
|
||||
self.assertEqual(75, message_json["color"]["r"])
|
||||
self.assertEqual(75, message_json["color"]["g"])
|
||||
@@ -228,11 +228,11 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
# Get the sent message
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
|
||||
self.assertEqual(5, message_json["flash"])
|
||||
self.assertEqual("ON", message_json["state"])
|
||||
|
||||
@@ -240,11 +240,11 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
# Get the sent message
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
|
||||
self.assertEqual(15, message_json["flash"])
|
||||
self.assertEqual("ON", message_json["state"])
|
||||
|
||||
@@ -268,11 +268,11 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
# Get the sent message
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
|
||||
self.assertEqual(10, message_json["transition"])
|
||||
self.assertEqual("ON", message_json["state"])
|
||||
|
||||
@@ -281,11 +281,11 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
# Get the sent message
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-1][1][1])
|
||||
message_json = json.loads(self.mock_publish.mock_calls[-2][1][1])
|
||||
self.assertEqual(10, message_json["transition"])
|
||||
self.assertEqual("OFF", message_json["state"])
|
||||
|
||||
|
||||
@@ -196,7 +196,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', 'on,,--', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
|
||||
@@ -205,7 +205,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('test_light_rgb/set', 'off', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
|
||||
@@ -215,12 +215,12 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(2, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
|
||||
# check the payload
|
||||
payload = self.mock_publish.mock_calls[-1][1][1]
|
||||
payload = self.mock_publish.mock_calls[-2][1][1]
|
||||
self.assertEqual('on,50,75-75-75', payload)
|
||||
|
||||
# check the state
|
||||
@@ -253,12 +253,12 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
|
||||
# check the payload
|
||||
payload = self.mock_publish.mock_calls[-1][1][1]
|
||||
payload = self.mock_publish.mock_calls[-2][1][1]
|
||||
self.assertEqual('on,short', payload)
|
||||
|
||||
# long flash
|
||||
@@ -266,12 +266,12 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
|
||||
# check the payload
|
||||
payload = self.mock_publish.mock_calls[-1][1][1]
|
||||
payload = self.mock_publish.mock_calls[-2][1][1]
|
||||
self.assertEqual('on,long', payload)
|
||||
|
||||
def test_transition(self):
|
||||
@@ -296,12 +296,12 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
|
||||
# check the payload
|
||||
payload = self.mock_publish.mock_calls[-1][1][1]
|
||||
payload = self.mock_publish.mock_calls[-2][1][1]
|
||||
self.assertEqual('on,10', payload)
|
||||
|
||||
# transition off
|
||||
@@ -309,12 +309,12 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual('test_light_rgb/set',
|
||||
self.mock_publish.mock_calls[-1][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-1][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-1][1][3])
|
||||
self.mock_publish.mock_calls[-2][1][0])
|
||||
self.assertEqual(0, self.mock_publish.mock_calls[-2][1][2])
|
||||
self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])
|
||||
|
||||
# check the payload
|
||||
payload = self.mock_publish.mock_calls[-1][1][1]
|
||||
payload = self.mock_publish.mock_calls[-2][1][1]
|
||||
self.assertEqual('off,4', payload)
|
||||
|
||||
def test_invalid_values(self): \
|
||||
|
||||
@@ -73,7 +73,7 @@ class TestLockMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'LOCK', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('lock.test')
|
||||
self.assertEqual(STATE_LOCKED, state.state)
|
||||
|
||||
@@ -81,7 +81,7 @@ class TestLockMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'UNLOCK', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('lock.test')
|
||||
self.assertEqual(STATE_UNLOCKED, state.state)
|
||||
|
||||
|
||||
@@ -12,9 +12,10 @@ def test_subscribing_config_topic(hass, mqtt_mock):
|
||||
"""Test setting up discovery."""
|
||||
hass_config = {}
|
||||
discovery_topic = 'homeassistant'
|
||||
async_start(hass, discovery_topic, hass_config)
|
||||
assert mqtt_mock.subscribe.called
|
||||
call_args = mqtt_mock.subscribe.mock_calls[0][1]
|
||||
yield from async_start(hass, discovery_topic, hass_config)
|
||||
|
||||
assert mqtt_mock.async_subscribe.called
|
||||
call_args = mqtt_mock.async_subscribe.mock_calls[0][1]
|
||||
assert call_args[0] == discovery_topic + '/#'
|
||||
assert call_args[1] == 0
|
||||
|
||||
@@ -24,7 +25,7 @@ def test_subscribing_config_topic(hass, mqtt_mock):
|
||||
def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
|
||||
"""Test sending in invalid JSON."""
|
||||
mock_load_platform.return_value = mock_coro()
|
||||
async_start(hass, 'homeassistant', {})
|
||||
yield from async_start(hass, 'homeassistant', {})
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/not_config',
|
||||
'{}')
|
||||
@@ -37,7 +38,7 @@ def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
|
||||
def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
|
||||
"""Test sending in invalid JSON."""
|
||||
mock_load_platform.return_value = mock_coro()
|
||||
async_start(hass, 'homeassistant', {})
|
||||
yield from async_start(hass, 'homeassistant', {})
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
|
||||
'not json')
|
||||
@@ -51,7 +52,7 @@ def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
|
||||
def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
|
||||
"""Test sending in invalid JSON."""
|
||||
mock_load_platform.return_value = mock_coro()
|
||||
async_start(hass, 'homeassistant', {})
|
||||
yield from async_start(hass, 'homeassistant', {})
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', '{}')
|
||||
yield from hass.async_block_till_done()
|
||||
@@ -62,7 +63,7 @@ def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
|
||||
@asyncio.coroutine
|
||||
def test_correct_config_discovery(hass, mqtt_mock, caplog):
|
||||
"""Test sending in invalid JSON."""
|
||||
async_start(hass, 'homeassistant', {})
|
||||
yield from async_start(hass, 'homeassistant', {})
|
||||
|
||||
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
|
||||
'{ "name": "Beer" }')
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""The tests for the MQTT component."""
|
||||
from collections import namedtuple
|
||||
import asyncio
|
||||
from collections import namedtuple, OrderedDict
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import socket
|
||||
@@ -7,14 +8,29 @@ import socket
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.bootstrap import setup_component
|
||||
from homeassistant.bootstrap import setup_component, async_setup_component
|
||||
import homeassistant.components.mqtt as mqtt
|
||||
from homeassistant.const import (
|
||||
EVENT_CALL_SERVICE, ATTR_DOMAIN, ATTR_SERVICE, EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STOP)
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
|
||||
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()
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@@ -40,7 +56,7 @@ class TestMQTT(unittest.TestCase):
|
||||
""""Test if client start on HA launch."""
|
||||
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
|
||||
self.hass.block_till_done()
|
||||
self.assertTrue(mqtt.MQTT_CLIENT.start.called)
|
||||
self.assertTrue(self.hass.data['mqtt'].async_start.called)
|
||||
|
||||
def test_client_stops_on_home_assistant_start(self):
|
||||
"""Test if client stops on HA launch."""
|
||||
@@ -48,7 +64,7 @@ class TestMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
self.hass.bus.fire(EVENT_HOMEASSISTANT_STOP)
|
||||
self.hass.block_till_done()
|
||||
self.assertTrue(mqtt.MQTT_CLIENT.stop.called)
|
||||
self.assertTrue(self.hass.data['mqtt'].async_stop.called)
|
||||
|
||||
@mock.patch('paho.mqtt.client.Client')
|
||||
def test_setup_fails_if_no_connect_broker(self, _):
|
||||
@@ -69,14 +85,17 @@ class TestMQTT(unittest.TestCase):
|
||||
"""Test setting up embedded server with no config."""
|
||||
client_config = ('localhost', 1883, 'user', 'pass', None, '3.1.1')
|
||||
|
||||
with mock.patch('homeassistant.components.mqtt.server.start',
|
||||
return_value=(True, client_config)) as _start:
|
||||
with mock.patch('homeassistant.components.mqtt.server.async_start',
|
||||
return_value=mock_coro(
|
||||
return_value=(True, client_config))
|
||||
) as _start:
|
||||
self.hass.config.components = set()
|
||||
assert setup_component(self.hass, mqtt.DOMAIN,
|
||||
{mqtt.DOMAIN: {}})
|
||||
assert _start.call_count == 1
|
||||
|
||||
# Test with `embedded: None`
|
||||
_start.return_value = mock_coro(return_value=(True, client_config))
|
||||
self.hass.config.components = set()
|
||||
assert setup_component(self.hass, mqtt.DOMAIN,
|
||||
{mqtt.DOMAIN: {'embedded': None}})
|
||||
@@ -105,7 +124,7 @@ class TestMQTT(unittest.TestCase):
|
||||
ATTR_SERVICE: mqtt.SERVICE_PUBLISH
|
||||
})
|
||||
self.hass.block_till_done()
|
||||
self.assertTrue(not mqtt.MQTT_CLIENT.publish.called)
|
||||
self.assertTrue(not self.hass.data['mqtt'].async_publish.called)
|
||||
|
||||
def test_service_call_with_template_payload_renders_template(self):
|
||||
"""Test the service call with rendered template.
|
||||
@@ -114,8 +133,9 @@ class TestMQTT(unittest.TestCase):
|
||||
"""
|
||||
mqtt.publish_template(self.hass, "test/topic", "{{ 1+1 }}")
|
||||
self.hass.block_till_done()
|
||||
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
|
||||
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][1], "2")
|
||||
self.assertTrue(self.hass.data['mqtt'].async_publish.called)
|
||||
self.assertEqual(
|
||||
self.hass.data['mqtt'].async_publish.call_args[0][1], "2")
|
||||
|
||||
def test_service_call_with_payload_doesnt_render_template(self):
|
||||
"""Test the service call with unrendered template.
|
||||
@@ -129,7 +149,7 @@ class TestMQTT(unittest.TestCase):
|
||||
mqtt.ATTR_PAYLOAD: payload,
|
||||
mqtt.ATTR_PAYLOAD_TEMPLATE: payload_template
|
||||
}, blocking=True)
|
||||
self.assertFalse(mqtt.MQTT_CLIENT.publish.called)
|
||||
self.assertFalse(self.hass.data['mqtt'].async_publish.called)
|
||||
|
||||
def test_service_call_with_ascii_qos_retain_flags(self):
|
||||
"""Test the service call with args that can be misinterpreted.
|
||||
@@ -142,9 +162,10 @@ class TestMQTT(unittest.TestCase):
|
||||
mqtt.ATTR_QOS: '2',
|
||||
mqtt.ATTR_RETAIN: 'no'
|
||||
}, blocking=True)
|
||||
self.assertTrue(mqtt.MQTT_CLIENT.publish.called)
|
||||
self.assertEqual(mqtt.MQTT_CLIENT.publish.call_args[0][2], 2)
|
||||
self.assertFalse(mqtt.MQTT_CLIENT.publish.call_args[0][3])
|
||||
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])
|
||||
|
||||
def test_subscribe_topic(self):
|
||||
"""Test the subscription of a topic."""
|
||||
@@ -231,15 +252,12 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
# mock_mqtt_component(self.hass)
|
||||
|
||||
with mock.patch('paho.mqtt.client.Client'):
|
||||
self.hass.config.components = set()
|
||||
assert setup_component(self.hass, mqtt.DOMAIN, {
|
||||
mqtt.DOMAIN: {
|
||||
mqtt.CONF_BROKER: 'mock-broker',
|
||||
mqtt.CONF_BIRTH_MESSAGE: {mqtt.ATTR_TOPIC: 'birth',
|
||||
mqtt.ATTR_PAYLOAD: 'birth'}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -261,7 +279,8 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
||||
message = MQTTMessage('test_topic', 1, 'Hello World!'.encode('utf-8'))
|
||||
|
||||
mqtt.MQTT_CLIENT._mqtt_on_message(None, {'hass': self.hass}, message)
|
||||
self.hass.data['mqtt']._mqtt_on_message(
|
||||
None, {'hass': self.hass}, message)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(calls))
|
||||
@@ -273,68 +292,36 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
def test_mqtt_failed_connection_results_in_disconnect(self):
|
||||
"""Test if connection failure leads to disconnect."""
|
||||
for result_code in range(1, 6):
|
||||
mqtt.MQTT_CLIENT._mqttc = mock.MagicMock()
|
||||
mqtt.MQTT_CLIENT._mqtt_on_connect(None, {'topics': {}}, 0,
|
||||
result_code)
|
||||
self.assertTrue(mqtt.MQTT_CLIENT._mqttc.disconnect.called)
|
||||
|
||||
def test_mqtt_subscribes_topics_on_connect(self):
|
||||
"""Test subscription to topic on connect."""
|
||||
from collections import OrderedDict
|
||||
prev_topics = OrderedDict()
|
||||
prev_topics['topic/test'] = 1,
|
||||
prev_topics['home/sensor'] = 2,
|
||||
prev_topics['still/pending'] = None
|
||||
|
||||
mqtt.MQTT_CLIENT.topics = prev_topics
|
||||
mqtt.MQTT_CLIENT.progress = {1: 'still/pending'}
|
||||
# Return values for subscribe calls (rc, mid)
|
||||
mqtt.MQTT_CLIENT._mqttc.subscribe.side_effect = ((0, 2), (0, 3))
|
||||
mqtt.MQTT_CLIENT._mqtt_on_connect(None, None, 0, 0)
|
||||
self.assertFalse(mqtt.MQTT_CLIENT._mqttc.disconnect.called)
|
||||
|
||||
expected = [(topic, qos) for topic, qos in prev_topics.items()
|
||||
if qos is not None]
|
||||
self.assertEqual(
|
||||
expected,
|
||||
[call[1] for call in mqtt.MQTT_CLIENT._mqttc.subscribe.mock_calls])
|
||||
self.assertEqual({
|
||||
1: 'still/pending',
|
||||
2: 'topic/test',
|
||||
3: 'home/sensor',
|
||||
}, mqtt.MQTT_CLIENT.progress)
|
||||
|
||||
def test_mqtt_birth_message_on_connect(self): \
|
||||
# pylint: disable=no-self-use
|
||||
"""Test birth message on connect."""
|
||||
mqtt.MQTT_CLIENT._mqtt_on_connect(None, None, 0, 0)
|
||||
mqtt.MQTT_CLIENT._mqttc.publish.assert_called_with('birth', 'birth', 0,
|
||||
False)
|
||||
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)
|
||||
|
||||
def test_mqtt_disconnect_tries_no_reconnect_on_stop(self):
|
||||
"""Test the disconnect tries."""
|
||||
mqtt.MQTT_CLIENT._mqtt_on_disconnect(None, None, 0)
|
||||
self.assertFalse(mqtt.MQTT_CLIENT._mqttc.reconnect.called)
|
||||
self.hass.data['mqtt']._mqtt_on_disconnect(None, None, 0)
|
||||
self.assertFalse(self.hass.data['mqtt']._mqttc.reconnect.called)
|
||||
|
||||
@mock.patch('homeassistant.components.mqtt.time.sleep')
|
||||
def test_mqtt_disconnect_tries_reconnect(self, mock_sleep):
|
||||
"""Test the re-connect tries."""
|
||||
mqtt.MQTT_CLIENT.topics = {
|
||||
self.hass.data['mqtt'].topics = {
|
||||
'test/topic': 1,
|
||||
'test/progress': None
|
||||
}
|
||||
mqtt.MQTT_CLIENT.progress = {
|
||||
self.hass.data['mqtt'].progress = {
|
||||
1: 'test/progress'
|
||||
}
|
||||
mqtt.MQTT_CLIENT._mqttc.reconnect.side_effect = [1, 1, 1, 0]
|
||||
mqtt.MQTT_CLIENT._mqtt_on_disconnect(None, None, 1)
|
||||
self.assertTrue(mqtt.MQTT_CLIENT._mqttc.reconnect.called)
|
||||
self.assertEqual(4, len(mqtt.MQTT_CLIENT._mqttc.reconnect.mock_calls))
|
||||
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))
|
||||
self.assertEqual([1, 2, 4],
|
||||
[call[1][0] for call in mock_sleep.mock_calls])
|
||||
|
||||
self.assertEqual({'test/topic': 1}, mqtt.MQTT_CLIENT.topics)
|
||||
self.assertEqual({}, mqtt.MQTT_CLIENT.progress)
|
||||
self.assertEqual({'test/topic': 1}, self.hass.data['mqtt'].topics)
|
||||
self.assertEqual({}, self.hass.data['mqtt'].progress)
|
||||
|
||||
def test_invalid_mqtt_topics(self):
|
||||
"""Test invalid topics."""
|
||||
@@ -356,7 +343,7 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
MQTTMessage = namedtuple('MQTTMessage', ['topic', 'qos', 'payload'])
|
||||
message = MQTTMessage(topic, 1, payload)
|
||||
with self.assertLogs(level='ERROR') as test_handle:
|
||||
mqtt.MQTT_CLIENT._mqtt_on_message(
|
||||
self.hass.data['mqtt']._mqtt_on_message(
|
||||
None,
|
||||
{'hass': self.hass},
|
||||
message)
|
||||
@@ -365,3 +352,47 @@ class TestMQTTCallbacks(unittest.TestCase):
|
||||
"ERROR:homeassistant.components.mqtt:Illegal utf-8 unicode "
|
||||
"payload from MQTT topic: %s, Payload: " % topic,
|
||||
test_handle.output[0])
|
||||
|
||||
|
||||
@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
|
||||
@@ -4,7 +4,7 @@ from unittest.mock import Mock, MagicMock, patch
|
||||
from homeassistant.bootstrap import setup_component
|
||||
import homeassistant.components.mqtt as mqtt
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, mock_coro
|
||||
|
||||
|
||||
class TestMQTT:
|
||||
@@ -21,9 +21,8 @@ class TestMQTT:
|
||||
|
||||
@patch('passlib.apps.custom_app_context', Mock(return_value=''))
|
||||
@patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock()))
|
||||
@patch('homeassistant.components.mqtt.server.run_coroutine_threadsafe',
|
||||
Mock(return_value=MagicMock()))
|
||||
@patch('hbmqtt.broker.Broker', Mock(return_value=MagicMock()))
|
||||
@patch('hbmqtt.broker.Broker.start', Mock(return_value=mock_coro()))
|
||||
@patch('homeassistant.components.mqtt.MQTT')
|
||||
def test_creating_config_with_http_pass(self, mock_mqtt):
|
||||
"""Test if the MQTT server gets started and subscribe/publish msg."""
|
||||
@@ -46,7 +45,7 @@ class TestMQTT:
|
||||
assert mock_mqtt.mock_calls[0][1][6] is None
|
||||
|
||||
@patch('tempfile.NamedTemporaryFile', Mock(return_value=MagicMock()))
|
||||
@patch('homeassistant.components.mqtt.server.run_coroutine_threadsafe')
|
||||
@patch('hbmqtt.broker.Broker.start', return_value=mock_coro())
|
||||
def test_broker_config_fails(self, mock_run):
|
||||
"""Test if the MQTT component fails if server fails."""
|
||||
from hbmqtt.broker import BrokerException
|
||||
|
||||
@@ -72,7 +72,7 @@ class TestSensorMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'beer on', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('switch.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
|
||||
@@ -80,7 +80,7 @@ class TestSensorMQTT(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(('command-topic', 'beer off', 2, False),
|
||||
self.mock_publish.mock_calls[-1][1])
|
||||
self.mock_publish.mock_calls[-2][1])
|
||||
state = self.hass.states.get('switch.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
|
||||
|
||||
Reference in new issue
Block a user