Skip to content

Commit 68ff68d

Browse files
authored
Merge pull request #158 from adafruit/update-tests-adjusted-time-failure
Add Timezone support to `receive_time` and update tests (adjusted-time-failure)
2 parents 4e16f2b + 84577df commit 68ff68d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

Diff for: Adafruit_IO/client.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,23 @@ def __init__(self, username, key, proxies=None, base_url='https://io.adafruit.co
7272
@staticmethod
7373
def to_red(data):
7474
"""Hex color feed to red channel.
75+
7576
:param int data: Color value, in hexadecimal.
7677
"""
7778
return ((int(data[1], 16))*16) + int(data[2], 16)
7879

7980
@staticmethod
8081
def to_green(data):
8182
"""Hex color feed to green channel.
83+
8284
:param int data: Color value, in hexadecimal.
8385
"""
8486
return (int(data[3], 16) * 16) + int(data[4], 16)
8587

8688
@staticmethod
8789
def to_blue(data):
8890
"""Hex color feed to blue channel.
91+
8992
:param int data: Color value, in hexadecimal.
9093
"""
9194
return (int(data[5], 16) * 16) + int(data[6], 16)
@@ -153,6 +156,7 @@ def send_data(self, feed, value, metadata=None, precision=None):
153156
specified value to the feed identified by either name, key, or ID.
154157
Returns a Data instance with details about the newly appended row of data.
155158
Note that send_data now operates the same as append.
159+
156160
:param string feed: Name/Key/ID of Adafruit IO feed.
157161
:param string value: Value to send.
158162
:param dict metadata: Optional metadata associated with the value.
@@ -173,6 +177,7 @@ def send_batch_data(self, feed, data_list):
173177
ID, feed key, or feed name. Data must be an instance of the Data class
174178
with at least a value property set on it. Returns a Data instance with
175179
details about the newly appended row of data.
180+
176181
:param string feed: Name/Key/ID of Adafruit IO feed.
177182
:param Data data_list: Multiple data values.
178183
"""
@@ -185,21 +190,28 @@ def append(self, feed, value):
185190
specified value to the feed identified by either name, key, or ID.
186191
Returns a Data instance with details about the newly appended row of data.
187192
Note that unlike send the feed should exist before calling append.
193+
188194
:param string feed: Name/Key/ID of Adafruit IO feed.
189195
:param string value: Value to append to feed.
190196
"""
191197
return self.create_data(feed, Data(value=value))
192198

193-
def receive_time(self):
194-
"""Returns a struct_time from the Adafruit IO Server based on the device's IP address.
199+
def receive_time(self, timezone=None):
200+
"""Returns a struct_time from the Adafruit IO Server based on requested
201+
timezone, or automatically based on the device's IP address.
195202
https://docs.python.org/3.7/library/time.html#time.struct_time
203+
204+
:param string timezone: Optional timezone to return the time in.
205+
See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
196206
"""
197207
path = 'integrations/time/struct.json'
208+
if timezone:
209+
path += f'?tz={timezone}'
198210
return self._parse_time_struct(self._get(path))
199211

200212
@staticmethod
201213
def _parse_time_struct(time_dict: dict) -> time.struct_time:
202-
"""Parse the time data returned by the server and return a time_struct
214+
"""Parse the time data returned by the server and return a time_struct
203215
204216
Corrects for the weekday returned by the server in Sunday=0 format
205217
(Python expects Monday=0)
@@ -211,6 +223,7 @@ def _parse_time_struct(time_dict: dict) -> time.struct_time:
211223

212224
def receive_weather(self, weather_id=None):
213225
"""Adafruit IO Weather Service, Powered by Dark Sky
226+
214227
:param int id: optional ID for retrieving a specified weather record.
215228
"""
216229
if weather_id:
@@ -222,6 +235,7 @@ def receive_weather(self, weather_id=None):
222235
def receive_random(self, randomizer_id=None):
223236
"""Access to Adafruit IO's Random Data
224237
service.
238+
225239
:param int randomizer_id: optional ID for retrieving a specified randomizer.
226240
"""
227241
if randomizer_id:
@@ -233,6 +247,7 @@ def receive_random(self, randomizer_id=None):
233247
def receive(self, feed):
234248
"""Retrieve the most recent value for the specified feed. Returns a Data
235249
instance whose value property holds the retrieved value.
250+
236251
:param string feed: Name/Key/ID of Adafruit IO feed.
237252
"""
238253
path = "feeds/{0}/data/last".format(feed)
@@ -241,6 +256,7 @@ def receive(self, feed):
241256
def receive_next(self, feed):
242257
"""Retrieve the next unread value from the specified feed. Returns a Data
243258
instance whose value property holds the retrieved value.
259+
244260
:param string feed: Name/Key/ID of Adafruit IO feed.
245261
"""
246262
path = "feeds/{0}/data/next".format(feed)
@@ -249,6 +265,7 @@ def receive_next(self, feed):
249265
def receive_previous(self, feed):
250266
"""Retrieve the previous unread value from the specified feed. Returns a
251267
Data instance whose value property holds the retrieved value.
268+
252269
:param string feed: Name/Key/ID of Adafruit IO feed.
253270
"""
254271
path = "feeds/{0}/data/previous".format(feed)
@@ -257,6 +274,7 @@ def receive_previous(self, feed):
257274
def data(self, feed, data_id=None, max_results=DEFAULT_PAGE_LIMIT):
258275
"""Retrieve data from a feed. If data_id is not specified then all the data
259276
for the feed will be returned in an array.
277+
260278
:param string feed: Name/Key/ID of Adafruit IO feed.
261279
:param string data_id: ID of the piece of data to delete.
262280
:param int max_results: The maximum number of results to return. To
@@ -306,6 +324,7 @@ def create_data(self, feed, data):
306324
"""Create a new row of data in the specified feed.
307325
Returns a Data instance with details about the newly
308326
appended row of data.
327+
309328
:param string feed: Name/Key/ID of Adafruit IO feed.
310329
:param Data data: Instance of the Data class. Must have a value property set.
311330
"""
@@ -314,6 +333,7 @@ def create_data(self, feed, data):
314333

315334
def delete(self, feed, data_id):
316335
"""Delete data from a feed.
336+
317337
:param string feed: Name/Key/ID of Adafruit IO feed.
318338
:param string data_id: ID of the piece of data to delete.
319339
"""
@@ -324,6 +344,7 @@ def delete(self, feed, data_id):
324344
def feeds(self, feed=None):
325345
"""Retrieve a list of all feeds, or the specified feed. If feed is not
326346
specified a list of all feeds will be returned.
347+
327348
:param string feed: Name/Key/ID of Adafruit IO feed, defaults to None.
328349
"""
329350
if feed is None:
@@ -334,6 +355,7 @@ def feeds(self, feed=None):
334355

335356
def create_feed(self, feed, group_key=None):
336357
"""Create the specified feed.
358+
337359
:param string feed: Key of Adafruit IO feed.
338360
:param group_key group: Group to place new feed in.
339361
"""
@@ -347,6 +369,7 @@ def create_feed(self, feed, group_key=None):
347369

348370
def delete_feed(self, feed):
349371
"""Delete the specified feed.
372+
350373
:param string feed: Name/Key/ID of Adafruit IO feed.
351374
"""
352375
path = "feeds/{0}".format(feed)
@@ -355,6 +378,7 @@ def delete_feed(self, feed):
355378
# Group functionality.
356379
def groups(self, group=None):
357380
"""Retrieve a list of all groups, or the specified group.
381+
358382
:param string group: Name/Key/ID of Adafruit IO Group. Defaults to None.
359383
"""
360384
if group is None:
@@ -365,13 +389,15 @@ def groups(self, group=None):
365389

366390
def create_group(self, group):
367391
"""Create the specified group.
392+
368393
:param string group: Name/Key/ID of Adafruit IO Group.
369394
"""
370395
path = "groups/"
371396
return Group.from_dict(self._post(path, group._asdict()))
372397

373398
def delete_group(self, group):
374399
"""Delete the specified group.
400+
375401
:param string group: Name/Key/ID of Adafruit IO Group.
376402
"""
377403
path = "groups/{0}".format(group)
@@ -380,6 +406,7 @@ def delete_group(self, group):
380406
# Dashboard functionality.
381407
def dashboards(self, dashboard=None):
382408
"""Retrieve a list of all dashboards, or the specified dashboard.
409+
383410
:param string dashboard: Key of Adafruit IO Dashboard. Defaults to None.
384411
"""
385412
if dashboard is None:
@@ -390,13 +417,15 @@ def dashboards(self, dashboard=None):
390417

391418
def create_dashboard(self, dashboard):
392419
"""Create the specified dashboard.
420+
393421
:param Dashboard dashboard: Dashboard object to create
394422
"""
395423
path = "dashboards/"
396424
return Dashboard.from_dict(self._post(path, dashboard._asdict()))
397425

398426
def delete_dashboard(self, dashboard):
399427
"""Delete the specified dashboard.
428+
400429
:param string dashboard: Key of Adafruit IO Dashboard.
401430
"""
402431
path = "dashboards/{0}".format(dashboard)
@@ -405,6 +434,7 @@ def delete_dashboard(self, dashboard):
405434
# Block functionality.
406435
def blocks(self, dashboard, block=None):
407436
"""Retrieve a list of all blocks from a dashboard, or the specified block.
437+
408438
:param string dashboard: Key of Adafruit IO Dashboard.
409439
:param string block: id of Adafruit IO Block. Defaults to None.
410440
"""
@@ -416,6 +446,7 @@ def blocks(self, dashboard, block=None):
416446

417447
def create_block(self, dashboard, block):
418448
"""Create the specified block under the specified dashboard.
449+
419450
:param string dashboard: Key of Adafruit IO Dashboard.
420451
:param Block block: Block object to create under dashboard
421452
"""
@@ -424,6 +455,7 @@ def create_block(self, dashboard, block):
424455

425456
def delete_block(self, dashboard, block):
426457
"""Delete the specified block.
458+
427459
:param string dashboard: Key of Adafruit IO Dashboard.
428460
:param string block: id of Adafruit IO Block.
429461
"""
@@ -433,6 +465,7 @@ def delete_block(self, dashboard, block):
433465
# Layout functionality.
434466
def layouts(self, dashboard):
435467
"""Retrieve the layouts array from a dashboard
468+
436469
:param string dashboard: key of Adafruit IO Dashboard.
437470
"""
438471
path = "dashboards/{0}".format(dashboard)
@@ -441,6 +474,7 @@ def layouts(self, dashboard):
441474

442475
def update_layout(self, dashboard, layout):
443476
"""Update the layout of the specified dashboard.
477+
444478
:param string dashboard: Key of Adafruit IO Dashboard.
445479
:param Layout layout: Layout object to update under dashboard
446480
"""

Diff for: tests/test_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_time_data(self):
176176
"""receive_time
177177
"""
178178
aio = self.get_client()
179-
server_time = aio.receive_time()
179+
server_time = aio.receive_time(timezone='UTC')
180180
# Check that each value is rx'd properly
181181
# (should never be None type)
182182
for time_data in server_time:

0 commit comments

Comments
 (0)