@@ -64,7 +64,6 @@ def __init__(
6464 debug = debug ,
6565 )
6666 self ._mqtt_client = None
67- self .mqtt_connect = None
6867
6968 def init_io_mqtt (self ):
7069 """Initialize MQTT for Adafruit IO"""
@@ -99,34 +98,50 @@ def init_mqtt(
9998 )
10099 if use_io :
101100 self ._mqtt_client = IO_MQTT (self ._mqtt_client )
102- self .mqtt_connect = self ._mqtt_client .connect
103101
104102 return self ._mqtt_client
105103
106104 # pylint: enable=too-many-arguments
107105
108106 def _get_mqtt_client (self ):
107+ print (self ._mqtt_client )
109108 if self ._mqtt_client is not None :
110109 return self ._mqtt_client
111110 raise RuntimeError ("Please initialize MQTT before using" )
112111
113- def mqtt_loop (self , * args , ** kwargs ):
112+ def mqtt_loop (self , * args , suppress_mqtt_errors = True , ** kwargs ):
114113 """Run the MQTT Loop"""
115- try :
114+ self ._get_mqtt_client ()
115+ if suppress_mqtt_errors :
116+ try :
117+ if self ._mqtt_client is not None :
118+ self ._mqtt_client .loop (* args , ** kwargs )
119+ except MQTT .MMQTTException as err :
120+ print ("MMQTTException: {0}" .format (err ))
121+ except OSError as err :
122+ print ("OSError: {0}" .format (err ))
123+ else :
116124 if self ._mqtt_client is not None :
117125 self ._mqtt_client .loop (* args , ** kwargs )
118- except MQTT .MMQTTException as err :
119- print ("MMQTTException: {0}" .format (err ))
120- except OSError as err :
121- print ("OSError: {0}" .format (err ))
122126
123- def mqtt_publish (self , * args , ** kwargs ):
127+ def mqtt_publish (self , * args , suppress_mqtt_errors = True , ** kwargs ):
124128 """Publish to MQTT"""
125- try :
129+ self ._get_mqtt_client ()
130+ if suppress_mqtt_errors :
131+ try :
132+ if self ._mqtt_client is not None :
133+ self ._mqtt_client .publish (* args , ** kwargs )
134+ except OSError as err :
135+ print ("OSError: {0}" .format (err ))
136+ else :
126137 if self ._mqtt_client is not None :
127138 self ._mqtt_client .publish (* args , ** kwargs )
128- except OSError as err :
129- print ("OSError: {0}" .format (err ))
139+
140+ def mqtt_connect (self , * args , ** kwargs ):
141+ """Connect to MQTT"""
142+ self ._get_mqtt_client ()
143+ if self ._mqtt_client is not None :
144+ self ._mqtt_client .connect (* args , ** kwargs )
130145
131146 @property
132147 def on_mqtt_connect (self ):
0 commit comments