Skip to content

Commit 3b5d0a2

Browse files
author
brentru
committed
refactor to use an external mqtt client, not init one.
1 parent febfdb7 commit 3b5d0a2

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

adafruit_io/adafruit_io.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT
4040
"""
4141
import time
42-
#from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError
42+
from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError, AdafruitIO_MQTTError
4343

4444
__version__ = "0.0.0-auto.0"
4545
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Adafruit_IO.git"
@@ -48,21 +48,22 @@
4848
'User-Agent': 'AIO-CircuitPython/{0}'.format(__version__)
4949
}
5050

51-
class MQTT_API():
51+
class IO_MQTT():
5252
"""
5353
Client for interacting with the Adafruit IO MQTT API. The client establishes
5454
a secure connection to Adafruit IO by default.
5555
:param MiniMQTT mqtt_client: MiniMQTT Client object.
5656
:param bool secure: Enables a secure SSL/TLS connection with Adafruit IO.
5757
"""
5858
def __init__(self, mqtt_client, secure=True):
59-
self._user = aio_username
60-
self._key = aio_key
6159
# MiniMQTT Object
62-
print(type(mqtt_client))
63-
print('MQTT CLIENT: ', mqtt_client)
64-
self._client = mqtt_client
65-
# User-defined MQTT callback methods need to be init'd to none
60+
mqtt_client_type = str(type(mqtt_client))
61+
if 'MQTT' in mqtt_client_type:
62+
self._client = mqtt_client
63+
else:
64+
raise TypeError("This class requires a MiniMQTT client.")
65+
self._user = self._client._user
66+
# User-defined MQTT callback methods must be init'd to None
6667
self.on_connect = None
6768
self.on_disconnect = None
6869
self.on_message = None
@@ -78,25 +79,27 @@ def __init__(self, mqtt_client, secure=True):
7879
self._client.set_logger_level('DEBUG')
7980
self._connected = False
8081

81-
@property
82-
def is_connected(self):
83-
"""Returns if connected to Adafruit IO MQTT Broker."""
84-
return self._connected
85-
8682
def connect(self):
8783
"""Connects to the Adafruit IO MQTT Broker.
8884
Must be called before any other API methods are called.
8985
"""
90-
if self._connected:
91-
return
92-
self._client.connect()
93-
86+
try:
87+
self._client.connect()
88+
except error as err:
89+
AdafruitIO_MQTTError(err)
90+
return
91+
9492
def disconnect(self):
9593
"""Disconnects from Adafruit IO.
9694
"""
9795
if self._connected:
9896
self._client.disconnect()
9997

98+
@property
99+
def is_connected(self):
100+
"""Returns if connected to Adafruit IO MQTT Broker."""
101+
return self._client.is_connected
102+
100103
def _on_connect_mqtt(self, client, userdata, flags, rc):
101104
"""Runs when the on_connect callback is run from code.
102105
"""
@@ -188,10 +191,10 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
188191
189192
client.subscribe([('temperature'), ('humidity')])
190193
"""
194+
print('sub called!')
191195
if shared_user is not None and feed_key is not None:
192196
self._client.subscribe('{0}/feeds/{1}'.format(shared_user, feed_key))
193197
elif group_key is not None:
194-
print('subscribing to group...')
195198
self._client.subscribe('{0}/groups/{1}'.format(self._user, group_key))
196199
elif feed_key is not None:
197200
self._client.subscribe('{0}/feeds/{1}'.format(self._user, feed_key))

0 commit comments

Comments
 (0)