Created
August 3, 2013 21:29
-
-
Save tclancy/6148047 to your computer and use it in GitHub Desktop.
Working on an API for Ecobee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import requests | |
from django.utils import simplejson | |
class EcobeeAPI(object): | |
def __init__(self, access_token, thermostat_ids): | |
self.api_url = 'https://api.ecobee.com/%s?format=json&body=%s' | |
self.access_token = access_token | |
self.selection_info = { | |
"selectionType": "thermostats", | |
"selectionMatch": thermostat_ids | |
} | |
def get_runtime_report(self, start_date=None): | |
""" | |
https://www.ecobee.com/home/developer/api/documentation/v1/operations/get-runtime-report.shtml | |
start_date should be coming from the installation; if an empty one | |
is passed, use 30 minutes ago for unit test purposes | |
DATES ARE IN UTC | |
TEMPS ARE IN F | |
""" | |
end_date = datetime.date.today() | |
if not start_date: | |
start_date = end_date - datetime.timedelta(days=1) | |
data = { | |
'startDate': start_date.strftime('%Y-%m-%d'), | |
'endDate': end_date.strftime('%Y-%m-%d'), | |
'columns': 'auxHeat1,compCool1,outdoorHumidity,zoneAveTemp,zoneCoolTemp,zoneHeatTemp', | |
'includeSensors': 'true', | |
'selection': self.selection_info | |
} | |
self._request('runtimeReport', data) | |
def _get_headers(self): | |
return { | |
'Content-Type': 'application/json;charset=UTF-8', | |
'Authorization': 'Bearer %s' % self.access_token | |
} | |
def _request(self, endpoint, data): | |
""" | |
http://developer.ecobee.com/api/topics/json_in_get_request_403_response | |
""" | |
body = str(simplejson.dumps(data)).replace(" ", "") | |
response = requests.get(self.api_url % (endpoint, body), headers=self._get_headers()) | |
print response | |
print response.headers | |
print response.url | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment