Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shanbs
Last active January 16, 2019 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanbs/b2551057792c651bd03f7d2b67f98242 to your computer and use it in GitHub Desktop.
Save shanbs/b2551057792c651bd03f7d2b67f98242 to your computer and use it in GitHub Desktop.
#This code sample uses requests (HTTP library)
import requests
from sys import version_info
import json
import sys
# HTTP libraries depends upon Python 2 or 3
if version_info.major == 3 :
import urllib.parse, urllib.request
else:
from urllib import urlencode
import urllib2
payload = {'grant_type': 'password',
'username': "<your_username>",
'password': "<your_password>",
'client_id':"<your_client_id>",
'client_secret': "<your_client_secret>",
'scope': 'read_station read_camera access_camera read_thermostat write_thermostat read_presence access_presence'}
try:
response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
response.raise_for_status()
access_token=response.json()["access_token"]
refresh_token=response.json()["refresh_token"]
scope=response.json()["scope"]
print("Your access token is:", access_token)
print("Your refresh token is:", refresh_token)
print("Your scopes are:", scope)
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
def postRequest(url, params=None, timeout=10):
if version_info.major == 3:
req = urllib.request.Request(url)
if params:
req.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
params = urllib.parse.urlencode(params).encode('utf-8')
try:
resp = urllib.request.urlopen(req, params, timeout=timeout) if params else urllib.request.urlopen(req, timeout=timeout)
except urllib.error.URLError:
return None
else:
if params:
params = urlencode(params)
headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"}
req = urllib2.Request(url=url, data=params, headers=headers) if params else urllib2.Request(url)
try:
resp = urllib2.urlopen(req, timeout=timeout)
except urllib2.URLError:
return None
data = b""
for buff in iter(lambda: resp.read(65535), b''): data += buff
# Return values in bytes if not json data to handle properly camera images
returnedContentType = resp.getheader("Content-Type") if version_info.major == 3 else resp.info()["Content-Type"]
return json.loads(data.decode("utf-8")) if "application/json" in returnedContentType else data
def getHomeData():
params = {
'access_token': access_token
}
try:
# print(params)
response = requests.post("https://api.netatmo.com/api/homesdata", params=params)
print(response.json()["body"])
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
home_id = response.json()["body"]["homes"][0]['id']
# print(home_id)
for home in response.json()["body"]["homes"]:
home_name = home["name"]
home_id = home["id"]
print "Home name is %s" %(home["name"])
print "Home ID is %s" %(home["id"])
if 'modules' not in home:
print("No 'modules' in home %s, skipping ..." %(home_name))
continue
if 'therm_schedules' not in home:
print("No 'therm_schedules' in home %s, skipping ..." %(home_name))
continue
params = {
'access_token': access_token,
'home_id': home_id
}
try:
print(params)
# response = requests.post("https://api.netatmo.com/api/homestatus", params=params)
response = postRequest("https://api.netatmo.com/api/homestatus", params)
print(response["body"])
except requests.exceptions.HTTPError as error:
print(error.response.status_code, error.response.text)
return home_id
home_id = getHomeData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment