From 9d6c2e99db769476f08a3c6cfdaf9bbc6ddca2f2 Mon Sep 17 00:00:00 2001 From: Sohaib90 Date: Thu, 19 Jan 2023 20:59:18 +0100 Subject: [PATCH 1/2] fixed topic population in subscribe() for tuple/list type topics --- flask_mqtt/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/flask_mqtt/__init__.py b/flask_mqtt/__init__.py index 9d177b4..92b5a04 100644 --- a/flask_mqtt/__init__.py +++ b/flask_mqtt/__init__.py @@ -276,7 +276,7 @@ def decorator(handler: Callable[[str], None]) -> Callable[[str], None]: return decorator - def subscribe(self, topic: str, qos: int = 0) -> Tuple[int, int]: + def subscribe(self, topic, qos: int = 0) -> Tuple[int, int]: """ Subscribe to a certain topic. @@ -311,7 +311,12 @@ def subscribe(self, topic: str, qos: int = 0) -> Tuple[int, int]: # if successful add to topics if result == MQTT_ERR_SUCCESS: - self.topics[topic] = TopicQos(topic=topic, qos=qos) + if isinstance(topic, tuple): + topic, qos = topic + self.topics[topic] = TopicQos(topic=topic, qos=qos) + elif isinstance(topic, list): + for t, q in topic: + self.topics[t] = TopicQos(topic=t, qos=q) logger.debug("Subscribed to topic: {0}, qos: {1}".format(topic, qos)) else: logger.error("Error {0} subscribing to topic: {1}".format(result, topic)) From 60df0928c1fa0c2814c80d7cae4b5195a59c7696 Mon Sep 17 00:00:00 2001 From: Sohaib90 Date: Thu, 19 Jan 2023 21:05:18 +0100 Subject: [PATCH 2/2] added default topic,qos to self.topics --- flask_mqtt/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flask_mqtt/__init__.py b/flask_mqtt/__init__.py index 92b5a04..356d524 100644 --- a/flask_mqtt/__init__.py +++ b/flask_mqtt/__init__.py @@ -317,6 +317,8 @@ def subscribe(self, topic, qos: int = 0) -> Tuple[int, int]: elif isinstance(topic, list): for t, q in topic: self.topics[t] = TopicQos(topic=t, qos=q) + else: + self.topics[topic] = TopicQos(topic=topic, qos=qos) logger.debug("Subscribed to topic: {0}, qos: {1}".format(topic, qos)) else: logger.error("Error {0} subscribing to topic: {1}".format(result, topic))