-
Notifications
You must be signed in to change notification settings - Fork 5
/
netatmo-api.js
133 lines (116 loc) · 4.1 KB
/
netatmo-api.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
'use strict';
var request = require('request');
var simpleoauth2 = require('simple-oauth2');
class NetatmoAPI {
constructor(clientid, clientsecret) {
const credentials = {
client: {
id: clientid,
secret: clientsecret,
},
auth: {
tokenHost: 'https://api.netatmo.com/',
tokenPath: '/oauth2/token'
},
http: { headers: { 'User-Agent': 'homebridge-thermostat-netatmo' } }
};
this._oauth2 = simpleoauth2.create(credentials);
}
authenticate(username, password) {
var tokenConfig = {
username: username,
password: password,
scope: 'read_thermostat write_thermostat'
};
var promise = this._oauth2.ownerPassword
.getToken(tokenConfig)
.then((result) => {
const token = this._oauth2.accessToken.create(result);
return token;
});
return promise;
}
getThermostatState(token) {
var deffered = new Promise((resolve, reject) => {
request({
url: 'https://api.netatmo.com/api/homestatus',
qs: {'access_token': token.token.access_token}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var processedData = NetatmoAPI.procesData(body);
if (!processedData) {
reject('No data found.');
return;
}
resolve(processedData);
return;
} else {
if (!error && body) {
error = body;
}
var errorstr = 'Error getting thermostat state (status code '+response.statusCode+'): ' + error;
reject(errorstr);
return;
}
});
});
return deffered;
}
setThermostat(data) {
var deffered = new Promise((resolve, reject) => {
request({
url: 'https://api.netatmo.com/api/setroomthermpoint',
qs: {
'access_token': data['token'].token.access_token,
'device_id': data['device_id'],
'module_id': data['module_id'],
'setpoint_temp': data['temperature'],
'setpoint_mode': data['mode'],
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var processedData = NetatmoAPI.processAPIResponse(body);
if (!processedData) {
reject('Setting thermostate state failed (invalid response)');
return;
}
resolve(processedData);
return;
} else {
if (!error && body) {
error = body;
}
var errorstr = 'Setting thermostate state failed (status code '+response.statusCode+'): ' + error;
reject(errorstr);
return;
}
});
});
return deffered;
}
static processAPIResponse(text) {
var data = JSON.parse(text);
if (data.status !== 'ok') {
return null;
}
return data;
}
static procesData(text) {
var data = this.processAPIResponse(text);
if (!data) {
return null;
}
var device = data.body.devices[0];
var thermostat = device.modules[0];
var result = {
device_id: device._id,
module_id: thermostat._id,
temperature: thermostat.measured.temperature,
heatingOn: (thermostat.therm_relay_cmd !== 0),
setPoint: thermostat.measured.setpoint_temp,
mode: thermostat.setpoint.setpoint_mode,
};
return result;
}
}
module.exports = NetatmoAPI;