-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.js
70 lines (60 loc) · 1.83 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
'use strict';
const Homey = require('homey');
const Tahoma = require('./lib/Tahoma');
const syncManager = require('./lib/sync');
const INITIAL_SYNC_INTERVAL = 10; //interval of 10 seconds
/**
* This class is the starting point of the app and initializes the neccessary
* services, listeners, etc.
* @extends {Homey.App}
**/
class App extends Homey.App {
/**
* Initializes the app
*/
onInit() {
this.log(`${Homey.app.manifest.id} running...`);
this.addScenarioActionListeners();
if (!Homey.ManagerSettings.get('syncInterval')) {
Homey.ManagerSettings.set('syncInterval', INITIAL_SYNC_INTERVAL);
}
Homey.ManagerSettings.on('set', (setting) => {
if (setting === 'syncInterval') this.initSync();
});
Homey.on('settings.set', this.initSync);
this.initSync();
}
/**
* Initializes synchronization between Homey and TaHoma
* with the interval as defined in the settings.
*/
initSync() {
let interval = null;
try {
interval = Number(Homey.ManagerSettings.get('syncInterval'));
} catch(e) {
interval = INITIAL_SYNC_INTERVAL;
}
syncManager.init(interval * 1000);
}
/**
* Adds a listener for flowcard scenario actions
*/
addScenarioActionListeners() {
/*** ADD FLOW ACTION LISTENERS ***/
new Homey.FlowCardAction('activate_scenario')
.register()
.registerRunListener(args => Tahoma.executeScenario(args.scenario.oid))
.getArgument('scenario')
.registerAutocompleteListener(query => {
return Tahoma.getActionGroups()
.then(data => data
.map(({ oid, label }) => ({ oid, name: label }))
.filter(({ name }) => name.toLowerCase().indexOf(query.toLowerCase()) > -1))
.catch(error => {
console.log(error.message, error.stack);
});
});
}
}
module.exports = App;