Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timezone support to receive_time and update tests (adjusted-time-failure) #158

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions Adafruit_IO/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,23 @@ def __init__(self, username, key, proxies=None, base_url='https://io.adafruit.co
@staticmethod
def to_red(data):
"""Hex color feed to red channel.

:param int data: Color value, in hexadecimal.
"""
return ((int(data[1], 16))*16) + int(data[2], 16)

@staticmethod
def to_green(data):
"""Hex color feed to green channel.

:param int data: Color value, in hexadecimal.
"""
return (int(data[3], 16) * 16) + int(data[4], 16)

@staticmethod
def to_blue(data):
"""Hex color feed to blue channel.

:param int data: Color value, in hexadecimal.
"""
return (int(data[5], 16) * 16) + int(data[6], 16)
Expand Down Expand Up @@ -153,6 +156,7 @@ def send_data(self, feed, value, metadata=None, precision=None):
specified value to the feed identified by either name, key, or ID.
Returns a Data instance with details about the newly appended row of data.
Note that send_data now operates the same as append.

:param string feed: Name/Key/ID of Adafruit IO feed.
:param string value: Value to send.
:param dict metadata: Optional metadata associated with the value.
Expand All @@ -173,6 +177,7 @@ def send_batch_data(self, feed, data_list):
ID, feed key, or feed name. Data must be an instance of the Data class
with at least a value property set on it. Returns a Data instance with
details about the newly appended row of data.

:param string feed: Name/Key/ID of Adafruit IO feed.
:param Data data_list: Multiple data values.
"""
Expand All @@ -185,21 +190,28 @@ def append(self, feed, value):
specified value to the feed identified by either name, key, or ID.
Returns a Data instance with details about the newly appended row of data.
Note that unlike send the feed should exist before calling append.

:param string feed: Name/Key/ID of Adafruit IO feed.
:param string value: Value to append to feed.
"""
return self.create_data(feed, Data(value=value))

def receive_time(self):
"""Returns a struct_time from the Adafruit IO Server based on the device's IP address.
def receive_time(self, timezone=None):
"""Returns a struct_time from the Adafruit IO Server based on requested
timezone, or automatically based on the device's IP address.
https://docs.python.org/3.7/library/time.html#time.struct_time

:param string timezone: Optional timezone to return the time in.
See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
"""
path = 'integrations/time/struct.json'
if timezone:
path += f'?tz={timezone}'
return self._parse_time_struct(self._get(path))

@staticmethod
def _parse_time_struct(time_dict: dict) -> time.struct_time:
"""Parse the time data returned by the server and return a time_struct
"""Parse the time data returned by the server and return a time_struct

Corrects for the weekday returned by the server in Sunday=0 format
(Python expects Monday=0)
Expand All @@ -211,6 +223,7 @@ def _parse_time_struct(time_dict: dict) -> time.struct_time:

def receive_weather(self, weather_id=None):
"""Adafruit IO Weather Service, Powered by Dark Sky

:param int id: optional ID for retrieving a specified weather record.
"""
if weather_id:
Expand All @@ -222,6 +235,7 @@ def receive_weather(self, weather_id=None):
def receive_random(self, randomizer_id=None):
"""Access to Adafruit IO's Random Data
service.

:param int randomizer_id: optional ID for retrieving a specified randomizer.
"""
if randomizer_id:
Expand All @@ -233,6 +247,7 @@ def receive_random(self, randomizer_id=None):
def receive(self, feed):
"""Retrieve the most recent value for the specified feed. Returns a Data
instance whose value property holds the retrieved value.

:param string feed: Name/Key/ID of Adafruit IO feed.
"""
path = "feeds/{0}/data/last".format(feed)
Expand All @@ -241,6 +256,7 @@ def receive(self, feed):
def receive_next(self, feed):
"""Retrieve the next unread value from the specified feed. Returns a Data
instance whose value property holds the retrieved value.

:param string feed: Name/Key/ID of Adafruit IO feed.
"""
path = "feeds/{0}/data/next".format(feed)
Expand All @@ -249,6 +265,7 @@ def receive_next(self, feed):
def receive_previous(self, feed):
"""Retrieve the previous unread value from the specified feed. Returns a
Data instance whose value property holds the retrieved value.

:param string feed: Name/Key/ID of Adafruit IO feed.
"""
path = "feeds/{0}/data/previous".format(feed)
Expand All @@ -257,6 +274,7 @@ def receive_previous(self, feed):
def data(self, feed, data_id=None, max_results=DEFAULT_PAGE_LIMIT):
"""Retrieve data from a feed. If data_id is not specified then all the data
for the feed will be returned in an array.

:param string feed: Name/Key/ID of Adafruit IO feed.
:param string data_id: ID of the piece of data to delete.
:param int max_results: The maximum number of results to return. To
Expand Down Expand Up @@ -306,6 +324,7 @@ def create_data(self, feed, data):
"""Create a new row of data in the specified feed.
Returns a Data instance with details about the newly
appended row of data.

:param string feed: Name/Key/ID of Adafruit IO feed.
:param Data data: Instance of the Data class. Must have a value property set.
"""
Expand All @@ -314,6 +333,7 @@ def create_data(self, feed, data):

def delete(self, feed, data_id):
"""Delete data from a feed.

:param string feed: Name/Key/ID of Adafruit IO feed.
:param string data_id: ID of the piece of data to delete.
"""
Expand All @@ -324,6 +344,7 @@ def delete(self, feed, data_id):
def feeds(self, feed=None):
"""Retrieve a list of all feeds, or the specified feed. If feed is not
specified a list of all feeds will be returned.

:param string feed: Name/Key/ID of Adafruit IO feed, defaults to None.
"""
if feed is None:
Expand All @@ -334,6 +355,7 @@ def feeds(self, feed=None):

def create_feed(self, feed, group_key=None):
"""Create the specified feed.

:param string feed: Key of Adafruit IO feed.
:param group_key group: Group to place new feed in.
"""
Expand All @@ -347,6 +369,7 @@ def create_feed(self, feed, group_key=None):

def delete_feed(self, feed):
"""Delete the specified feed.

:param string feed: Name/Key/ID of Adafruit IO feed.
"""
path = "feeds/{0}".format(feed)
Expand All @@ -355,6 +378,7 @@ def delete_feed(self, feed):
# Group functionality.
def groups(self, group=None):
"""Retrieve a list of all groups, or the specified group.

:param string group: Name/Key/ID of Adafruit IO Group. Defaults to None.
"""
if group is None:
Expand All @@ -365,13 +389,15 @@ def groups(self, group=None):

def create_group(self, group):
"""Create the specified group.

:param string group: Name/Key/ID of Adafruit IO Group.
"""
path = "groups/"
return Group.from_dict(self._post(path, group._asdict()))

def delete_group(self, group):
"""Delete the specified group.

:param string group: Name/Key/ID of Adafruit IO Group.
"""
path = "groups/{0}".format(group)
Expand All @@ -380,6 +406,7 @@ def delete_group(self, group):
# Dashboard functionality.
def dashboards(self, dashboard=None):
"""Retrieve a list of all dashboards, or the specified dashboard.

:param string dashboard: Key of Adafruit IO Dashboard. Defaults to None.
"""
if dashboard is None:
Expand All @@ -390,13 +417,15 @@ def dashboards(self, dashboard=None):

def create_dashboard(self, dashboard):
"""Create the specified dashboard.

:param Dashboard dashboard: Dashboard object to create
"""
path = "dashboards/"
return Dashboard.from_dict(self._post(path, dashboard._asdict()))

def delete_dashboard(self, dashboard):
"""Delete the specified dashboard.

:param string dashboard: Key of Adafruit IO Dashboard.
"""
path = "dashboards/{0}".format(dashboard)
Expand All @@ -405,6 +434,7 @@ def delete_dashboard(self, dashboard):
# Block functionality.
def blocks(self, dashboard, block=None):
"""Retrieve a list of all blocks from a dashboard, or the specified block.

:param string dashboard: Key of Adafruit IO Dashboard.
:param string block: id of Adafruit IO Block. Defaults to None.
"""
Expand All @@ -416,6 +446,7 @@ def blocks(self, dashboard, block=None):

def create_block(self, dashboard, block):
"""Create the specified block under the specified dashboard.

:param string dashboard: Key of Adafruit IO Dashboard.
:param Block block: Block object to create under dashboard
"""
Expand All @@ -424,6 +455,7 @@ def create_block(self, dashboard, block):

def delete_block(self, dashboard, block):
"""Delete the specified block.

:param string dashboard: Key of Adafruit IO Dashboard.
:param string block: id of Adafruit IO Block.
"""
Expand All @@ -433,6 +465,7 @@ def delete_block(self, dashboard, block):
# Layout functionality.
def layouts(self, dashboard):
"""Retrieve the layouts array from a dashboard

:param string dashboard: key of Adafruit IO Dashboard.
"""
path = "dashboards/{0}".format(dashboard)
Expand All @@ -441,6 +474,7 @@ def layouts(self, dashboard):

def update_layout(self, dashboard, layout):
"""Update the layout of the specified dashboard.

:param string dashboard: Key of Adafruit IO Dashboard.
:param Layout layout: Layout object to update under dashboard
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_time_data(self):
"""receive_time
"""
aio = self.get_client()
server_time = aio.receive_time()
server_time = aio.receive_time(timezone='UTC')
# Check that each value is rx'd properly
# (should never be None type)
for time_data in server_time:
Expand Down