-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
120 lines (92 loc) · 3.21 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
'use strict';
if (process.env.DEBUG === '1') {
//require("inspector").open(9229, "0.0.0.0", false);
require('inspector').open(9229, "0.0.0.0");
}
const Homey = require('homey');
class PhilipsTV extends Homey.App {
onInit() {
this.log('Philips TV app is running...');
this.onInitFlow();
}
onInitFlow() {
new Homey.FlowCardAction('open_application')
.register()
.registerRunListener(this.onFlowActionOpenApplication)
.getArgument('app')
.registerAutocompleteListener(this.onFlowApplicationAutocomplete);
new Homey.FlowCardAction('select_source')
.register()
.registerRunListener(this.onFlowActionSelectSource);
new Homey.FlowCardAction('set_ambihue')
.register()
.registerRunListener(this.onFlowActionSetAmbiHue);
new Homey.FlowCardAction('set_ambilight')
.register()
.registerRunListener(this.onFlowActionSetAmbilight);
new Homey.FlowCardAction('send_key')
.register()
.registerRunListener(this.onFlowActionSendKey)
.getArgument('option')
.registerAutocompleteListener(this.onFlowKeyAutocomplete.bind(this));
this.log('Initialized flow');
}
async onFlowActionOpenApplication(args) {
let device = args.device,
app = args.app;
return device.openApplication(app);
}
async onFlowActionSelectSource(args) {
let device = args.device,
source = args.source;
return device.sendGoogleAssistantSearch(source);
}
async onFlowActionSetAmbiHue(args) {
let device = args.device,
state = (args.state === 'on');
return device.setAmbiHue(state);
}
async onFlowActionSetAmbilight(args) {
let device = args.device,
state = (args.state === 'on');
return device.setAmbilight(state);
}
async onFlowApplicationAutocomplete(query, args) {
let device = args.device;
return device.getApplications().then(applications => {
return applications.filter(result => {
return result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
});
});
}
async onFlowActionSendKey(args) {
let device = args.device,
option = args.option,
client = device.getJointspaceClient();
return client.sendKey(option.key);
}
async onFlowKeyAutocomplete(query, args) {
let device = args.device,
client = device.getJointspaceClient();
let results = client.getPossibleKeys().map(key => {
return {
"id": key.inputName,
"key": key.inputName,
"name": this.getI18nString(key.friendlyName)
}
}).filter(result => {
return result.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
});
return Promise.resolve(results);
}
getI18nString(i18n) {
const lang = Homey.ManagerI18n.getLanguage();
if (i18n[lang])
return i18n[lang];
else if (i18n['en'])
return i18n['en'];
else
return `Untranslated string: ${i18n}`;
}
}
module.exports = PhilipsTV;