Skip to content

Commit 2ae8741

Browse files
authored
Merge pull request #19 from brentru/add-new-time-integration
Add new IP-based time endpoint
2 parents 566a9ac + 88cb011 commit 2ae8741

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

adafruit_io/adafruit_io.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI
4040
https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol
4141
"""
42+
from time import struct_time
4243
from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError
4344

4445
__version__ = "0.0.0-auto.0"
@@ -119,18 +120,15 @@ def _post(self, path, payload):
119120
self._handle_error(response)
120121
return response.json()
121122

122-
def _get(self, path, return_text=False):
123+
def _get(self, path):
123124
"""
124125
GET data from Adafruit IO
125126
:param str path: Formatted Adafruit IO URL from _compose_path
126-
:param bool return_text: Returns text instead of json
127127
"""
128128
response = self.wifi.get(
129129
path,
130130
headers=self._create_headers(self._aio_headers[1]))
131131
self._handle_error(response)
132-
if return_text:
133-
return response.text
134132
return response.json()
135133

136134
def _delete(self, path):
@@ -268,10 +266,12 @@ def receive_random_data(self, generator_id):
268266
path = self._compose_path("integrations/words/{0}".format(generator_id))
269267
return self._get(path)
270268

271-
def receive_time(self, time_type):
269+
def receive_time(self):
272270
"""
273-
Returns the current time from the Adafruit IO Server
274-
:param string time_type: Type of time to be returned: millis, seconds, or ISO-8601
271+
Returns a struct_time from the Adafruit IO Server based on the device's IP address.
272+
https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.struct_time
275273
"""
276-
path = 'https://io.adafruit.com/api/v2/time/{0}'.format(time_type)
277-
return self._get(path, return_text=True)
274+
path = self._compose_path('integrations/time/struct.json')
275+
time = self._get(path)
276+
return struct_time((time['year'], time['mon'], time['mday'], time['hour'],
277+
time['min'], time['sec'], time['wday'], time['yday'], time['isdst']))

0 commit comments

Comments
 (0)