Exemple #1
0
 def _on_connect_mqtt(self, client, userdata, flags, return_code):
     """Runs when the client calls on_connect."""
     if return_code == 0:
         self._connected = True
     else:
         raise AdafruitIO_MQTTError(return_code)
     # Call the user-defined on_connect callback if defined
     if self.on_connect is not None:
         self.on_connect(self)
 def connect(self):
     """Connects to the Adafruit IO MQTT Broker.
     Must be called before any other API methods are called.
     """
     try:
         self._client.connect()
     except Exception as err:
         raise AdafruitIO_MQTTError(
             "Unable to connect to Adafruit IO.") from err
 def publish_multiple(self, feeds_and_data, timeout=3, is_group=False):
     """Publishes multiple data points to multiple feeds or groups with a variable
     timeout.
     :param str feeds_and_data: List of tuples containing topic strings and data values.
     :param int timeout: Delay between publishing data points to Adafruit IO, in seconds.
     :param bool is_group: Set to True if you're publishing to a group.
     Example of publishing multiple data points on different feeds to Adafruit IO:
     ..code-block:: python
         client.publish_multiple([('humidity', 24.5), ('temperature', 54)])
     """
     if isinstance(feeds_and_data, list):
         feed_data = []
         for topic, data in feeds_and_data:
             feed_data.append((topic, data))
     else:
         raise AdafruitIO_MQTTError("This method accepts a list of tuples.")
     for topic, data in feed_data:
         if is_group:
             self.publish(topic, data, is_group=True)
         else:
             self.publish(topic, data)
         time.sleep(timeout)
    def unsubscribe(self, feed_key=None, group_key=None, shared_user=None):
        """Unsubscribes from an Adafruit IO feed or group.
        Can also subscribe to someone else's feed.
        :param str feed_key: Adafruit IO Feed key.
        :param str group_key: Adafruit IO Group key.
        :param str shared_user: Owner of the Adafruit IO feed, required for shared feeds.

        Example of unsubscribing from a feed.

        .. code-block:: python

            client.unsubscribe('temperature')

        Example of unsubscribing from two feeds: 'temperature'
        and 'humidity'

        .. code-block:: python

            client.unsubscribe([('temperature'), ('humidity')])

        Example of unsubscribing from a shared feed.

        .. code-block:: python

            client.unsubscribe('temperature', shared_user='******')

        """
        if shared_user is not None and feed_key is not None:
            self._client.unsubscribe("{0}/feeds/{1}".format(
                shared_user, feed_key))
        elif group_key is not None:
            self._client.unsubscribe("{0}/groups/{1}".format(
                self._user, feed_key))
        elif feed_key is not None:
            self._client.unsubscribe("{0}/feeds/{1}".format(
                self._user, feed_key))
        else:
            raise AdafruitIO_MQTTError("Must provide a feed_key or group_key.")
Exemple #5
0
 def reconnect(self):
     """Attempts to reconnect to the Adafruit IO MQTT Broker."""
     try:
         self._client.reconnect()
     except Exception as err:
         raise AdafruitIO_MQTTError("Unable to reconnect to Adafruit IO.") from err