env: Python 3.9 paho.MQTT on RasPi OS or Mac OS 12.1:
Trying to get on_log set up for debugging a periodic connection issue, but the callback isn't being triggered. All other callbacks are working as expected. I added an enable_logging and it works but I'd like to use the callback to control the volume of log entries. The pertinent code is below. Thanks.
class MQ:
"""
Container and methods for MQ related tasks
"""
def __init__(self):
self.connected = False
self.client = None
self.msg_time = datetime(2000, 1, 1)
self.mq_topics = {
}
# Instantiate MQ and set up callbacks
self.client = mqtt.Client()
self.client.on_message = self.on_message
self.client.on_connect = self.on_connect
self.client.on_publish = self.on_publish
self.client.on_subscribe = self.on_subscribe
self.client.on_log = self.on_log
logger.debug("MQ client instantiation complete")
self.client.enable_logger(logger=logger)
# Issue connect request to the MQ broker. This will loop forever waiting for the broker to become available
logger.debug("Waiting for MQ broker to become available")
while not self.connected:
try:
self.client.connect("192.168.0.18", 1884, 30)
except IOError as e:
if e.errno != ENETUNREACH:
raise
logger.warning('Network error - retrying')
sleep(5)
continue
except socket.timeout as e:
logger.warning("Socket timeout trying to connect to MQTT broker - will retry after delay")
sleep(30)
continue
logger.debug('Connect to MQ broker initiated')
break
logger.debug("Connect request successfully issued to MQ broker")
# Start processing MQ messages/responses to us
self.client.loop_start()
logger.debug("MQ processing loop started")
# This will loop forever until the connection to the MQ broker is complete
logger.debug("Waiting for MQTT broker to connect")
while not self.connected:
sleep(1)
logger.debug("Successfully connected to MQ broker")
def subscribe(self, topic):
self.client.subscribe(topic, 0)
logger.debug("Subscribe request for " + topic)
def stop(self):
# Stop the MQ processing of messages/responses
self.client.loop_stop()
logger.debug("MQ processing loop terminated")
self.client.disconnect()
logger.info("MQ disconnected from broker")
def on_connect(self, mq_client, obj, flags, rc):
if rc == 0:
logger.debug("(Re)connected to HomeAssist MQ broker")
self.connected = True
else:
logger.warning("Error connecting to MQTT broker; rc={}".format(rc))
def on_disconnect(self, mq_client, obj, flags, rc):
logger.debug("MQ Disconnected: rc = " + str(rc))
def on_message(self, mq_client, obj, msg):
global MQ_topics
value_string = msg.payload.decode('utf-8')
topics = msg.topic.split('/')
logger.debug(f"MQ msg: topic={msg.topic}, value={value_string}")
# Message processing removed for brevity
def on_publish(self, mq_client, obj, mid):
logger.debug("Publish confirmed: id = " + str(mid))
def on_subscribe(self, mq_client, obj, mid, granted_qos):
logger.debug("Subscribed: id = " + str(mid) + ", qos = " + str(granted_qos))
def on_log(self, mq_client, obj, level, string):
# verb = string.split('(').strip()
# if verb[0] in ['Sending PINGREQ', 'Received PINGRESP', 'Received PUBLISH']:
# return
logger.debug('LOG: {}'.format(string))
# end class MQ
# Instantiation of MQ in main
MQ_client = MQ()
MQ_client.subscribe("Tempest/outTemp_F")