mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
MQTT: add support for + wildcard in subscribe
This commit is contained in:
parent
969fe1f3b9
commit
f79567f7db
@ -94,7 +94,8 @@ def subscribe(hass, topic, callback, qos=0):
|
|||||||
def mqtt_topic_subscriber(event):
|
def mqtt_topic_subscriber(event):
|
||||||
""" Match subscribed MQTT topic. """
|
""" Match subscribed MQTT topic. """
|
||||||
if _match_topic(topic, event.data[ATTR_TOPIC]):
|
if _match_topic(topic, event.data[ATTR_TOPIC]):
|
||||||
callback(topic, event.data[ATTR_PAYLOAD], event.data[ATTR_QOS])
|
callback(event.data[ATTR_TOPIC], event.data[ATTR_PAYLOAD],
|
||||||
|
event.data[ATTR_QOS])
|
||||||
|
|
||||||
hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED, mqtt_topic_subscriber)
|
hass.bus.listen(EVENT_MQTT_MESSAGE_RECEIVED, mqtt_topic_subscriber)
|
||||||
|
|
||||||
@ -244,7 +245,12 @@ def _raise_on_error(result):
|
|||||||
|
|
||||||
def _match_topic(subscription, topic):
|
def _match_topic(subscription, topic):
|
||||||
""" Returns if topic matches subscription. """
|
""" Returns if topic matches subscription. """
|
||||||
if not subscription.endswith('#'):
|
if subscription.endswith('#'):
|
||||||
return subscription == topic
|
return (subscription[:-2] == topic or
|
||||||
|
topic.startswith(subscription[:-1]))
|
||||||
|
|
||||||
return subscription[:-2] == topic or topic.startswith(subscription[:-1])
|
sub_parts = subscription.split('/')
|
||||||
|
topic_parts = topic.split('/')
|
||||||
|
|
||||||
|
return (len(sub_parts) == len(topic_parts) and
|
||||||
|
all(a == b for a, b in zip(sub_parts, topic_parts) if a != '+'))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user