From a5a4797a1d4f5408deb9a116a1281837c050a8ff Mon Sep 17 00:00:00 2001 From: Friedo <8314092+unconfigured@users.noreply.github.com> Date: Wed, 28 Nov 2018 15:52:52 +0100 Subject: [PATCH 1/4] Fix mqtt prefix handling on thermostat worker (#30) --- mqtt.py | 2 ++ workers/thermostat.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mqtt.py b/mqtt.py index 88037ed..2f94792 100644 --- a/mqtt.py +++ b/mqtt.py @@ -59,6 +59,8 @@ class MqttClient: self.mqttc.connect(self.hostname, port=self.port) for topic, callback in callbacks: + if self.topic_prefix is not None: + topic = "{}/{}".format(self.topic_prefix, topic) self.mqttc.message_callback_add(topic, callback) self.mqttc.subscribe(topic) diff --git a/workers/thermostat.py b/workers/thermostat.py index de4fc14..3f4dc8e 100644 --- a/workers/thermostat.py +++ b/workers/thermostat.py @@ -71,7 +71,9 @@ class ThermostatWorker(BaseWorker): return ret def on_command(self, topic, value): - _, device_name, method, _ = topic.split('/') + if self.topic_prefix is not None: + _, device_name, method, _ = topic[len(self.topic_prefix+"/"):].split('/') + thermostat = self.devices[device_name] value = value.decode('utf-8') From 03bb93e2eba613a207432503b6ea155278c32c7c Mon Sep 17 00:00:00 2001 From: zewelor Date: Wed, 19 Dec 2018 10:31:17 +0100 Subject: [PATCH 2/4] Fix global topic prefix handling (#32) --- mqtt.py | 2 +- workers/thermostat.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mqtt.py b/mqtt.py index 2f94792..24f7f35 100644 --- a/mqtt.py +++ b/mqtt.py @@ -59,7 +59,7 @@ class MqttClient: self.mqttc.connect(self.hostname, port=self.port) for topic, callback in callbacks: - if self.topic_prefix is not None: + if self.topic_prefix: topic = "{}/{}".format(self.topic_prefix, topic) self.mqttc.message_callback_add(topic, callback) self.mqttc.subscribe(topic) diff --git a/workers/thermostat.py b/workers/thermostat.py index 3f4dc8e..1023a42 100644 --- a/workers/thermostat.py +++ b/workers/thermostat.py @@ -71,7 +71,7 @@ class ThermostatWorker(BaseWorker): return ret def on_command(self, topic, value): - if self.topic_prefix is not None: + if self.topic_prefix: _, device_name, method, _ = topic[len(self.topic_prefix+"/"):].split('/') thermostat = self.devices[device_name] From 0252aac8ae3b29d1402080d3e59330d56da9cfe6 Mon Sep 17 00:00:00 2001 From: Dennis Keitzel Date: Wed, 19 Dec 2018 10:36:02 +0100 Subject: [PATCH 3/4] Use a single mqtt connection for publish and subscribe operations instead of separate one-shot connections for each update (#35) --- mqtt.py | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/mqtt.py b/mqtt.py index 24f7f35..0ff9c13 100644 --- a/mqtt.py +++ b/mqtt.py @@ -1,29 +1,25 @@ -import paho.mqtt.publish as publish import paho.mqtt.client as mqtt + class MqttClient: def __init__(self, config): self._config = config self._mqttc = mqtt.Client(client_id=self.client_id, clean_session=False) - - if self.username is not None and self.password is not None: - self._auth = {'username': self.username, 'password': self.password} - else: - self._auth = None + + if self.username and self.password: + self.mqttc.username_pw_set(self.username, self.password) + + self.mqttc.connect(self.hostname, port=self.port) + self.mqttc.loop_start() def publish(self, messages): - if messages is None or len(messages) == 0: + if not messages: return - - if self.topic_prefix is not None: - for m in messages: - m.topic = "{}/{}".format(self.topic_prefix, m.topic) - - publish.multiple(list(map(lambda m: m.as_dict, messages)), - hostname=self.hostname, - port=self.port, - auth=self._auth) + + for m in messages: + topic = "{}/{}".format(self.topic_prefix, m.topic) if self.topic_prefix else m.topic + self.mqttc.publish(topic, m.payload) @property def client_id(self): @@ -54,18 +50,11 @@ class MqttClient: return self._mqttc def callbacks_subscription(self, callbacks): - if self.username is not None and self.password is not None: - self.mqttc.username_pw_set(self.username, self.password) - self.mqttc.connect(self.hostname, port=self.port) - for topic, callback in callbacks: - if self.topic_prefix: - topic = "{}/{}".format(self.topic_prefix, topic) + topic = "{}/{}".format(self.topic_prefix, topic) if self.topic_prefix else topic self.mqttc.message_callback_add(topic, callback) self.mqttc.subscribe(topic) - self.mqttc.loop_start() - class MqttMessage: def __init__(self, topic=None, payload=None): From 1dad41cbc42d958dba5b1e0b242ad23755b806dc Mon Sep 17 00:00:00 2001 From: zewelor Date: Wed, 19 Dec 2018 11:53:44 +0100 Subject: [PATCH 4/4] Revert wrong topic_prefix handling --- workers/thermostat.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/workers/thermostat.py b/workers/thermostat.py index 1023a42..52ea74f 100644 --- a/workers/thermostat.py +++ b/workers/thermostat.py @@ -71,8 +71,7 @@ class ThermostatWorker(BaseWorker): return ret def on_command(self, topic, value): - if self.topic_prefix: - _, device_name, method, _ = topic[len(self.topic_prefix+"/"):].split('/') + _, device_name, method, _ = topic.split('/') thermostat = self.devices[device_name]