-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
245 lines (172 loc) · 7.85 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
var Service, Characteristic;
var request = require("request");
const COOKER_ENDPOINT = (id, secretKey, requestKey) => (
`https://api.anovaculinary.com/cookers/${encodeURIComponent(id)}?secret=${secretKey}&requestKey=${requestKey}`
);
const JOBS_ENDPOINT = (id, secretKey, requestKey) => (
`https://api.anovaculinary.com/cookers/${encodeURIComponent(id)}/jobs?secret=${secretKey}&requestKey=${requestKey}`
);
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-anova", "AnovaCooker", AnovaCooker);
};
function AnovaCooker(log, config) {
this.log = log;
//Characteristic.TemperatureDisplayUnits.CELSIUS = 0;
//Characteristic.TemperatureDisplayUnits.FAHRENHEIT = 1;
this.temperatureDisplayUnits = Characteristic.TemperatureDisplayUnits.CELSIUS;
this.maxTemp = this.temperatureDisplayUnits == Characteristic.TemperatureDisplayUnits.CELSIUS ? 99 : 210;
this.minTemp = this.temperatureDisplayUnits == Characteristic.TemperatureDisplayUnits.CELSIUS ? 25 : 77;
this.name = config.name;
this.cooker = config.cooker || null;
this.secret = config.secret || null;
this.currentTemperature = this.minTemp;
// The value property of CurrentHeatingCoolingState must be one of the following:
//Characteristic.CurrentHeatingCoolingState.OFF = 0;
//Characteristic.CurrentHeatingCoolingState.HEAT = 1;
//Characteristic.CurrentHeatingCoolingState.COOL = 2;
this.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
this.targetTemperature = this.minTemp;
// The value property of TargetHeatingCoolingState must be one of the following:
//Characteristic.TargetHeatingCoolingState.OFF = 0;
//Characteristic.TargetHeatingCoolingState.HEAT = 1;
//Characteristic.TargetHeatingCoolingState.COOL = 2;
//Characteristic.TargetHeatingCoolingState.AUTO = 3;
this.targetHeatingCoolingState = Characteristic.CurrentHeatingCoolingState.OFF;
this.service = new Service.Thermostat(this.name);
}
AnovaCooker.prototype = {
//Start
identify: function(callback) {
this.log("Identify requested!");
callback(null);
},
getName: function(callback) {
this.log("getName :", this.name);
var error = null;
callback(error, this.name);
},
getServices: function() {
function syncLocalData(json, obj){
var unit = json.status.temp_unit == 'c' ? Characteristic.TemperatureDisplayUnits.CELSIUS : Characteristic.TemperatureDisplayUnits.FAHRENHEIT;
obj.temperatureDisplayUnits = unit;
obj.maxTemp = obj.temperatureDisplayUnits == Characteristic.TemperatureDisplayUnits.CELSIUS ? 99 : 210;
obj.minTemp = obj.temperatureDisplayUnits == Characteristic.TemperatureDisplayUnits.CELSIUS ? 25 : 77;
obj.currentTemperature = json.status.current_temp ? json.status.current_temp : obj.minTemp;
obj.targetTemperature = json.status.target_temp ? json.status.target_temp : obj.minTemp;
if(json.status.is_running){
if(obj.currentTemperature < obj.targetTemperature ){
obj.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.HEAT;
obj.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.HEAT;
}
else{
obj.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.COOL;
obj.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.AUTO;
}
}
else{
obj.heatingCoolingState = Characteristic.CurrentHeatingCoolingState.COOL;
obj.targetHeatingCoolingState = Characteristic.TargetHeatingCoolingState.COOL;
}
}
function setTargetHeatingCoolingState(value) {
if(value == Characteristic.TargetHeatingCoolingState.COOL || value == Characteristic.TargetHeatingCoolingState.OFF ){
return "{ is_running: false }";
}
return "{ is_running: true }";
}
function setTargetTemperature(value) {
return "{ target_temp: ${value} }";
}
function setTemperatureDisplayUnits(value) {
return value == Characteristic.TemperatureDisplayUnits.CELSIUS ? "{ temp_unit: 'c' }" : "{ temp_unit: 'f' }";
}
var getDispatch = function (callback, characteristic){
var value = 0;
var actionName = "get" + characteristic.displayName.replace(/\s/g, '');
var requestKey = new Date().valueOf();
request.get({ url: COOKER_ENDPOINT(this.cooker, this.secret, requestKey)}, function (err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
syncLocalData(json, this);
switch (actionName) {
case "getCurrentHeatingCoolingState": value = this.heatingCoolingState; break;
case "getTargetHeatingCoolingState": value = this.targetHeatingCoolingState; break;
case "getCurrentTemperature": value = this.currentTemperature; break;
case "getTargetTemperature": value = this.targetTemperature; break;
case "getTemperatureDisplayUnits": value = this.temperatureDisplayUnits; break;
default: value = 0;
}
}
else
{
switch (actionName) {
case "getCurrentHeatingCoolingState": value = Characteristic.CurrentHeatingCoolingState.OFF; break;
case "getTargetHeatingCoolingState": value = Characteristic.TargetHeatingCoolingState.OFF; break;
case "getCurrentTemperature": value = this.minTemp; break;
case "getTargetTemperature": value = this.minTemp; break;
case "getTemperatureDisplayUnits": value = this.temperatureDisplayUnits; break;
default: value = 0;
}
}
this.log("getDispatch: ", actionName + ":", value);
callback(null, value);
}.bind(this));
}.bind(this);
var setDispatch = function (value, callback, characteristic) {
var actionName = "set" + characteristic.displayName.replace(/\s/g, '');
this.log("setDispatch: " + actionName + ":", value);
var body = [];
switch (actionName) {
case "setTargetHeatingCoolingState": body = setTargetHeatingCoolingState(value); break;
case "setTargetTemperature": body = setTargetTemperature(value); break;
case "setTemperatureDisplayUnits": body = setTemperatureDisplayUnits(value); break;
default:
}
request.post({
url: COOKER_ENDPOINT(this.cooker, this.secret, new Date().valueOf()),
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
}, function (err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
syncLocalData(json, this);
}
callback(null, value);
}.bind(this));
}.bind(this);
function makeHelper(characteristic) {
return {
getter: function (callback) { getDispatch(callback, characteristic); },
setter: function (value, callback) { setDispatch(value, callback, characteristic) }
};
}
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Anovaculinary.com")
.setCharacteristic(Characteristic.Model, "Anova Precision Cooker")
.setCharacteristic(Characteristic.SerialNumber, this.cooker);
var counters = [];
this.service.addCharacteristic(Characteristic.LockManagementAutoSecurityTimeout);
// Bind Characteristics
for (var characteristicIndex in this.service.characteristics)
{
var characteristic = this.service.characteristics[characteristicIndex];
var compactName = characteristic.displayName.replace(/\s/g, '');
if(compactName == 'Name'){
continue;
}
this.log("makeHelper: ", compactName);
counters[characteristicIndex] = makeHelper(characteristic);
characteristic.on('get', counters[characteristicIndex].getter.bind(this));
characteristic.on('set', counters[characteristicIndex].setter.bind(this));
if(compactName.indexOf("Temperature")){
characteristic.props.maxValue = 99;
}
}
return [informationService, this.service];
}
};