From 8770c625cc291905fb72ae1e056b0161aa037392 Mon Sep 17 00:00:00 2001 From: unconfigured <8314092+unconfigured@users.noreply.github.com> Date: Wed, 28 Nov 2018 11:53:06 +0100 Subject: [PATCH 1/2] Fix mqtt prefix handling on thermostat worker --- 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 f9f95a756b54d29a8e9f5f273be04e18caff7236 Mon Sep 17 00:00:00 2001 From: unconfigured <8314092+unconfigured@users.noreply.github.com> Date: Thu, 20 Dec 2018 12:27:31 +0100 Subject: [PATCH 2/2] global topic_prefix fix try 2 --- mqtt.py | 4 +++- workers_manager.py | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mqtt.py b/mqtt.py index 0ff9c13..0d9e4c8 100644 --- a/mqtt.py +++ b/mqtt.py @@ -5,7 +5,9 @@ class MqttClient: def __init__(self, config): self._config = config - self._mqttc = mqtt.Client(client_id=self.client_id, clean_session=False) + self._mqttc = mqtt.Client(client_id=self.client_id, + clean_session=False, + userdata = {'global_topic_prefix': self.topic_prefix}) if self.username and self.password: self.mqttc.username_pw_set(self.username, self.password) diff --git a/workers_manager.py b/workers_manager.py index 616d77e..15c039d 100644 --- a/workers_manager.py +++ b/workers_manager.py @@ -102,6 +102,8 @@ class WorkersManager: for package in package_names: pip_main(['install', '-q', package]) - def _on_command_wrapper(self, worker_obj, client, _, c): + def _on_command_wrapper(self, worker_obj, client, userdata, c): _LOGGER.debug("on command wrapper for with %s: %s", c.topic, c.payload) - self._queue_command(self.Command(worker_obj.on_command, [c.topic, c.payload])) + global_topic_prefix = userdata['global_topic_prefix'] + topic = c.topic[len(global_topic_prefix+'/'):] if global_topic_prefix is not None else c.topic + self._queue_command(self.Command(worker_obj.on_command, [topic, c.payload]))