Skip to content

Commit 10d374a

Browse files
committed
Add missing "on_publish" event callback in the class IO_MQTT - adafruit_io.py
Fixes #122
1 parent 2076935 commit 10d374a

9 files changed

+96
-0
lines changed

adafruit_io/adafruit_io.py

+8
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ def __init__(self, mqtt_client):
9898
self.on_message = None
9999
self.on_subscribe = None
100100
self.on_unsubscribe = None
101+
self.on_publish = None
101102
# MQTT event callbacks
102103
self._client.on_connect = self._on_connect_mqtt
103104
self._client.on_disconnect = self._on_disconnect_mqtt
104105
self._client.on_message = self._on_message_mqtt
105106
self._client.on_subscribe = self._on_subscribe_mqtt
106107
self._client.on_unsubscribe = self._on_unsubscribe_mqtt
108+
self._client.on_publish = self._on_publish_mqtt
107109
self._connected = False
108110

109111
def __enter__(self):
@@ -201,6 +203,12 @@ def _on_message_mqtt(self, client, topic: str, payload: str):
201203
)
202204
self.on_message(self, topic_name, message)
203205

206+
# pylint: disable=not-callable, unused-argument
207+
def _on_publish_mqtt(self, client, user_data, pid):
208+
"""Runs when the client calls on_publish."""
209+
if self.on_publish is not None:
210+
self.on_publish(self, user_data, pid)
211+
204212
# pylint: disable=not-callable
205213
def _on_subscribe_mqtt(self, client, user_data, topic, qos):
206214
"""Runs when the client calls on_subscribe."""

examples/adafruit_io_mqtt/adafruit_io_feed_callback.py

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ def disconnected(client):
7676
print("Disconnected from Adafruit IO!")
7777

7878

79+
# pylint: disable=unused-argument
80+
def publish(client, userdata, topic, pid):
81+
# This method is called when the client publishes data to a feed.
82+
print("Published to {0} with PID {1}".format(topic, pid))
83+
if userdata is not None:
84+
print("Published User data: ",end="")
85+
print(userdata)
86+
87+
7988
# pylint: disable=unused-argument
8089
def on_message(client, feed_id, payload):
8190
# Message function will be called when a subscribed feed has a new value.
@@ -116,6 +125,7 @@ def on_battery_msg(client, topic, message):
116125
io.on_subscribe = subscribe
117126
io.on_unsubscribe = unsubscribe
118127
io.on_message = on_message
128+
io.on_publish = publish
119129

120130
# Connect to Adafruit IO
121131
print("Connecting to Adafruit IO...")

examples/adafruit_io_mqtt/adafruit_io_groups.py

+10
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def disconnected(client):
7272
print("Disconnected from Adafruit IO!")
7373

7474

75+
# pylint: disable=unused-argument
76+
def publish(client, userdata, topic, pid):
77+
# This method is called when the client publishes data to a feed.
78+
print("Published to {0} with PID {1}".format(topic, pid))
79+
if userdata is not None:
80+
print("Published User data: ", end="")
81+
print(userdata)
82+
83+
7584
# pylint: disable=unused-argument
7685
def message(client, feed_id, payload):
7786
# Message function will be called when a subscribed feed has a new value.
@@ -105,6 +114,7 @@ def message(client, feed_id, payload):
105114
io.on_connect = connected
106115
io.on_disconnect = disconnected
107116
io.on_message = message
117+
io.on_publish = publish
108118

109119
# Group name
110120
group_name = "weatherstation"

examples/adafruit_io_mqtt/adafruit_io_location.py

+10
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def disconnected(client):
7272
print("Disconnected from Adafruit IO!")
7373

7474

75+
# pylint: disable=unused-argument
76+
def publish(client, userdata, topic, pid):
77+
# This method is called when the client publishes data to a feed.
78+
print("Published to {0} with PID {1}".format(topic, pid))
79+
if userdata is not None:
80+
print("Published User data: ", end="")
81+
print(userdata)
82+
83+
7584
# pylint: disable=unused-argument
7685
def message(client, feed_id, payload):
7786
# Message function will be called when a subscribed feed has a new value.
@@ -105,6 +114,7 @@ def message(client, feed_id, payload):
105114
io.on_connect = connected
106115
io.on_disconnect = disconnected
107116
io.on_message = message
117+
io.on_publish = publish
108118

109119
# Connect to Adafruit IO
110120
io.connect()

examples/adafruit_io_mqtt/adafruit_io_pubsub_rp2040.py

+12
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,28 @@ def connected(client):
4343
print("Connected to Adafruit IO! ")
4444

4545

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

5051

52+
# pylint: disable=unused-argument
53+
def publish(client, userdata, topic, pid):
54+
# This method is called when the client publishes data to a feed.
55+
print("Published to {0} with PID {1}".format(topic, pid))
56+
if userdata is not None:
57+
print("Published User data: ",end="")
58+
print(userdata)
59+
60+
5161
# pylint: disable=unused-argument
5262
def disconnected(client):
5363
# Disconnected function will be called when the client disconnects.
5464
print("Disconnected from Adafruit IO!")
5565

5666

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

94106
# Set up a callback for the led feed
95107
io.add_feed_callback("led", on_led_msg)

examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py

+12
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ def connected(client):
5959
client.subscribe("DemoFeed")
6060

6161

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

6667

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

7779

80+
# pylint: disable=unused-argument
81+
def publish(client, userdata, topic, pid):
82+
# This method is called when the client publishes data to a feed.
83+
print("Published to {0} with PID {1}".format(topic, pid))
84+
if userdata is not None:
85+
print("Published User data: ",end="")
86+
print(userdata)
87+
88+
7889
# pylint: disable=unused-argument
7990
def message(client, feed_id, payload):
8091
# Message function will be called when a subscribed feed has a new value.
@@ -104,6 +115,7 @@ def message(client, feed_id, payload):
104115
io.on_subscribe = subscribe
105116
io.on_unsubscribe = unsubscribe
106117
io.on_message = message
118+
io.on_publish = publish
107119

108120
# Connect to Adafruit IO
109121
print("Connecting to Adafruit IO...")

examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py

+12
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ def connected(client):
5656
client.subscribe("DemoFeed")
5757

5858

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

6364

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

7476

77+
# pylint: disable=unused-argument
78+
def publish(client, userdata, topic, pid):
79+
# This method is called when the client publishes data to a feed.
80+
print("Published to {0} with PID {1}".format(topic, pid))
81+
if userdata is not None:
82+
print("Published User data: ",end="")
83+
print(userdata)
84+
85+
7586
# pylint: disable=unused-argument
7687
def message(client, feed_id, payload):
7788
# Message function will be called when a subscribed feed has a new value.
@@ -103,6 +114,7 @@ def message(client, feed_id, payload):
103114
io.on_subscribe = subscribe
104115
io.on_unsubscribe = unsubscribe
105116
io.on_message = message
117+
io.on_publish = publish
106118

107119
# Connect to Adafruit IO
108120
print("Connecting to Adafruit IO...")

examples/adafruit_io_mqtt/adafruit_io_simpletest_eth.py

+12
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ def connected(client):
4343
client.subscribe("DemoFeed")
4444

4545

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

5051

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

6163

64+
# pylint: disable=unused-argument
65+
def publish(client, userdata, topic, pid):
66+
# This method is called when the client publishes data to a feed.
67+
print("Published to {0} with PID {1}".format(topic, pid))
68+
if userdata is not None:
69+
print("Published User data: ", end="")
70+
print(userdata)
71+
72+
6273
# pylint: disable=unused-argument
6374
def message(client, feed_id, payload):
6475
# Message function will be called when a subscribed feed has a new value.
@@ -89,6 +100,7 @@ def message(client, feed_id, payload):
89100
io.on_subscribe = subscribe
90101
io.on_unsubscribe = unsubscribe
91102
io.on_message = message
103+
io.on_publish = publish
92104

93105
# Connect to Adafruit IO
94106
print("Connecting to Adafruit IO...")

examples/adafruit_io_mqtt/adafruit_io_time.py

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ def disconnected(client):
8686
print("Disconnected from Adafruit IO!")
8787

8888

89+
# pylint: disable=unused-argument
90+
def publish(client, userdata, topic, pid):
91+
# This method is called when the client publishes data to a feed.
92+
print("Published to {0} with PID {1}".format(topic, pid))
93+
if userdata is not None:
94+
print("Published User data: ", end="")
95+
print(userdata)
96+
97+
8998
# pylint: disable=unused-argument
9099
def message(client, feed_id, payload):
91100
# Message function will be called when a subscribed feed has a new value.
@@ -119,6 +128,7 @@ def message(client, feed_id, payload):
119128
io.on_connect = connected
120129
io.on_disconnect = disconnected
121130
io.on_message = message
131+
io.on_publish = publish
122132

123133
# Connect to Adafruit IO
124134
io.connect()

0 commit comments

Comments
 (0)