correct MQTT subscription filter (#7269)
* correct MQTT subscription filter * wildcard handling (#) fixed * wildcard handling (#) fixed * added tests for topic subscription like +/something/# * function names changed (line too long) * using raw strings for regular expression import order changed
This commit is contained in:
1 parent
570c5549a9
commit
0e08925259
2 files changed
+55
-6
No files matched your search
@@ -10,6 +10,7 @@ import os
|
||||
import socket
|
||||
import time
|
||||
import ssl
|
||||
import re
|
||||
import requests.certs
|
||||
|
||||
import voluptuous as vol
|
||||
@@ -639,12 +640,20 @@ def _raise_on_error(result):
|
||||
|
||||
def _match_topic(subscription, topic):
|
||||
"""Test if topic matches subscription."""
|
||||
reg_ex_parts = []
|
||||
suffix = ""
|
||||
if subscription.endswith('#'):
|
||||
return (subscription[:-2] == topic or
|
||||
topic.startswith(subscription[:-1]))
|
||||
|
||||
subscription = subscription[:-2]
|
||||
suffix = "(.*)"
|
||||
sub_parts = subscription.split('/')
|
||||
topic_parts = topic.split('/')
|
||||
for sub_part in sub_parts:
|
||||
if sub_part == "+":
|
||||
reg_ex_parts.append(r"([^\/]+)")
|
||||
else:
|
||||
reg_ex_parts.append(sub_part)
|
||||
|
||||
return (len(sub_parts) == len(topic_parts) and
|
||||
all(a == b for a, b in zip(sub_parts, topic_parts) if a != '+'))
|
||||
reg_ex = "^" + (r'\/'.join(reg_ex_parts)) + suffix + "$"
|
||||
|
||||
reg = re.compile(reg_ex)
|
||||
|
||||
return reg.match(topic) is not None
|
||||
Reference in new issue
Block a user