Skip to content

Commit

Permalink
Build time converter so a datetime.datetime object can be passed into…
Browse files Browse the repository at this point in the history
… get_data
  • Loading branch information
avryhof committed Oct 2, 2018
1 parent d566d5f commit 8731529
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 11 additions & 2 deletions ambient_api/ambientapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import pprint
import time

import requests
Expand Down Expand Up @@ -28,9 +29,14 @@ def __str__(self):
return '%s@%s' % (self.info.get('name'), self.mac_address)

@staticmethod
def current_time():
def convert_datetime(datetime_object):
epoch = datetime.datetime.fromtimestamp(0)

return int(round(time.time() * 1000))
return int((datetime_object - epoch).total_seconds() * 1000.0)

def current_time(self):

return self.convert_datetime(datetime.datetime.now())

def get_data(self, **kwargs):
"""
Expand All @@ -45,6 +51,9 @@ def get_data(self, **kwargs):
limit = int(kwargs.get('limit', 288))
end_date = kwargs.get('end_date', self.current_time())

if isinstance(end_date, datetime.datetime):
end_date = self.convert_datetime(end_date)

if self.mac_address is not None:
service_address = 'devices/%s' % self.mac_address

Expand Down
6 changes: 5 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pprint
import datetime

from ambient_api.ambientapi import AmbientAPI

Expand All @@ -7,9 +8,12 @@
devices = weather.get_devices()

for device in devices:
print('Device')
print(str(device))

print('Last Data')
pprint.pprint(device.last_data)

pprint.pprint(device.get_data())
print('Get Data')
pprint.pprint(device.get_data(end_date=datetime.datetime(year=2018, month=10, day=1)))

0 comments on commit 8731529

Please sign in to comment.