Fix create_group. Update feeds example to create or fetch group.

This commit is contained in:
tyeth 2024-11-18 20:30:13 +00:00
parent f8c6b0c56a
commit 48b406df0e
2 changed files with 31 additions and 13 deletions

View file

@ -390,10 +390,19 @@ class Client(object):
def create_group(self, group): def create_group(self, group):
"""Create the specified group. """Create the specified group.
:param string group: Name/Key/ID of Adafruit IO Group. :param Group/string group: Group object to create, or name/key of of Adafruit IO Group.
""" """
path = "groups/" path = "groups/"
return Group.from_dict(self._post(path, group._asdict())) return Group.from_dict(
self._post(
path=path,
data=(
group._asdict()
if isinstance(group, Group)
else {"name": group} if isinstance(group, str) else group
),
)
)
def delete_group(self, group): def delete_group(self, group):
"""Delete the specified group. """Delete the specified group.

View file

@ -1,6 +1,6 @@
# Simple example of sending and receiving values from Adafruit IO with the REST # Simple example of sending and receiving values from Adafruit IO with the REST
# API client. # API client.
# Author: Tony Dicola, Justin Cooper, Brent Rubell # Author: Tony Dicola, Justin Cooper, Brent Rubell, Tyeth Gundry
# Import Adafruit IO REST client. # Import Adafruit IO REST client.
from Adafruit_IO import Client, Feed from Adafruit_IO import Client, Feed
@ -19,9 +19,10 @@ ADAFRUIT_IO_KEY = 'YOUR_AIO_KEY'
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# List all of your feeds # List all of your feeds
print("Obtaining user's feeds...") print("Obtaining user's feeds (key, name)...")
feeds = aio.feeds() feeds = aio.feeds()
print('Feeds: ', feeds) print([(f.key, f.name) for f in feeds])
print("End of feeds listing.\n")
# Create a new feed # Create a new feed
print("Creating new feed...") print("Creating new feed...")
@ -30,19 +31,27 @@ response = aio.create_feed(feed)
print("New feed: ", response) print("New feed: ", response)
# Delete a feed # Delete a feed
print("Deleting feed...", end="")
aio.delete_feed(response.key) aio.delete_feed(response.key)
print("done.\n")
group2 = aio.groups('example') # Get / Create group - use aio.groups(key) and catch the error or do this:
GROUP_KEY = "example"
groups = aio.groups()
group_keys = [g.key for g in groups]
group = (
groups[group_keys.index(GROUP_KEY)]
if GROUP_KEY in group_keys
else aio.create_group(GROUP_KEY)
)
# Create feed in a group # Create feed in a group
feed = Feed(name="PythonGroupFeed") feed = Feed(name="PythonGroupFeed")
group_key = "example" print("Creating feed in group %s" % GROUP_KEY)
groups = aio.groups() response = aio.create_feed(feed, GROUP_KEY)
group = groups[group_key] if group_key in groups else aio.create_group(group_key)
print("Creating feed in group %s"%group_key)
response = aio.create_feed(feed, group_key)
print("New feed: ", response) print("New feed: ", response)
# Delete a feed within a group # Delete a feed within a group
print("Deleting feed within group %s"%group_key) print("Deleting feed within group %s..." % GROUP_KEY, end="")
aio.delete_feed(response.key) aio.delete_feed(response.key)
print("done.")