-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
402 lines (339 loc) · 14.7 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
const axios = require('axios');
let hap;
module.exports = (api) => {
hap = api.hap;
api.registerPlatform("VenstarExplorerMini", VenstarExplorerMiniPlatform);
};
class VenstarExplorerMiniPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.thermostats = config.thermostats || [];
this.accessories = [];
if (!this.thermostats.length) {
this.log.warn('No thermostats configured.');
}
this.api.on('didFinishLaunching', () => {
this.log('didFinishLaunching');
for (const thermostatConfig of this.thermostats) {
const uuid = this.api.hap.uuid.generate('homebridge:venstar:' + thermostatConfig.ip);
const existingAccessory = this.accessories.find(acc => acc.UUID === uuid);
if (existingAccessory) {
this.log(`Updating existing accessory: ${thermostatConfig.name}`);
existingAccessory.context.config = thermostatConfig;
if (!existingAccessory.context.initialized) {
new VenstarThermostatAccessory(this.log, thermostatConfig, this.api, existingAccessory);
existingAccessory.context.initialized = true;
}
} else {
this.log(`Adding new accessory: ${thermostatConfig.name}`);
const accessory = new this.api.platformAccessory(thermostatConfig.name, uuid);
accessory.context.config = thermostatConfig;
new VenstarThermostatAccessory(this.log, thermostatConfig, this.api, accessory);
accessory.context.initialized = true;
this.api.registerPlatformAccessories("homebridge-venstar-explorer-mini-dec-2024", "VenstarExplorerMini", [accessory]);
this.accessories.push(accessory);
}
}
});
}
configureAccessory(accessory) {
this.log(`Configuring cached accessory: ${accessory.displayName}`);
this.accessories.push(accessory);
}
}
class VenstarThermostatAccessory {
constructor(log, config, api, accessory) {
this.log = log;
this.api = api;
this.accessory = accessory;
this.name = config.name || "Venstar Thermostat";
this.ip = config.ip;
this.currentTemperature = 20;
this.targetTemperature = 22;
this.currentHeatingCoolingState = hap.Characteristic.CurrentHeatingCoolingState.OFF;
this.targetHeatingCoolingState = hap.Characteristic.TargetHeatingCoolingState.AUTO;
this.temperatureDisplayUnits = hap.Characteristic.TemperatureDisplayUnits.CELSIUS;
this.userChangedUnits = false;
this.fanOn = false;
// Variables for AUTO mode thresholds
this.heatingSetpointC = 20;
this.coolingSetpointC = 24;
this.deviceUnits = 1; // updated after polling
this.accessory.getService(hap.Service.AccessoryInformation)
.setCharacteristic(hap.Characteristic.Manufacturer, "Venstar")
.setCharacteristic(hap.Characteristic.Model, "Explorer Mini");
this.thermostatService = this.accessory.getService(hap.Service.Thermostat)
|| this.accessory.addService(hap.Service.Thermostat, this.name);
this.fanService = this.accessory.getService(hap.Service.Fan)
|| this.accessory.addService(hap.Service.Fan, `${this.name} Fan`);
// Existing handlers unchanged
this.thermostatService.getCharacteristic(hap.Characteristic.CurrentHeatingCoolingState)
.on('get', (callback) => {
callback(null, this.currentHeatingCoolingState);
});
this.thermostatService.getCharacteristic(hap.Characteristic.TargetHeatingCoolingState)
.setProps({
validValues: [
hap.Characteristic.TargetHeatingCoolingState.OFF,
hap.Characteristic.TargetHeatingCoolingState.HEAT,
hap.Characteristic.TargetHeatingCoolingState.COOL,
hap.Characteristic.TargetHeatingCoolingState.AUTO
]
})
.on('get', (callback) => {
callback(null, this.targetHeatingCoolingState);
})
.on('set', this.handleTargetHeatingCoolingStateSet.bind(this));
this.thermostatService.getCharacteristic(hap.Characteristic.CurrentTemperature)
.on('get', (callback) => {
callback(null, this.currentTemperature);
});
this.thermostatService.getCharacteristic(hap.Characteristic.TargetTemperature)
.on('get', (callback) => {
callback(null, this.targetTemperature);
})
.on('set', this.handleTargetTemperatureSet.bind(this))
.setProps({ minValue: 10, maxValue: 32, minStep: 0.5 });
this.thermostatService.getCharacteristic(hap.Characteristic.TemperatureDisplayUnits)
.on('get', (callback) => {
callback(null, this.temperatureDisplayUnits);
})
.on('set', this.handleTemperatureDisplayUnitsSet.bind(this));
this.fanService.getCharacteristic(hap.Characteristic.On)
.on('get', async (callback) => {
try {
const response = await axios.get(`http://${this.ip}/query/info`);
const data = response.data;
this.fanOn = (data.fan === 1);
callback(null, this.fanOn);
} catch (err) {
this.log.error('Error getting fan state:', err.message);
callback(err);
}
})
.on('set', this.handleFanOnSet.bind(this));
// Add Heating and Cooling Threshold characteristics for AUTO mode
this.thermostatService.getCharacteristic(hap.Characteristic.HeatingThresholdTemperature)
.on('get', (callback) => callback(null, this.heatingSetpointC))
.on('set', this.handleHeatingThresholdTemperatureSet.bind(this))
.setProps({ minValue: 10, maxValue: 32, minStep: 0.5 });
this.thermostatService.getCharacteristic(hap.Characteristic.CoolingThresholdTemperature)
.on('get', (callback) => callback(null, this.coolingSetpointC))
.on('set', this.handleCoolingThresholdTemperatureSet.bind(this))
.setProps({ minValue: 10, maxValue: 32, minStep: 0.5 });
this.pollThermostat();
this.pollInterval = setInterval(() => {
this.pollThermostat();
}, 60 * 1000);
}
async pollThermostat() {
try {
const infoUrl = `http://${this.ip}/query/info`;
const response = await axios.get(infoUrl);
const data = response.data;
const modeMap = {
0: hap.Characteristic.TargetHeatingCoolingState.OFF,
1: hap.Characteristic.TargetHeatingCoolingState.HEAT,
2: hap.Characteristic.TargetHeatingCoolingState.COOL,
3: hap.Characteristic.TargetHeatingCoolingState.AUTO,
};
this.deviceUnits = data.tempunits;
this.currentTemperature = this.convertToHomeKitTemp(data.spacetemp, data.tempunits);
this.targetHeatingCoolingState = modeMap[data.mode] || hap.Characteristic.TargetHeatingCoolingState.OFF;
this.currentHeatingCoolingState = this.determineCurrentState(data.state);
if (!this.userChangedUnits) {
this.temperatureDisplayUnits = (data.tempunits === 0)
? hap.Characteristic.TemperatureDisplayUnits.FAHRENHEIT
: hap.Characteristic.TemperatureDisplayUnits.CELSIUS;
}
this.fanOn = (data.fan === 1);
if (data.mode === 3) {
// AUTO mode: update thresholds and do NOT update TargetTemperature
this.heatingSetpointC = this.convertToHomeKitTemp(data.heattemp, data.tempunits);
this.coolingSetpointC = this.convertToHomeKitTemp(data.cooltemp, data.tempunits);
this.thermostatService.updateCharacteristic(hap.Characteristic.HeatingThresholdTemperature, this.heatingSetpointC);
this.thermostatService.updateCharacteristic(hap.Characteristic.CoolingThresholdTemperature, this.coolingSetpointC);
} else {
// Non-AUTO: update TargetTemperature as before
this.targetTemperature = this.determineTargetTemperature(data);
this.thermostatService.updateCharacteristic(hap.Characteristic.TargetTemperature, this.targetTemperature);
}
this.thermostatService.updateCharacteristic(hap.Characteristic.CurrentTemperature, this.currentTemperature);
this.thermostatService.updateCharacteristic(hap.Characteristic.CurrentHeatingCoolingState, this.currentHeatingCoolingState);
this.thermostatService.updateCharacteristic(hap.Characteristic.TargetHeatingCoolingState, this.targetHeatingCoolingState);
this.thermostatService.updateCharacteristic(hap.Characteristic.TemperatureDisplayUnits, this.temperatureDisplayUnits);
this.fanService.updateCharacteristic(hap.Characteristic.On, this.fanOn);
} catch (err) {
this.log.error('Error polling thermostat:', err.message);
}
}
determineTargetTemperature(data) {
if (data.mode === 3) {
const avg = (data.heattemp + data.cooltemp) / 2;
return this.convertToHomeKitTemp(avg, data.tempunits);
}
if (data.mode === 1) return this.convertToHomeKitTemp(data.heattemp, data.tempunits);
if (data.mode === 2) return this.convertToHomeKitTemp(data.cooltemp, data.tempunits);
return this.convertToHomeKitTemp(data.spacetemp, data.tempunits);
}
determineCurrentState(state) {
if (state === 1) return hap.Characteristic.CurrentHeatingCoolingState.HEAT;
if (state === 2) return hap.Characteristic.CurrentHeatingCoolingState.COOL;
return hap.Characteristic.CurrentHeatingCoolingState.OFF;
}
convertToHomeKitTemp(temp, units) {
if (units === 0) {
return (temp - 32) * (5.0 / 9.0);
}
return temp;
}
convertFromHomeKitTemp(tempC, targetUnits) {
if (targetUnits === 0) {
return Math.round((tempC * 9.0 / 5.0) + 32);
}
return Math.round(tempC);
}
async setThermostat(mode, heattemp, cooltemp, fan) {
try {
const controlUrl = `http://${this.ip}/control`;
const fallbackHeat = this.lastHeattemp || 70;
const fallbackCool = this.lastCooltemp || 75;
const delta = this.setpointdelta || 2;
const currentMode = mode ?? 0;
let finalHeattemp = (heattemp != null) ? heattemp : fallbackHeat;
let finalCooltemp = (cooltemp != null) ? cooltemp : fallbackCool;
if (currentMode === 3) {
if (finalCooltemp <= finalHeattemp + delta) {
finalCooltemp = finalHeattemp + delta + 1;
}
}
const payload = {
mode: currentMode,
heattemp: finalHeattemp,
cooltemp: finalCooltemp
};
if (fan != null) {
payload.fan = fan;
}
const qs = new URLSearchParams(payload).toString();
await axios.post(controlUrl, qs, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
this.log(`Thermostat updated: mode=${currentMode}, heattemp=${finalHeattemp}, cooltemp=${finalCooltemp}, fan=${fan}`);
this.pollThermostat();
} catch (err) {
this.log.error('Error setting thermostat:', err.message);
}
}
handleHeatingThresholdTemperatureSet(value, callback) {
this.heatingSetpointC = value;
if (this.targetHeatingCoolingState === hap.Characteristic.TargetHeatingCoolingState.AUTO) {
const heattemp = this.convertFromHomeKitTemp(this.heatingSetpointC, this.deviceUnits);
const cooltemp = this.convertFromHomeKitTemp(this.coolingSetpointC, this.deviceUnits);
this.setThermostat(3, heattemp, cooltemp, null);
}
callback(null);
}
handleCoolingThresholdTemperatureSet(value, callback) {
this.coolingSetpointC = value;
if (this.targetHeatingCoolingState === hap.Characteristic.TargetHeatingCoolingState.AUTO) {
const heattemp = this.convertFromHomeKitTemp(this.heatingSetpointC, this.deviceUnits);
const cooltemp = this.convertFromHomeKitTemp(this.coolingSetpointC, this.deviceUnits);
this.setThermostat(3, heattemp, cooltemp, null);
}
callback(null);
}
async handleTargetHeatingCoolingStateSet(value, callback) {
this.targetHeatingCoolingState = value;
const modeMap = {
[hap.Characteristic.TargetHeatingCoolingState.OFF]: 0,
[hap.Characteristic.TargetHeatingCoolingState.HEAT]: 1,
[hap.Characteristic.TargetHeatingCoolingState.COOL]: 2,
[hap.Characteristic.TargetHeatingCoolingState.AUTO]: 3,
};
const venstarMode = modeMap[value];
try {
const response = await axios.get(`http://${this.ip}/query/info`);
const data = response.data;
const tempUnits = data.tempunits;
const convertedTemp = this.convertFromHomeKitTemp(this.targetTemperature, tempUnits);
let heattemp = null;
let cooltemp = null;
let fan = null; // default fan action
if (venstarMode === 1) {
heattemp = convertedTemp;
} else if (venstarMode === 2) {
cooltemp = convertedTemp;
} else if (venstarMode === 3) {
const heatC = this.heatingSetpointC;
const coolC = this.coolingSetpointC;
heattemp = this.convertFromHomeKitTemp(heatC, tempUnits);
cooltemp = this.convertFromHomeKitTemp(coolC, tempUnits);
} else if (venstarMode === 0) {
// If mode is OFF, also turn fan off
fan = 0;
}
await this.setThermostat(venstarMode, heattemp, cooltemp, fan);
callback(null);
} catch (err) {
this.log.error('Error setting target state:', err.message);
callback(err);
}
}
async handleTargetTemperatureSet(value, callback) {
this.targetTemperature = value;
try {
const response = await axios.get(`http://${this.ip}/query/info`);
const data = response.data;
const tempUnits = data.tempunits;
const convertedTemp = this.convertFromHomeKitTemp(value, tempUnits);
let newHeattemp = null;
let newCooltemp = null;
if (data.mode === 1) newHeattemp = convertedTemp;
else if (data.mode === 2) newCooltemp = convertedTemp;
else if (data.mode === 3) {
// In AUTO, ignore TargetTemperature sets; rely on thresholds instead
}
if (data.mode !== 3) {
await this.setThermostat(data.mode, newHeattemp, newCooltemp, null);
}
callback(null);
} catch (err) {
this.log.error('Error setting target temperature:', err.message);
callback(err);
}
}
handleTemperatureDisplayUnitsSet(value, callback) {
this.temperatureDisplayUnits = value;
this.userChangedUnits = true;
callback(null);
}
async handleFanOnSet(value, callback) {
this.fanOn = value;
const fanValue = this.fanOn ? 1 : 0;
try {
const response = await axios.get(`http://${this.ip}/query/info`);
const data = response.data;
const tempUnits = data.tempunits;
const convertedTemp = this.convertFromHomeKitTemp(this.targetTemperature, tempUnits);
let heattemp = null;
let cooltemp = null;
if (data.mode === 1) {
heattemp = convertedTemp;
} else if (data.mode === 2) {
cooltemp = convertedTemp;
} else if (data.mode === 3) {
heattemp = convertedTemp - 1;
cooltemp = convertedTemp + 1;
}
await this.setThermostat(data.mode, heattemp, cooltemp, fanValue);
callback(null);
} catch (err) {
this.log.error('Error setting fan state:', err.message);
callback(err);
}
}
}