Skip to content

Commit

Permalink
added refresh_token flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfgang Malgadey committed Jan 1, 2017
1 parent 54d6b07 commit 8d994c7
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions PyTado/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from http.cookiejar import CookieJar

import json

import json, datetime

class Tado:
"""Interacts with a Tado thermostat via public API.
Expand All @@ -14,10 +13,13 @@ class Tado:
headers = { 'Referer' : 'https://my.tado.com/' }
api2url = 'https://my.tado.com/api/v2/homes/'
mobi2url = 'https://my.tado.com/mobile/1.9/'

refresh_token = ''
refresh_at = datetime.datetime.now() + datetime.timedelta(minutes=5)

# 'Private' methods for use in class, Tado mobile API V1.9.
def _mobile_apiCall(self, cmd):
self._refreshToken()

url = '%s%s' % (self.mobi2url, cmd)
req = urllib.request.Request(url, headers=self.headers)
response = self.opener.open(req)
Expand All @@ -27,6 +29,8 @@ def _mobile_apiCall(self, cmd):

# 'Private' methods for use in class, Tado API V2.
def _apiCall(self, cmd):
self._refreshToken()

url = '%s%i/%s' % (self.api2url, self.id, cmd)
req = urllib.request.Request(url, headers=self.headers)
response = self.opener.open(req)
Expand All @@ -36,7 +40,36 @@ def _apiCall(self, cmd):

def _setOAuthHeader(self, data):
access_token = data['access_token']
expires_in = float(data['expires_in'])
refresh_token = data['refresh_token']

self.refresh_token = refresh_token
self.refresh_at = datetime.datetime.now()
self.refresh_at = self.refresh_at + datetime.timedelta(minutes = expires_in)

self.headers['Authorization'] = 'Bearer ' + access_token

def _refreshToken(self):
if self.refresh_at >= datetime.datetime.now():
return False

url='https://my.tado.com/oauth/token'
data = { 'client_id' : 'tado-webapp',
'grant_type' : 'refresh_token',
'scope' : 'home.user',
'refresh_token' : self.refresh_token }
data = urllib.parse.urlencode(data)
url = url + '?' + data
req = urllib.request.Request(url, data=json.dumps({}).encode('utf8'), method='POST',
headers={'Content-Type': 'application/json', 'Referer' : 'https://my.tado.com/'})

response = self.opener.open(req)
str_response = response.read().decode('utf-8')

print(str_response)

self._setOAuthHeader(json.loads(str_response))
return response

def _loginV2(self, username, password):
url='https://my.tado.com/oauth/token'
Expand All @@ -52,6 +85,7 @@ def _loginV2(self, username, password):

response = self.opener.open(req)
str_response = response.read().decode('utf-8')

self._setOAuthHeader(json.loads(str_response))
return response

Expand Down

0 comments on commit 8d994c7

Please sign in to comment.