Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
CONF_PASSWORD, CONF_PORT, CONF_PROTOCOL, CONF_PAYLOAD)
from homeassistant.components.mqtt.server import HBMQTT_CONFIG_SCHEMA

REQUIREMENTS = ['paho-mqtt==1.2.3']
REQUIREMENTS = ['paho-mqtt==1.3.0']

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -102,7 +102,7 @@ def valid_discovery_topic(value):
_VALID_QOS_SCHEMA = vol.All(vol.Coerce(int), vol.In([0, 1, 2]))

CLIENT_KEY_AUTH_MSG = 'client_key and client_cert must both be present in ' \
'the mqtt broker config'
'the MQTT broker configuration'

MQTT_WILL_BIRTH_SCHEMA = vol.Schema({
vol.Required(ATTR_TOPIC): valid_publish_topic,
Expand All @@ -126,9 +126,8 @@ def valid_discovery_topic(value):
vol.Inclusive(CONF_CLIENT_CERT, 'client_key_auth',
msg=CLIENT_KEY_AUTH_MSG): cv.isfile,
vol.Optional(CONF_TLS_INSECURE): cv.boolean,
vol.Optional(CONF_TLS_VERSION,
default=DEFAULT_TLS_PROTOCOL): vol.Any('auto', '1.0',
'1.1', '1.2'),
vol.Optional(CONF_TLS_VERSION, default=DEFAULT_TLS_PROTOCOL):
vol.Any('auto', '1.0', '1.1', '1.2'),
vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL):
vol.All(cv.string, vol.In([PROTOCOL_31, PROTOCOL_311])),
vol.Optional(CONF_EMBEDDED): HBMQTT_CONFIG_SCHEMA,
Expand Down Expand Up @@ -237,9 +236,7 @@ def subscribe(hass, topic, msg_callback, qos=DEFAULT_QOS,
encoding='utf-8'):
"""Subscribe to an MQTT topic."""
async_remove = run_coroutine_threadsafe(
async_subscribe(hass, topic, msg_callback,
qos, encoding),
hass.loop
async_subscribe(hass, topic, msg_callback, qos, encoding), hass.loop
).result()

def remove():
Expand Down Expand Up @@ -649,7 +646,7 @@ def _match_topic(subscription, topic):
if sub_part == "+":
reg_ex_parts.append(r"([^\/]+)")
else:
reg_ex_parts.append(sub_part)
reg_ex_parts.append(re.escape(sub_part))

reg_ex = "^" + (r'\/'.join(reg_ex_parts)) + suffix + "$"

Expand Down
47 changes: 47 additions & 0 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,53 @@ def test_subscribe_topic_level_wildcard_and_wildcard_no_match(self):
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/$(.)[^]{-}'
payload = 'p4y.l[]a|> ?'

mqtt.subscribe(self.hass, topic, self.record_calls)

fire_mqtt_message(self.hass, topic, payload)
self.hass.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual(topic, self.calls[0][0])
self.assertEqual(payload, self.calls[0][1])

def test_subscribe_binary_topic(self):
"""Test the subscription to a binary topic."""
mqtt.subscribe(self.hass, 'test-topic', self.record_calls,
Expand Down