connection blah

This commit is contained in:
Lynn Ochs 2023-06-05 21:10:51 +02:00
parent d754bb0861
commit 3d9dece330

View File

@ -43,7 +43,7 @@ class Value:
if remote_update_callback is not None: if remote_update_callback is not None:
_logger.debug("Subscribing to topic %s", self.topic) _logger.debug("Subscribing to topic %s", self.topic)
self.client.message_callback_add(self.topic, remote_update_callback) self.client.my_message_callback_add(self.topic, remote_update_callback)
while self.client.subscribe(self.topic, 2)[1] is None: while self.client.subscribe(self.topic, 2)[1] is None:
time.sleep(0.5) time.sleep(0.5)
_logger.warning("Retrying...") _logger.warning("Retrying...")
@ -84,8 +84,17 @@ class Value:
def reconnecting_client(host: str, *, keepalive: int = 60): def reconnecting_client(host: str, *, keepalive: int = 60):
_logger.debug("Creating MQTT client with keepalive interval %d", keepalive) _logger.debug("Creating MQTT client with keepalive interval %d", keepalive)
client = Client() client = Client()
client.on_connect = lambda client, userdata, flags, rc: \ client.mmcbdata = {}
def mmcb(topic, cb):
client.mmcbdata[topic] = cb
client.message_callback_add(topic, cb)
def on_connect(client, userdata, flags, rc):
_logger.debug("Connected to mqtt host") _logger.debug("Connected to mqtt host")
for topic, cb in client.mmcbdata.items():
_logger.debug("Re-adding subscription for %s", topic);
client.message_callback_add(topic, cb)
client.my_message_callback_add = mmcb
client.on_connect = on_connect
client.on_disconnect = lambda client, userdata, rc: \ client.on_disconnect = lambda client, userdata, rc: \
_logger.debug("Disconnected from mqtt host") _logger.debug("Disconnected from mqtt host")
# client.enable_logger(_logger.getChild("paho")) # client.enable_logger(_logger.getChild("paho"))