-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
344 lines (295 loc) · 12.4 KB
/
index.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'use strict';
var Service, Characteristic;
var NetatmoAPI = require('./netatmo-api.js');
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-thermostat-netatmo', 'Netatmo-Thermostat', NetatmoThermostat);
};
class NetatmoThermostat {
constructor(log, config) {
this.log = log;
this.name = config.name;
this.device_id = null;
this.module_id = null;
this.temperatureDisplayUnits = Characteristic.TemperatureDisplayUnits.CELSIUS;
this.temperature = 0;
this.targetTemperature = 0;
this.heatingThresholdTemperature = 0;
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.OFF;
this.api = new NetatmoAPI(config.client_id, config.client_secret);
this.auth = this.api.authenticate(config.username, config.password);
this.thermostatService = new Service.Thermostat(this.name);
this.refreshThermostat((error) => {
if (error) {
this.log('Error loading themostat data.');
} else {
this.log('Thermostat data loaded.');
}
});
}
refreshThermostat(callback) {
this.log('Loading themostat data from server');
this.auth.then((token) => {
if (token.expired()) {
return token.refresh();
}
return token;
})
.then(this.api.getThermostatState)
.then( (data) => {
this.device_id = data.device_id;
this.module_id = data.module_id;
this.temperature = parseFloat(data.temperature);
this.targetTemperature = parseFloat(data.setPoint);
this.heatingThresholdTemperature = this.targetTemperature;
if (data.heatingOn) {
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.HEAT;
} else {
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
}
switch(data.mode) {
case 'away':
case 'hg':
case 'off':
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.OFF;
break;
case 'manual':
case 'max':
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.HEAT;
break;
case 'program':
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.AUTO;
break;
default:
this.log('Unknown setpoint state');
}
if (this.targetTemperature < 10){
// Homekit only goes as low as 10.
this.targetTemperature = 10;
}
callback(null);
}).catch((reason) => {
// rejection
callback(reason);
});
}
setHeatingMode(callback) {
this.log('Setting thermostat to heating MAX mode.');
var setpointData = {
device_id: this.device_id,
module_id: this.module_id,
mode: 'max',
};
this.setThermostat(setpointData, () => {
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.HEAT;
this.targetTemperature = 30;
this.heatingThresholdTemperature = 25; // Max value allowed by Homekit.
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.HEAT;
this.TargetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.HEAT;
callback();
// Force Characteristic updates..
this.thermostatService.setCharacteristic(Characteristic.HeatingThresholdTemperature,
this.heatingThresholdTemperature);
this.thermostatService.setCharacteristic(Characteristic.CurrentHeatingCoolingState,
this.heatingCoolingState);
});
}
setFrostguardMode(callback) {
this.log('Setting thermostat to frostguard Mode');
var setpointData = {
device_id: this.device_id,
module_id: this.module_id,
mode: 'hg',
};
this.setThermostat(setpointData, () => {
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.OFF;
this.targetTemperature = 10; // Lowest value allowed by Homekit.
this.heatingThresholdTemperature = 7;
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
callback();
// Force Characteristic updates..
this.thermostatService.setCharacteristic(Characteristic.HeatingThresholdTemperature,
this.heatingThresholdTemperature);
this.thermostatService.setCharacteristic(Characteristic.CurrentHeatingCoolingState,
this.heatingCoolingState);
});
}
setAutoMode(callback) {
this.log('Setting thermostat to auto schedule');
var setpointData = {
device_id: this.device_id,
module_id: this.module_id,
mode: 'program',
};
this.setThermostat(setpointData, () => {
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.AUTO;
callback();
// Force Characteristic updates..
this.thermostatService.setCharacteristic(Characteristic.CurrentHeatingCoolingState,
this.heatingCoolingState);
});
}
setHeatingTo(temperature, callback) {
if (temperature > 30) {
// Max allowed by thermostat.
temperature = 30;
}
if (temperature == this.targetTemperature) {
callback();
return;
}
this.log('Setting thermostat to ' + temperature);
var setpointData = {
device_id: this.device_id,
module_id: this.module_id,
temperature: temperature,
mode: 'manual',
};
this.setThermostat(setpointData, () => {
this.targetTemperature = temperature;
this.heatingThresholdTemperature = temperature;
if (temperature >= this.temperature) {
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.HEAT;
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.HEAT;
} else {
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
this.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.COOL;
}
callback();
// Force Characteristic updates..
this.thermostatService.setCharacteristic(Characteristic.HeatingThresholdTemperature,
this.heatingThresholdTemperature);
this.thermostatService.setCharacteristic(Characteristic.CurrentHeatingCoolingState,
this.heatingCoolingState);
this.thermostatService.setCharacteristic(Characteristic.TargetHeatingCoolingState,
this.targetHeatingCoolingState);
});
}
setThermostat(setpointData, callback) {
// TODO. Implement some sort of queue systme to prevent multiple updates.
this.auth.then((token) => {
if (token.expired()) {
return token.refresh();
}
return token;
}).then( (token) => {
setpointData['token'] = token;
return this.api.setThermostat(setpointData);
})
.then( () => {
callback(null);
}).catch((reason) => {
// rejection
callback(reason);
return;
});
}
getCurrentHeatingCoolingState(callback) {
this.log('getCurrentHeatingCoolingState:');
this.refreshThermostat(function () {
callback(null, this.heatingCoolingState);
}.bind(this));
}
getTargetHeatingCoolingState(callback) {
this.log('getTargetHeatingCoolingState:', this.targetHeatingCoolingState);
callback(null, this.targetHeatingCoolingState);
}
setTargetHeatingCoolingState(value, callback) {
this.log('setTargetHeatingCoolingState from/to:', this.targetHeatingCoolingState, value);
if (this.targetHeatingCoolingState === value) {
this.log('Doing nothing');
callback(null);
return;
}
switch (value) {
case Characteristic.TargetHeatingCoolingState.OFF:
this.log('Setting heating off.');
this.setFrostguardMode(callback);
break;
case Characteristic.TargetHeatingCoolingState.HEAT:
this.log('Setting to heat mode.');
this.setHeatingMode(callback);
break;
case Characteristic.TargetHeatingCoolingState.AUTO:
this.log('Setting to atio mode.');
this.setAutoMode(callback);
break;
default:
this.log('Unsupported mode:', value);
callback('Unsupport mode');
return;
}
}
getCurrentTemperature(callback) {
this.refreshThermostat(function () {
this.log('getCurrentTemperature:', this.temperature);
callback(null, this.temperature);
}.bind(this));
}
getTargetTemperature(callback) {
this.refreshThermostat(function () {
this.log('getTargetTemperature:', this.targetTemperature);
callback(null, this.targetTemperature);
}.bind(this));
}
setTargetTemperature(value, callback) {
this.log('setTargetTemperature:', value);
this.setHeatingTo(value, function () {
callback(null); // success
});
}
getTemperatureDisplayUnits(callback) {
this.log('getTemperatureDisplayUnits:', this.temperatureDisplayUnits);
var error = null;
callback(error, this.temperatureDisplayUnits);
}
setTemperatureDisplayUnits(value, callback) {
this.log('setTemperatureDisplayUnits from %s to %s', this.temperatureDisplayUnits, value);
this.temperatureDisplayUnits = value;
var error = null;
callback(error);
}
getHeatingThresholdTemperature(callback) {
this.log('getHeatingThresholdTemperature :' , this.heatingThresholdTemperature);
var error = null;
callback(error, this.heatingThresholdTemperature);
}
getName(callback) {
this.log('getName :', this.name);
var error = null;
callback(error, this.name);
}
getServices() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Netatmo')
.setCharacteristic(Characteristic.Model, 'Smart Thermostat');
this.thermostatService
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentHeatingCoolingState.bind(this));
this.thermostatService
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetHeatingCoolingState.bind(this))
.on('set', this.setTargetHeatingCoolingState.bind(this));
this.thermostatService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getCurrentTemperature.bind(this));
this.thermostatService
.getCharacteristic(Characteristic.TargetTemperature)
.on('get', this.getTargetTemperature.bind(this))
.on('set', this.setTargetTemperature.bind(this));
this.thermostatService
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', this.getTemperatureDisplayUnits.bind(this))
.on('set', this.setTemperatureDisplayUnits.bind(this));
this.thermostatService
.getCharacteristic(Characteristic.HeatingThresholdTemperature)
.on('get', this.getTargetTemperature.bind(this));
this.thermostatService
.getCharacteristic(Characteristic.Name)
.on('get', this.getName.bind(this));
return [informationService, this.thermostatService];
}
}