Skip to content

Commit 1498e28

Browse files
committed
IO_HTTP: Support creating group data with send_group_data
Fixes #97
1 parent 2076935 commit 1498e28

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

adafruit_io/adafruit_io.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,10 +631,29 @@ def send_batch_data(self, feed_key: str, data_list: list):
631631
:param list Data: Data list to send
632632
"""
633633
validate_feed_key(feed_key)
634-
path = "feeds/{0}/data/batch".format(feed_key)
634+
path = self._compose_path("feeds/{0}/data/batch".format(feed_key))
635635
data_dict = type(data_list)((data._asdict() for data in data_list))
636636
self._post(path, {"data": data_dict})
637637

638+
def send_group_data(
639+
self, group_key: str, feeds_and_data: list, metadata: Optional[dict] = None
640+
):
641+
"""
642+
Sends data to specified Adafruit IO feeds in a group
643+
644+
:param str group_key: Adafruit IO feed key
645+
:param list feeds_and_data: A list of dicts, with feed "key" and "value" entries
646+
:param dict metadata: Optional metadata associated with the data e.g. created_at, lat, lon, ele
647+
"""
648+
validate_feed_key(group_key)
649+
path = self._compose_path("groups/{0}/data".format(group_key))
650+
if not isinstance(feeds_and_data, list):
651+
raise ValueError("This method accepts a list of dicts with \"key\" and \"value\".")
652+
if metadata is not None:
653+
self._post(path, {**metadata, "feeds": feeds_and_data})
654+
else:
655+
self._post(path, {"feeds": feeds_and_data})
656+
638657
def receive_all_data(self, feed_key: str):
639658
"""
640659
Get all data values from a specified Adafruit IO feed. Data is

examples/adafruit_io_http/adafruit_io_groups.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Adafruit IO HTTP API - Group Interactions
55
# Documentation: https://io.adafruit.com/api/docs/#groups
66
# adafruit_circuitpython_adafruitio with an esp32spi_socket
7+
import datetime
78
import board
89
import busio
910
from digitalio import DigitalInOut
@@ -50,6 +51,13 @@
5051
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
5152
requests = adafruit_requests.Session(pool, ssl_context)
5253

54+
# If you are testing on python with blinka, use real requests below and comment out above:
55+
# import os, datetime, requests as real_requests
56+
# from adafruit_io.adafruit_io import IO_HTTP
57+
# secrets = {"aio_username": os.getenv("AIO_USERNAME"), "aio_key": os.getenv("AIO_KEY")}
58+
# requests = real_requests.Session()
59+
60+
5361
# Set your Adafruit IO Username and Key in secrets.py
5462
# (visit io.adafruit.com if you need to create an account,
5563
# or if you need your Adafruit IO key.)
@@ -72,8 +80,29 @@
7280
humidity_feed = io.create_new_feed("humidity", "a feed for humidity data")
7381
io.add_feed_to_group(sensor_group["key"], humidity_feed["key"])
7482

83+
# show humidity feed is in two groups
84+
print("Getting fresh humidity feed info... (notice groups)")
85+
print(io.get_feed(humidity_feed["key"]))
86+
87+
# Publish data for multiple feeds to a group, use different timestamps for no reason
88+
print("Publishing batch data to group feeds with created_at set 99minutes ago...")
89+
thetime = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(minutes=99)
90+
io.send_group_data(
91+
group_key=sensor_group["key"],
92+
feeds_and_data=[
93+
{"key": "temperature", "value": 20.0},
94+
{"key": "humidity", "value": 40.0},
95+
],
96+
metadata={
97+
"lat": 50.1858942,
98+
"lon": -4.9677478,
99+
"ele": 4,
100+
"created_at": thetime.isoformat(),
101+
},
102+
)
103+
75104
# Get info from the group
76-
print("Getting fresh group info...")
105+
print("Getting fresh group info... (notice created_at vs updated_at)")
77106
sensor_group = io.get_group("envsensors") # refresh data via HTTP API
78107
print(sensor_group)
79108

0 commit comments

Comments
 (0)