-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
109 lines (91 loc) · 3.99 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
"use strict";
var self = module.exports = {
connecting: false,
init: function() {
console.log("Initializing Harmony Hub app...");
self.listenForTriggers();
console.log("Initializing Harmony Hub app completed.");
},
/**
* Starts listening for specific triggers and sends them to the requested Harmony Hub.
*
* @returns {}
*/
listenForTriggers: function() {
Array.prototype.sortBy = function(p) {
return this.slice(0)
.sort(function(a, b) {
return (a[p] > b[p]) ? 1 : (a[p] < b[p]) ? -1 : 0;
});
}
Homey.manager("flow")
.on("action.start_activity.activity.autocomplete",
function(callback, args) {
Homey.manager("drivers").getDriver("hub").autocompleteActivity(args, callback);
});
Homey.manager("flow")
.on("action.send_command_to_device.device.autocomplete",
function(callback, args) {
Homey.manager("drivers").getDriver("hub").autocompleteDevice(args, callback);
});
Homey.manager("flow")
.on("action.send_command_to_device.controlGroup.autocomplete",
function(callback, args) {
//Homey.log(JSON.stringify(args));
if (args.args.device.length === 0) {
callback(null, []);
return;
}
var listOfControlGroups = [];
args.args.device.controlGroup.forEach(function(controlGroup) {
if (args.query.length === 0 ||
controlGroup.name.toUpperCase().indexOf(args.query.toUpperCase()) !== -1) {
listOfControlGroups.push(controlGroup);
}
});
callback(null, listOfControlGroups.sortBy("name"));
});
Homey.manager("flow")
.on("action.send_command_to_device.action.autocomplete",
function(callback, args) {
//Homey.log(JSON.stringify(args));
if (args.args.device.length === 0 || args.args.controlGroup.length === 0) {
callback(null, []);
return;
}
var actions = [];
args.args.controlGroup.function.forEach(function(action) {
if (args.query.length === 0 ||
action.label.toUpperCase().indexOf(args.query.toUpperCase()) !== -1) {
action.name = action.label;
actions.push(action);
}
});
callback(null, actions.sortBy("name"));
});
Homey.manager("flow")
.on("action.start_activity",
function(callback, args) {
Homey.manager("drivers").getDriver("hub").startActivity(args, callback);
});
Homey.manager("flow")
.on("action.send_command_to_device",
function(callback, args) {
Homey.manager("drivers").getDriver("hub").sendCommandToDevice(args, callback);
});
Homey.manager("flow")
.on("action.all_off",
function(callback, args) {
Homey.manager("drivers").getDriver("hub").allOff(args, callback);
});
console.log("Listening for triggers...");
},
updateSettings: function(settings, callback) {
// Update settings.
Homey.manager("settings").set("reconnect_interval", parseInt(settings.reconnectIntervalInSeconds));
Homey.manager("settings").set("enable_speech", settings.enableSpeech);
console.log("Settings updated: " + JSON.stringify(settings));
// Return success
if (callback) callback(null, true);
}
};