diff --git a/Adafruit_IO/_version.py b/Adafruit_IO/_version.py index 202075f..3a5935a 100644 --- a/Adafruit_IO/_version.py +++ b/Adafruit_IO/_version.py @@ -1 +1 @@ -__version__ = "2.3" +__version__ = "2.3.1" diff --git a/Adafruit_IO/mqtt_client.py b/Adafruit_IO/mqtt_client.py index 8272ff1..d70118f 100644 --- a/Adafruit_IO/mqtt_client.py +++ b/Adafruit_IO/mqtt_client.py @@ -103,23 +103,26 @@ def _mqtt_disconnect(self, client, userdata, rc): self.on_disconnect(self) def _mqtt_message(self, client, userdata, msg): - logger.debug('Client on_message called.') """Parse out the topic and call on_message callback assume topic looks like `username/topic/id` """ + logger.debug('Client on_message called.') parsed_topic = msg.topic.split('/') - if self.on_message is not None and parsed_topic[2] == 'weather': - topic = parsed_topic[4] # parse out the forecast type - payload = '' if msg.payload is None else msg.payload.decode('utf-8') - elif self.on_message is not None and parsed_topic[0] == 'time': - topic = parsed_topic[0] - payload = msg.payload.decode('utf-8') - elif self.on_message is not None and parsed_topic[1] == 'groups': - topic = parsed_topic[3] - payload = msg.payload.decode('utf-8') - else: # default topic - topic = parsed_topic[2] - payload = '' if msg.payload is None else msg.payload.decode('utf-8') + if self.on_message is not None: + if parsed_topic[0] == 'time': + topic = parsed_topic[0] + payload = msg.payload.decode('utf-8') + elif parsed_topic[1] == 'groups': + topic = parsed_topic[3] + payload = msg.payload.decode('utf-8') + elif parsed_topic[2] == 'weather': + topic = parsed_topic[4] + payload = '' if msg.payload is None else msg.payload.decode('utf-8') + else: + topic = parsed_topic[2] + payload = '' if msg.payload is None else msg.payload.decode('utf-8') + else: + raise ValueError('on_message not defined') self.on_message(self, topic, payload) def _mqtt_subscribe(client, userdata, mid, granted_qos): diff --git a/examples/mqtt/mqtt_time.py b/examples/mqtt/mqtt_time.py index e91f820..19875d3 100644 --- a/examples/mqtt/mqtt_time.py +++ b/examples/mqtt/mqtt_time.py @@ -48,17 +48,14 @@ def message(client, feed_id, payload): # Connect to the Adafruit IO server. client.connect() -# time per loop -loop_time = 2 +# Subscribe to the time feeds +print('* Subscribing to time/seconds') +client.subscribe_time('seconds') -client.loop_background() -while True: - print('* Subscribing to /time/seconds') - client.subscribe_time('seconds') - time.sleep(loop_time) - print('* Subscribing to /time/millis') - client.subscribe_time('millis') - time.sleep(loop_time) - print('* Subscribing to iso-8601') - client.subscribe_time('iso') - time.sleep(loop_time) +print('* Subscribing to time/millis') +client.subscribe_time('millis') + +print('* Subscribing to time/ISO-8601') +client.subscribe_time('iso') + +client.loop_blocking()