-
Notifications
You must be signed in to change notification settings - Fork 0
/
tado-client.js
227 lines (194 loc) · 6.42 KB
/
tado-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const EXPIRATION_WINDOW_IN_SECONDS = 300;
// const tado_auth_url = 'https://auth.tado.com';
const tado_url = 'https://my.tado.com';
// const oauth_path = '/oauth/token';
// const tado_config = {
// client: {
// id: 'tado-web-app',
// secret: 'wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc',
// },
// auth: {
// tokenHost: tado_auth_url,
// }
// }
// const oauth2 = require('simple-oauth2').create(tado_config);
const ClientOAuth2 = require('client-oauth2');
const axios = require('axios');
const auth = new ClientOAuth2({
clientId: 'tado-web-app',
clientSecret: 'wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc',
accessTokenUri: 'https://auth.tado.com/oauth/token',
authorizationUri: 'https://auth.tado.com/oauth/authorize',
scopes: ['home.user']
})
class Tado {
constructor(log) {
this._accessToken;
this.log = log;
}
_refreshToken() {
const token = this._accessToken;
const expirationTimeInSeconds = token.expires.getTime() / 1000;
const expirationWindowStart = expirationTimeInSeconds - EXPIRATION_WINDOW_IN_SECONDS;
// If the start of the window has passed, refresh the token
const nowInSeconds = (new Date()).getTime() / 1000;
const shouldRefresh = nowInSeconds >= expirationWindowStart;
return new Promise((resolve, reject) => {
if (shouldRefresh) {
this.log.info("Refreshing token");
this._accessToken.refresh()
.then(result => {
this.log.info("Token refreshed");
this._accessToken = result;
resolve(this._accessToken);
})
.catch(error => {
this.log.info("Token refresh failed");
reject(error);
});
} else {
this.log.info("Returning current accesstoken");
resolve(this._accessToken);
}
});
}
login(username, password) {
return new Promise((resolve, reject) => {
// const credentials = {
// scope: 'home.user',
// username: username,
// password: password
// };
this.log.info("Fetching access token");
auth.owner.getToken(username, password)
.then(result => {
this.log.info("succesfully fetched access token");
this._accessToken = result;
resolve(result);
}, error => {
this.log.info("error on getToken");
reject(error);
});
// oauth2.ownerPassword.getToken(credentials)
// .then(result => {
// this._accessToken = oauth2.accessToken.create(result);
// resolve(this._accessToken);
// })
// .catch(error => {
// reject(error);
// });
});
}
apiCall(url, method = 'get', data = {}) {
return new Promise((resolve, reject) => {
this.log.info("api call");
if (this._accessToken) {
this._refreshToken().then(() => {
this.log.info("calling api using axios");
axios({
url: tado_url + url,
method: method,
data: data,
headers: {
Authorization: 'Bearer ' + this._accessToken.accessToken
}
}).then(response => {
this.log.info("succesfully called api");
resolve(response.data);
}).catch(error => {
this.log.info("error calling api");
reject(error);
});
});
} else {
this.log.info("Error: no accesstoken");
reject(new Error('Not yet logged in'));
}
});
}
getMe() {
return this.apiCall('/api/v2/me');
}
getHome(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}`);
}
getWeather(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/weather`);
}
getDevices(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/devices`);
}
getInstallations(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/installations`);
}
getUsers(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/users`);
}
getState(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/state`);
}
getMobileDevices(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/mobileDevices`);
}
getMobileDevice(home_id, device_id) {
return this.apiCall(`/api/v2/homes/${home_id}/mobileDevices/${device_id}`);
}
getMobileDeviceSettings(home_id, device_id) {
return this.apiCall(`/api/v2/homes/${home_id}/mobileDevices/${device_id}/settings`);
}
getZones(home_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones`);
}
getZoneState(home_id, zone_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/state`);
}
getZoneCapabilities(home_id, zone_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/capabilities`);
}
getZoneOverlay(home_id, zone_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/overlay`);
}
getTimeTables(home_id, zone_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/schedule/activeTimetable`);
}
getAwayConfiguration(home_id, zone_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/awayConfiguration`);
}
getTimeTable(home_id, zone_id, timetable_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/schedule/timetables/${timetable_id}/blocks`);
}
clearZoneOverlay(home_id, zone_id) {
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/overlay`, 'delete');
}
setZoneOverlay(home_id, zone_id, power, temperature, termination) {
var config = {
setting: {
type: "HEATING",
},
termination: {
}
}
if (power.toLowerCase() == 'on') {
config.setting.power = 'ON';
if (temperature) {
config.setting.temperature = {};
config.setting.temperature.celsius = temperature;
}
} else {
config.setting.power = 'OFF';
}
if (!isNaN(parseInt(termination))) {
config.termination.type = 'TIMER';
config.termination.durationInSeconds = termination;
} else if (termination && termination.toLowerCase() == 'auto') {
config.termination.type = 'TADO_MODE';
} else {
config.termination.type = 'MANUAL';
}
return this.apiCall(`/api/v2/homes/${home_id}/zones/${zone_id}/overlay`, 'put', config);
}
identifyDevice(device_id) {
return this.apiCall(`/api/v2/devices/${device_id}/identify`, 'post');
}
}
module.exports = Tado;