Skip to content

Add missing "on_publish" event callback in the class IO_MQTT - adafruit_io.py #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ def __init__(self, mqtt_client):
self.on_message = None
self.on_subscribe = None
self.on_unsubscribe = None
self.on_publish = None
# MQTT event callbacks
self._client.on_connect = self._on_connect_mqtt
self._client.on_disconnect = self._on_disconnect_mqtt
self._client.on_message = self._on_message_mqtt
self._client.on_subscribe = self._on_subscribe_mqtt
self._client.on_unsubscribe = self._on_unsubscribe_mqtt
self._client.on_publish = self._on_publish_mqtt
self._connected = False

def __enter__(self):
Expand Down Expand Up @@ -201,6 +203,12 @@ def _on_message_mqtt(self, client, topic: str, payload: str):
)
self.on_message(self, topic_name, message)

# pylint: disable=not-callable, unused-argument
def _on_publish_mqtt(self, client, user_data, topic, pid):
"""Runs when the client calls on_publish."""
if self.on_publish is not None:
self.on_publish(self, user_data, topic, pid)

# pylint: disable=not-callable
def _on_subscribe_mqtt(self, client, user_data, topic, qos):
"""Runs when the client calls on_subscribe."""
Expand Down
10 changes: 10 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_feed_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def on_message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -116,6 +125,7 @@ def on_battery_msg(client, topic, message):
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = on_message
io.on_publish = publish

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
Expand Down
10 changes: 10 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -105,6 +114,7 @@ def message(client, feed_id, payload):
io.on_connect = connected
io.on_disconnect = disconnected
io.on_message = message
io.on_publish = publish

# Group name
group_name = "weatherstation"
Expand Down
10 changes: 10 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -105,6 +114,7 @@ def message(client, feed_id, payload):
io.on_connect = connected
io.on_disconnect = disconnected
io.on_message = message
io.on_publish = publish

# Connect to Adafruit IO
io.connect()
Expand Down
12 changes: 12 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_pubsub_rp2040.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,28 @@ def connected(client):
print("Connected to Adafruit IO! ")


# pylint: disable=unused-argument
def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def disconnected(client):
# Disconnected function will be called when the client disconnects.
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def on_led_msg(client, topic, message):
# Method called whenever user/feeds/led has a new value
print("New message on topic {0}: {1} ".format(topic, message))
Expand Down Expand Up @@ -90,6 +101,7 @@ def on_led_msg(client, topic, message):
io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe
io.on_publish = publish

# Set up a callback for the led feed
io.add_feed_callback("led", on_led_msg)
Expand Down
12 changes: 12 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ def connected(client):
client.subscribe("DemoFeed")


# pylint: disable=unused-argument
def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


# pylint: disable=unused-argument
def unsubscribe(client, userdata, topic, pid):
# This method is called when the client unsubscribes from a feed.
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
Expand All @@ -75,6 +77,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -104,6 +115,7 @@ def message(client, feed_id, payload):
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message
io.on_publish = publish

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
Expand Down
12 changes: 12 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ def connected(client):
client.subscribe("DemoFeed")


# pylint: disable=unused-argument
def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


# pylint: disable=unused-argument
def unsubscribe(client, userdata, topic, pid):
# This method is called when the client unsubscribes from a feed.
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
Expand All @@ -72,6 +74,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -103,6 +114,7 @@ def message(client, feed_id, payload):
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message
io.on_publish = publish

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
Expand Down
12 changes: 12 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_simpletest_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ def connected(client):
client.subscribe("DemoFeed")


# pylint: disable=unused-argument
def subscribe(client, userdata, topic, granted_qos):
# This method is called when the client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))


# pylint: disable=unused-argument
def unsubscribe(client, userdata, topic, pid):
# This method is called when the client unsubscribes from a feed.
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
Expand All @@ -59,6 +61,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -89,6 +100,7 @@ def message(client, feed_id, payload):
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message
io.on_publish = publish

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
Expand Down
10 changes: 10 additions & 0 deletions examples/adafruit_io_mqtt/adafruit_io_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
# This method is called when the client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -119,6 +128,7 @@ def message(client, feed_id, payload):
io.on_connect = connected
io.on_disconnect = disconnected
io.on_message = message
io.on_publish = publish

# Connect to Adafruit IO
io.connect()
Expand Down
10 changes: 10 additions & 0 deletions examples/adafruit_io_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def disconnected(client):
print("Disconnected from Adafruit IO!")


# pylint: disable=unused-argument
def publish(client, userdata, topic, pid):
"""This method is called when the client publishes data to a feed."""
print(f"Published to {topic} with PID {pid}")
if userdata is not None:
print("Published User data: ", end="")
print(userdata)


# pylint: disable=unused-argument
def message(client, feed_id, payload):
# Message function will be called when a subscribed feed has a new value.
Expand Down Expand Up @@ -120,6 +129,7 @@ def message(client, feed_id, payload):
io.on_subscribe = subscribe
io.on_unsubscribe = unsubscribe
io.on_message = message
io.on_publish = publish

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")
Expand Down
Loading