@@ -65,7 +65,7 @@ def __init__(
6565 )
6666 self ._mqtt_client = None
6767 self .mqtt_connect = None
68- self .mqtt_publish = None
68+ self ._mqtt_publish = None
6969
7070 def init_io_mqtt (self ):
7171 """Initialize MQTT for Adafruit IO"""
@@ -101,7 +101,7 @@ def init_mqtt(
101101 if use_io :
102102 self ._mqtt_client = IO_MQTT (self ._mqtt_client )
103103 self .mqtt_connect = self ._mqtt_client .connect
104- self .mqtt_publish = self ._mqtt_client .publish
104+ self ._mqtt_publish = self ._mqtt_client .publish
105105
106106 return self ._mqtt_client
107107
@@ -120,6 +120,14 @@ def mqtt_loop(self):
120120 except MQTT .MMQTTException as err :
121121 print ("MMQTTException: {0}" .format (err ))
122122
123+ def mqtt_publish (self , * args , ** kwargs ):
124+ """Publish to MQTT"""
125+ try :
126+ if self ._mqtt_client is not None :
127+ self ._mqtt_publish (* args , ** kwargs )
128+ except OSError as err :
129+ print ("OSError: {0}" .format (err ))
130+
123131 @property
124132 def on_mqtt_connect (self ):
125133 """
@@ -147,8 +155,7 @@ def on_mqtt_disconnect(self):
147155
148156 @on_mqtt_disconnect .setter
149157 def on_mqtt_disconnect (self , value ):
150- self ._get_mqtt_client ()
151- self ._mqtt_client .on_disconnect = value
158+ self ._get_mqtt_client ().on_disconnect = value
152159
153160 @property
154161 def on_mqtt_subscribe (self ):
@@ -162,8 +169,21 @@ def on_mqtt_subscribe(self):
162169
163170 @on_mqtt_subscribe .setter
164171 def on_mqtt_subscribe (self , value ):
165- self ._get_mqtt_client ()
166- self ._mqtt_client .on_subscribe = value
172+ self ._get_mqtt_client ().on_subscribe = value
173+
174+ @property
175+ def on_mqtt_unsubscribe (self ):
176+ """
177+ Get or Set the MQTT Unsubscribe Handler
178+
179+ """
180+ if self ._mqtt_client :
181+ return self ._mqtt_client .on_unsubscribe
182+ return None
183+
184+ @on_mqtt_unsubscribe .setter
185+ def on_mqtt_unsubscribe (self , value ):
186+ self ._get_mqtt_client ().on_unsubscribe = value
167187
168188 @property
169189 def on_mqtt_message (self ):
@@ -177,8 +197,7 @@ def on_mqtt_message(self):
177197
178198 @on_mqtt_message .setter
179199 def on_mqtt_message (self , value ):
180- self ._get_mqtt_client ()
181- self ._mqtt_client .on_message = value
200+ self ._get_mqtt_client ().on_message = value
182201
183202 @property
184203 def enabled (self ):
0 commit comments