Skip to content

Commit ce3d868

Browse files
authored
Merge branch 'master' into master
2 parents 09c021b + 0f83289 commit ce3d868

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

.github/workflows/build.yml

+11
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@ name: Build-CI
33
on: [pull_request, push]
44

55
jobs:
6+
7+
approve: # First step
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Approve
12+
run: echo For security reasons, all pull requests to this repository need to be approved first before running any automated CI.
13+
614
build:
715
runs-on: ubuntu-latest
816

17+
needs: [approve] # Require the first step to finish
18+
environment:
19+
name: IO
920
steps:
1021
- uses: actions/checkout@v2
1122

Adafruit_IO/client.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
21+
import time
2122
from time import struct_time
2223
import json
2324
import platform
@@ -182,9 +183,19 @@ def receive_time(self):
182183
https://docs.python.org/3.7/library/time.html#time.struct_time
183184
"""
184185
path = 'integrations/time/struct.json'
185-
time = self._get(path)
186-
return struct_time((time['year'], time['mon'], time['mday'], time['hour'],
187-
time['min'], time['sec'], time['wday'], time['yday'], time['isdst']))
186+
return self._parse_time_struct(self._get(path))
187+
188+
@staticmethod
189+
def _parse_time_struct(time_dict: dict) -> time.struct_time:
190+
"""Parse the time data returned by the server and return a time_struct
191+
192+
Corrects for the weekday returned by the server in Sunday=0 format
193+
(Python expects Monday=0)
194+
"""
195+
wday = (time_dict['wday'] - 1) % 7
196+
return struct_time((time_dict['year'], time_dict['mon'], time_dict['mday'],
197+
time_dict['hour'], time_dict['min'], time_dict['sec'],
198+
wday, time_dict['yday'], time_dict['isdst']))
188199

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

tests/test_client.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TestClient(base.IOTestCase):
2323
# If your IP isn't put on the list of non-throttled IPs, uncomment the
2424
# function below to waste time between tests to prevent throttling.
2525
#def tearDown(self):
26-
time.sleep(30.0)
26+
# time.sleep(30.0)
2727

2828
# Helper Methods
2929
def get_client(self):
@@ -154,7 +154,7 @@ def test_create_data(self):
154154
data = Data(value=42)
155155
result = aio.create_data('testfeed', data)
156156
self.assertEqual(int(result.value), 42)
157-
157+
158158
def test_location_data(self):
159159
"""receive_location
160160
"""
@@ -176,11 +176,41 @@ def test_time_data(self):
176176
"""receive_time
177177
"""
178178
aio = self.get_client()
179-
time = aio.receive_time()
179+
server_time = aio.receive_time()
180180
# Check that each value is rx'd properly
181181
# (should never be None type)
182-
for time_data in time:
182+
for time_data in server_time:
183183
self.assertIsNotNone(time_data)
184+
# Check that the week day was interpreted properly
185+
adjusted_time = time.localtime(time.mktime(server_time))
186+
self.assertEqual(server_time.tm_wday, adjusted_time.tm_wday)
187+
188+
def test_parse_time_struct(self):
189+
"""Ensure the _parse_time_struct method properly handles all 7
190+
week days. Particularly important to make sure Sunday is 6,
191+
not -1"""
192+
# Zero time is a dictionary as would be provided by server
193+
# (wday is one higher than it should be)
194+
zero_time = {'year': 1970,
195+
'mon': 1,
196+
'mday': 1,
197+
'hour': 0,
198+
'min': 0,
199+
'sec': 0,
200+
'wday': 4,
201+
'yday': 1,
202+
'isdst': 0}
203+
204+
# Create a good struct for each day of the week and make sure
205+
# the server-style dictionary is parsed correctly
206+
for k in range(7):
207+
real_struct = time.gmtime(k * 86400)
208+
d = zero_time.copy()
209+
d['mday'] += k
210+
d['wday'] += k
211+
d['yday'] += k
212+
newd = Client._parse_time_struct(d)
213+
self.assertEqual(newd.tm_wday, real_struct.tm_wday)
184214

185215
# Test Feed Functionality
186216
def test_append_by_feed_name(self):
@@ -286,6 +316,7 @@ def test_receive_group_by_key(self):
286316
response = io.groups(group.key)
287317
self.assertEqual(response.key, 'grouprx')
288318

319+
289320
# Test Dashboard Functionality
290321
def test_dashboard_create_dashboard(self):
291322
io = self.get_client()
@@ -372,3 +403,7 @@ def test_layout_update_layout(self):
372403
self.assertEqual(response.lg[0]['w'], 16)
373404
io.delete_block(dash.key, block.id)
374405
io.delete_dashboard(dash.key)
406+
407+
408+
if __name__ == "__main__":
409+
unittest.main()

0 commit comments

Comments
 (0)