Skip to content

Commit 770bb96

Browse files
author
brentru
committed
add metadata, precision kwargs to send_data, add create_payload staticmethod
1 parent ab83808 commit 770bb96

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Adafruit_IO/client.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,34 @@ def _delete(self, path):
109109
proxies=self.proxies)
110110
self._handle_error(response)
111111

112+
@staticmethod
113+
def _create_payload(value, metadata):
114+
"""
115+
"""
116+
if metadata is not None:
117+
payload = Data(value=value,lat=metadata['lat'], lon=metadata['lon'],
118+
ele=metadata['ele'], created_at=metadata['created_at'])
119+
return payload
120+
return Data(value=value)
121+
112122
# Data functionality.
113-
def send_data(self, feed, value):
123+
def send_data(self, feed, value, metadata=None, precision=None):
114124
"""Helper function to simplify adding a value to a feed. Will append the
115125
specified value to the feed identified by either name, key, or ID.
116126
Returns a Data instance with details about the newly appended row of data.
117127
Note that send_data now operates the same as append.
118128
:param string feed: Name/Key/ID of Adafruit IO feed.
119129
:param string value: Value to send.
130+
:param dict metadata: Optional metadata associated with the value.
131+
:param int precision: Optional amount of precision points to send.
120132
"""
121-
return self.create_data(feed, Data(value=value))
133+
if precision:
134+
try:
135+
value = round(value, precision)
136+
except NotImplementedError:
137+
raise NotImplementedError("Using the precision kwarg requires a floating point value")
138+
payload = self._create_payload(value, metadata)
139+
return self.create_data(feed, payload)
122140

123141
send = send_data
124142

0 commit comments

Comments
 (0)