From 969fe1f3b9a716a27cb83740e40ab79c04afecfc Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 10 Aug 2015 18:12:22 -0700 Subject: [PATCH] Fix MQTT wildcard topic subscriptions --- homeassistant/components/mqtt.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/mqtt.py b/homeassistant/components/mqtt.py index 7f6b0c11b5f..be50cc31175 100644 --- a/homeassistant/components/mqtt.py +++ b/homeassistant/components/mqtt.py @@ -92,8 +92,8 @@ def publish(hass, topic, payload): def subscribe(hass, topic, callback, qos=0): """ Subscribe to a topic. """ def mqtt_topic_subscriber(event): - """ Subscribes to a specific MQTT topic. """ - if event.data[ATTR_TOPIC] == topic: + """ Match subscribed MQTT topic. """ + if _match_topic(topic, event.data[ATTR_TOPIC]): callback(topic, event.data[ATTR_PAYLOAD], event.data[ATTR_QOS]) hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED, mqtt_topic_subscriber) @@ -240,3 +240,11 @@ def _raise_on_error(result): """ Raise error if error result. """ if result != 0: raise HomeAssistantError('Error talking to MQTT: {}'.format(result)) + + +def _match_topic(subscription, topic): + """ Returns if topic matches subscription. """ + if not subscription.endswith('#'): + return subscription == topic + + return subscription[:-2] == topic or topic.startswith(subscription[:-1])