-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
435 lines (374 loc) · 14.4 KB
/
app.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
'use strict';
const Homey = require('homey');
const { HomeyAPI } = require('athom-api');
const { ManagerCron } = require('homey');
const ZoneDB = require('./logizonedb/ZoneDB.js');
const MQTTClient = require('./mqtt/MQTTClient.js');
const Message = require('./mqtt/Message.js');
let logiSettings = {};
let defaultSettings = {
'awayMode': false,
'awayTemp': 10,
'mqttLogging': true,
'hwSchedule': false,
'showAllZones': false, // This is a GUI only setting
'manualOverride': true,
'overridePeriod': 90, // Time in minutes to suspend schedule
};
let myThermostats = [];
let myZoneDB = new ZoneDB();
// Some of this code from Heimdall :)
class Logi extends Homey.App {
async onInit() {
this.log('Logi is initialising...');
this.api = await this.getApi();
// "Homey" or this.systemName
this.mqttClient = new MQTTClient('homey/logi/logs');
this.mlog('Logi Heating Scheduler initialising...');
await this.loadZones(); // Get zones from Homey
await this.loadZoneDB(); // Load our zone DB
await this.enumerateDevices(); // See what thermostats we have
myZoneDB.updateZoneTree();
await this.loadSettings();
this.initSettingsListener();
// OK - ready to rock and roll
await this.registerCronJob();
this.mlog('Logi Heating Scheduler init complete.');
//await this.saveZoneDB();
}
// Get API control function
getApi() {
if (!this.api) {
this.api = HomeyAPI.forCurrentHomey();
}
return this.api;
}
async mlog(message) {
if (logiSettings.mqttLogging === true) {
if (this.mqttClient.isRegistered()) {
const msg = new Message('log', message);
this.mqttClient.publish(msg);
}
}
this.log(message);
}
async getZones() {
const api = await this.getApi();
return await this.api.zones.getZones();
}
async loadZones() {
const api = await this.getApi();
const allZones = await this.api.zones.getZones()
myZoneDB.refreshZones(allZones);
}
getUIZones() {
this.mlog('Get UI Zones Called');
return myZoneDB.getOrderedZoneList();
}
//uiEnableZone( args.params.id, args.body );
uiEnableZone( id, argsBody ) {
let result = myZoneDB.enableZone(id);
if (result === true) {
this.saveZoneDB();
}
return result;
}
uiDisableZone( id, argsBody ) {
let result = myZoneDB.disableZone(id,argsBody.andDelete);
if (result === true) {
this.saveZoneDB();
}
return result;
}
uiDeleteSetpoint( params ) {
let result = myZoneDB.getZoneById(params.zone).schedule.getByNum(params.day).uiDeleteSetpoint(params.hours,params.mins);
if (result === true) {
this.saveZoneDB();
}
return result;
}
uiUpdateSetpoint( params, body) {
let result = myZoneDB.getZoneById(params.zone).schedule.getByNum(params.day).uiUpdateSetpoint(body.orig, body.new);
if (result === true) {
this.saveZoneDB();
}
return result;
}
uiCreateSetpoint(params, body) {
let result = myZoneDB.getZoneById(params.zone).schedule.getByNum(params.day).uiCreateSetpoint(body.new);
if (result === true) {
this.saveZoneDB();
}
return result;
}
uiCloneDays(params, body) {
let result = myZoneDB.getZoneById(params.zone).schedule.cloneDays(body.src, body.dsts);
if (result === true) {
this.saveZoneDB();
}
return result;
}
// Get all devices function for API
async getDevices() {
const api = await this.getApi();
return await this.api.devices.getDevices();
}
replacer(key, value) {
// Filtering out properties
if (key === 'zonetree') {
return undefined;
}
return value;
}
async saveZoneDB() {
// Saves zoneDB to Homey settings.
let zonedb = JSON.stringify(myZoneDB, this.replacer);
let result = await Homey.ManagerSettings.set('schedules', zonedb);
this.mlog('Saved schedules to Homey settings.');
}
async loadZoneDB() {
// Loads zoneDB from Homey settings, if it is present.
let schedules = await Homey.ManagerSettings.get('schedules');
let result = JSON.parse(schedules);
if (typeof result.zonelist !== 'undefined') {
myZoneDB.loadSavedSettings(result);
this.mlog('Loaded saved schedules'); // should add more error handling
} else {
this.mlog('Could not load saved schedules from Homey.');
}
}
async registerCronJob() {
let result;
try {
this.mlog('Unregistering All Cron Tasks...');
result = await ManagerCron.unregisterAllTasks();
this.mlog('Done.');
} catch (err) {
this.mlog('Unregister All Cron Tasks error : ' + err);
}
try {
this.mlog('Registering my Cron Job...');
result = await ManagerCron.registerTask('logiminutecron', '* * * * *', 'oneminute');
this.mlog('Register Cron Job result: ' + JSON.stringify(result));
} catch (err) {
this.mlog('Register Cron Job error : ' + err);
}
try {
result = await ManagerCron.getTasks();
this.mlog('getTasks says we have ' + result.length + ' cron task registered.');
} catch (err) {
this.mlog('Register Cron Job error : ' + err);
}
await this.registerCronListener();
}
async registerCronListener() {
let task;
let ref = this;
try {
task = await ManagerCron.getTask('logiminutecron');
let result = await task.on('run', function() {
ref.processSchedules();
});
this.mlog('Registered Logi one minute Cron Listener ' + JSON.stringify(result));
} catch (err) {
this.mlog('Register Cron Listener error : ' + err);
}
}
/*
async getThermostatCurrentSetting(device) {
const api = await this.getApi();
let myDev = await api.devices.getDevice(device);
return myDev.capabilitiesObj['target_temperature']['value'];
}
*/
/* old method
async setThermostat(opts) {
const api = await this.getApi();
//this.log(opts)
try {
await api.devices.setCapabilityValue(opts);
} catch (err) {
this.mlog('Error setting thermostat: ' + err + ' Data: ');
this.mlog(opts);
}
return;
}
*/
async processSchedules() {
// called by Cron
// Runs the one minute Checks
this.mlog('Checking setpoints...');
for (let device in myThermostats) {
// check if device is eligible
//this.mlog('Device: ' + device.name + ' overide ' + device.overide)
let thisDevice = myThermostats[device];
if (thisDevice.exclude === true) { continue; }
if (thisDevice.override > 0) {
this.mlog('Not updating device: ' + thisDevice.name + ' manual overide for ' + thisDevice.override + ' minutes.');
thisDevice.override--;
continue;
}
let targetTemp = 10; // why not?
if (logiSettings.awayMode === true) {
targetTemp = logiSettings.awayTemp;
} else {
targetTemp = this.getSchedule(thisDevice).getCurrentTarget();
}
let actualTemp = thisDevice.cInst.target_temperature.value;
//this.log(thisDevice.name + ' target temp: ' + targetTemp + ' actual setting: ' + actualTemp);
if (targetTemp != actualTemp) {
this.mlog('Updating ' + thisDevice.name + ' Target: ' + targetTemp + 'C, Current: ' + actualTemp + 'C');
/* let opts = {
deviceId: thisDevice.id,
capabilityId: 'target_temperature',
value: targetTemp
};
await this.setThermostat(opts);
use cap instance instead */
try {
thisDevice.shadowset = targetTemp;
await thisDevice.cInst.target_temperature.setValue(targetTemp);
} catch (err) {
this.mlog('Error setting thermostat: ' + err + ' Data: ');
this.mlog(thisDevice.id, targetTemp);
}
}
}
this.mlog('Done checking setpoints.');
}
async enumerateDevices() {
// Get the homey object
const api = await this.getApi();
api.devices.on('device.create', async (id) => {
this.mlog('New device found!');
// V1 - const device = await api.devices.getDevice({id: id})
// V2 - var device = await api.devices.getDevice({id: id.id})
var device = await this.waitForDevice(id);
await this.addDevice(device);
});
// before reading a newly created device you should wait until a device is ready, this is not emited though
// https://github.com/athombv/homey-apps-sdk-issues/issues/23
api.devices.on('device.ready', async (id) => {
this.mlog('Device is ready' + id); // never runs...
});
api.devices.on('device.delete', async (id) => {
this.mlog('Device deleted!: ' + id);
});
let allDevices = await this.getDevices();
for (let device in allDevices) {
this.addDevice(allDevices[device]);
}
this.mlog('Enumerating devices done.');
}
// Yolo function courtesy of Robert Klep ;)
// via Heimdall
async waitForDevice(id) {
const device = await this.api.devices.getDevice({
id: id.id
});
if (device.ready) {
return device;
}
await delay(1000);
return this.waitForDevice(id);
} // End of waitForDevice
// Add the device to application list
async registerThermostat(device) {
// Add some fields:
device['exclude'] = false; // Allow exclusion from Scheduling
device['override'] = 0; // simple countdown to end of override in minutes
device['shadowset'] = 0; // workaround for self trigger of stateChange listener
myThermostats.push(device); // GLOBAL VAR???
let zoneName = myZoneDB.getName(device.zone);
this.mlog('Thermostat ' + device.name + ' is in zone ID: ' + device.zone + ' (' + zoneName + ')');
myZoneDB.logNewDevice(device);
let targetTemp = this.getSchedule(device).getCurrentTarget();
this.mlog(device.name + ' target temp: ' + targetTemp);
}
setOverride(someDevice) {
for (let myDevice in myThermostats) {
if (myThermostats[myDevice].id == someDevice.id) {
myThermostats[myDevice].override = logiSettings['overridePeriod'];
return true;
}
}
return false;
}
// Add device function, all device types with target_temperature capability
addDevice(device) {
// Find Away Mode Button
if (device.data.id === 'aMode') {
var aModeDevice = device;
this.mlog('Found Away Mode Button named: ' + device.name);
}
// console.log(device.capabilities.indexOf("target_temperature"))
if ('target_temperature' in device.capabilitiesObj) {
this.mlog('Found thermostat: ' + device.name);
device['cInst'] = [];
this.attachEventListener(device, 'thermostat');
this.registerThermostat(device);
}
} // End of addDevice
getSchedule(device) {
return myZoneDB.getSchedule(device.zone);
}
attachEventListener(device, sensorType) {
switch (sensorType) {
// Kept switch case for future capablities
case 'thermostat':
/* V1
device.on('alarm_motion', this.debounce(alarm_motion => {
this.stateChange(device,alarm_motion,sensorType)
},250));
*/
device['cInst']['target_temperature'] = device.makeCapabilityInstance('target_temperature', function(device) {
this.stateChange(device);
}.bind(this, device));
break;
}
this.mlog('Attached Eventlistener to: ' + device.name);
} // End of attachEventListener
// this function gets called when a device with an attached eventlistener fires an event.
async stateChange(thisDevice) {
// thisDevice being the Homey device, not my own list
this.mlog('stateChange: ' + thisDevice.name);
//let actualTemp = await this.getThermostatCurrentSetting(thisDevice);
//let actualTemp = thisDevice.cInst.target_temperature.value;
let targetTemp = logiSettings.awayTemp;
if (logiSettings.awayMode === false) {
targetTemp = this.getSchedule(thisDevice).getCurrentTarget();
}
if ((thisDevice.shadowset != targetTemp) && (logiSettings.manualOverride === true)) {
// Untested assertion is that this will only be the case with a manual intervention
this.mlog('Setting manual override for ' + logiSettings['overridePeriod'] + ' minutes.');
this.setOverride(thisDevice);
}
} // End of stateChange
async loadSettings() {
// New method, load all defaults then overwrite with any saved
// Easier for upgrades
// This is ONLY run at init.
logiSettings = defaultSettings;
let loadedSettings = await Homey.ManagerSettings.get('settings');
// Read in properties
for (let property in loadedSettings) {
if (loadedSettings.hasOwnProperty(property)) {
logiSettings[property] = loadedSettings[property];
}
}
// Resave for GUI, in case we have added anything
await Homey.ManagerSettings.set('settings', logiSettings);
this.mlog('Loaded settings....');
this.mlog(logiSettings);
}
initSettingsListener() {
Homey.ManagerSettings.on('set', (variable) => {
if (variable === 'settings') {
logiSettings = Homey.ManagerSettings.get('settings');
this.mlog('New settings:');
this.mlog(logiSettings);
}
});
}
} // End of Logi class
module.exports = Logi;