Files
py-eagle-mqtt/Docker/src/python/tHome/broker/connect.py
Evan Richardson ad0b0e45ef
All checks were successful
Build and publish Image / build-and-push (push) Successful in 17s
try placing callback version
2024-03-06 23:42:05 +00:00

45 lines
1.3 KiB
Python

#===========================================================================
#
# Broker connection
#
#===========================================================================
from . import config
import paho.mqtt.client as mqtt
#===========================================================================
class Client( mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) ):
"""Logging client
"""
def __init__( self, log=None ):
mqtt.Client.__init__( self )
self._logger = log
# Restore callbacks overwritten by stupid mqtt library
self.on_log = Client.on_log
def on_log( self, userData, level, buf ):
if self._logger:
self._logger.log( level, buf )
#===========================================================================
def connect( configDir, log, client=None ):
cfg = config.parse( configDir )
if client is None:
client = Client( log )
if cfg.user:
client.username_pw_set( cfg.user, cfg.password )
if cfg.ca_certs:
client.tls_set( cfg.ca_certs, cfg.certFile, cfg.keyFile )
log.info( "Connecting to broker at %s:%d" % ( cfg.host, cfg.port ) )
client.connect( cfg.host, cfg.port, cfg.keepAlive )
return client
#===========================================================================