@@ -64,7 +64,6 @@ def __init__(
64
64
debug = debug ,
65
65
)
66
66
self ._mqtt_client = None
67
- self .mqtt_connect = None
68
67
69
68
def init_io_mqtt (self ):
70
69
"""Initialize MQTT for Adafruit IO"""
@@ -99,34 +98,50 @@ def init_mqtt(
99
98
)
100
99
if use_io :
101
100
self ._mqtt_client = IO_MQTT (self ._mqtt_client )
102
- self .mqtt_connect = self ._mqtt_client .connect
103
101
104
102
return self ._mqtt_client
105
103
106
104
# pylint: enable=too-many-arguments
107
105
108
106
def _get_mqtt_client (self ):
107
+ print (self ._mqtt_client )
109
108
if self ._mqtt_client is not None :
110
109
return self ._mqtt_client
111
110
raise RuntimeError ("Please initialize MQTT before using" )
112
111
113
- def mqtt_loop (self , * args , ** kwargs ):
112
+ def mqtt_loop (self , * args , suppress_mqtt_errors = True , ** kwargs ):
114
113
"""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 :
116
124
if self ._mqtt_client is not None :
117
125
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 ))
122
126
123
- def mqtt_publish (self , * args , ** kwargs ):
127
+ def mqtt_publish (self , * args , suppress_mqtt_errors = True , ** kwargs ):
124
128
"""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 :
126
137
if self ._mqtt_client is not None :
127
138
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 )
130
145
131
146
@property
132
147
def on_mqtt_connect (self ):
0 commit comments