From 23400c4b0ae1da76115709789329ff358335d0c3 Mon Sep 17 00:00:00 2001 From: natemason Date: Tue, 27 Jun 2017 13:17:55 +0800 Subject: [PATCH] Fixed mqtt subscription filter on sys $ topics (#8166) * Fixed mqtt subscription filter on sys $ topics * fixed linting issue * added unit tests for $ topics and changed fix to use re.escape * merge upstream/dev mqtt unit tests * Update test_init.py --- tests/components/mqtt/test_init.py | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 3be3d5d5ef6..3d068224243 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -249,6 +249,40 @@ class TestMQTT(unittest.TestCase): self.hass.block_till_done() self.assertEqual(0, len(self.calls)) + def test_subscribe_topic_sys_root(self): + """Test the subscription of $ root topics.""" + mqtt.subscribe(self.hass, '$test-topic/subtree/on', self.record_calls) + + fire_mqtt_message(self.hass, '$test-topic/subtree/on', 'test-payload') + + self.hass.block_till_done() + self.assertEqual(1, len(self.calls)) + self.assertEqual('$test-topic/subtree/on', self.calls[0][0]) + self.assertEqual('test-payload', self.calls[0][1]) + + def test_subscribe_topic_sys_root_and_wildcard_topic(self): + """Test the subscription of $ root and wildcard topics.""" + mqtt.subscribe(self.hass, '$test-topic/#', self.record_calls) + + fire_mqtt_message(self.hass, '$test-topic/some-topic', 'test-payload') + + self.hass.block_till_done() + self.assertEqual(1, len(self.calls)) + self.assertEqual('$test-topic/some-topic', self.calls[0][0]) + self.assertEqual('test-payload', self.calls[0][1]) + + def test_subscribe_topic_sys_root_and_wildcard_subtree_topic(self): + """Test the subscription of $ root and wildcard subtree topics.""" + mqtt.subscribe(self.hass, '$test-topic/subtree/#', self.record_calls) + + fire_mqtt_message(self.hass, '$test-topic/subtree/some-topic', + 'test-payload') + + self.hass.block_till_done() + self.assertEqual(1, len(self.calls)) + self.assertEqual('$test-topic/subtree/some-topic', self.calls[0][0]) + self.assertEqual('test-payload', self.calls[0][1]) + def test_subscribe_special_characters(self): """Test the subscription to topics with special characters.""" topic = '/test-topic/$(.)[^]{-}'