Fix MQTT retained message not being re-dispatched (#12004)

* Fix MQTT retained message not being re-dispatched

* Fix tests

* Use paho-mqtt for retained messages

* Improve code style

* Store list of subscribers

* Fix lint error

* Adhere to Home Assistant's logging standard

"Try to avoid brackets and additional quotes around the output to make it easier for users to parse the log."
 - https://home-assistant.io/developers/development_guidelines/

* Add reconnect tests

* Fix lint error

* Introduce Subscription

Tests still need to be updated

* Use namedtuple for MQTT messages

... And fix issues

Accessing the config manually at runtime isn't ideal

* Fix MQTT __init__.py tests

* Updated usage of Mocks
* Moved tests that were testing subscriptions out of the MQTTComponent test, because of how mock.patch was used
* Adjusted the remaining tests for the MQTT clients new behavior - e.g. self.progress was removed
* Updated the async_fire_mqtt_message helper

*  Update MQTT tests

* Re-introduce the MQTT subscriptions through the dispatcher for tests - quite ugly though...  🚧
* Update fixtures to use our new MQTT mock 🎨

* 📝 Update base code according to comments

* 🔨 Adjust MQTT test base

* 🔨 Update other MQTT tests

* 🍎 Fix carriage return in source files

Apparently test_mqtt_json.py and test_mqtt_template.py were written on Windows. In order to not mess up the diff, I'll just redo the carriage return.

* 🎨 Remove unused import

* 📝 Remove fire_mqtt_client_message

* 🐛 Fix using python 3.6 method

What's very interesting is that 3.4 didn't fail on travis...

* 🐛 Fix using assert directly
This commit is contained in:
Otto Winter 2018-02-11 18:17:58 +01:00 committed by Paulus Schoutsen
parent 17e5740a0c
commit b1c0cabe6c
15 changed files with 1531 additions and 1490 deletions

View file

@ -116,16 +116,17 @@ class TestCoverMQTT(unittest.TestCase):
cover.open_cover(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'OPEN', 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'command-topic', 'OPEN', 0, False)
self.mock_publish.async_publish.reset_mock()
state = self.hass.states.get('cover.test')
self.assertEqual(STATE_OPEN, state.state)
cover.close_cover(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'CLOSE', 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'command-topic', 'CLOSE', 0, False)
state = self.hass.states.get('cover.test')
self.assertEqual(STATE_CLOSED, state.state)
@ -147,8 +148,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.open_cover(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'OPEN', 2, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'command-topic', 'OPEN', 2, False)
state = self.hass.states.get('cover.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -170,8 +171,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.close_cover(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'CLOSE', 2, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'command-topic', 'CLOSE', 2, False)
state = self.hass.states.get('cover.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -193,8 +194,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.stop_cover(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('command-topic', 'STOP', 2, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'command-topic', 'STOP', 2, False)
state = self.hass.states.get('cover.test')
self.assertEqual(STATE_UNKNOWN, state.state)
@ -295,8 +296,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.set_cover_position(self.hass, 100, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('position-topic', '38', 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'position-topic', '38', 0, False)
def test_set_position_untemplated(self):
"""Test setting cover position via template."""
@ -316,8 +317,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.set_cover_position(self.hass, 62, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('position-topic', 62, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'position-topic', 62, 0, False)
def test_no_command_topic(self):
"""Test with no command topic."""
@ -401,14 +402,15 @@ class TestCoverMQTT(unittest.TestCase):
cover.open_cover_tilt(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('tilt-command-topic', 100, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'tilt-command-topic', 100, 0, False)
self.mock_publish.async_publish.reset_mock()
cover.close_cover_tilt(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('tilt-command-topic', 0, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'tilt-command-topic', 0, 0, False)
def test_tilt_given_value(self):
"""Test tilting to a given value."""
@ -432,14 +434,15 @@ class TestCoverMQTT(unittest.TestCase):
cover.open_cover_tilt(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('tilt-command-topic', 400, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'tilt-command-topic', 400, 0, False)
self.mock_publish.async_publish.reset_mock()
cover.close_cover_tilt(self.hass, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('tilt-command-topic', 125, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'tilt-command-topic', 125, 0, False)
def test_tilt_via_topic(self):
"""Test tilt by updating status via MQTT."""
@ -538,8 +541,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.set_cover_tilt_position(self.hass, 50, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('tilt-command-topic', 50, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'tilt-command-topic', 50, 0, False)
def test_tilt_position_altered_range(self):
"""Test tilt via method invocation with altered range."""
@ -565,8 +568,8 @@ class TestCoverMQTT(unittest.TestCase):
cover.set_cover_tilt_position(self.hass, 50, 'cover.test')
self.hass.block_till_done()
self.assertEqual(('tilt-command-topic', 25, 0, False),
self.mock_publish.mock_calls[-2][1])
self.mock_publish.async_publish.assert_called_once_with(
'tilt-command-topic', 25, 0, False)
def test_find_percentage_in_range_defaults(self):
"""Test find percentage in range with default range."""