-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (56 loc) · 1.96 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
'use strict';
const Homey = require('homey');
const flowConditions = require('./lib/flows/conditions');
const flowTriggers = require('./lib/flows/triggers');
const _settingsKey = `${Homey.manifest.id}.settings`;
class App extends Homey.App {
log() {
console.log.bind(this, '[log]').apply(this, arguments);
}
error() {
console.error.bind(this, '[error]').apply(this, arguments);
}
// -------------------- INIT ----------------------
async onInit() {
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version} started...`);
await this.initSettings();
await flowConditions.init(this.homey);
await flowTriggers.init(this.homey);
}
async initSettings() {
try {
let settingsInitialized = false;
this.homey.settings.getKeys().forEach((key) => {
if (key == _settingsKey) {
settingsInitialized = true;
}
});
if (settingsInitialized) {
this.log('[initSettings] - Found settings key', _settingsKey);
this.appSettings = this.homey.settings.get(_settingsKey);
} else {
this.log(`Initializing ${_settingsKey} with defaults`);
await this.updateSettings({
API_KEY: null
});
}
this.log('[onInit] - Loaded settings', this.appSettings);
} catch (err) {
this.error(err);
}
}
async updateSettings(settings) {
try {
this.log('[updateSettings] - New settings:', settings);
this.appSettings = settings;
await this.homey.settings.set(_settingsKey, this.appSettings);
} catch (err) {
this.error(err);
}
}
// ---------------------------- GETTERS/SETTERS ----------------------------------
getSettings() {
return this.appSettings;
}
}
module.exports = App;