-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
317 lines (284 loc) · 12.5 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
'use strict';
const Homey = require('homey');
const SonyBraviaAndroidTvCommunicator = require('./lib/sony-bravia-android-tv-communicator');
const SonyBraviaFlowActions = require('./lib/flow-actions');
const wol = require('./lib/wol');
class SonyBraviaAndroidTvApp extends Homey.App {
async onInit() {
if (process.env.DEBUG === '1'){
if (this.homey.platform == "local"){
try{
require('inspector').waitForDebugger();
}
catch(error){
require('inspector').open(9229, '0.0.0.0', true);
}
}
}
this.log('SonyBraviaAndroidTvApp is running...');
await this.registerFlowListeners();
}
async registerFlowListeners() {
// FLOW ACTIONS ===============================================================================================
// Flow Listener for IRCC calls.
// Mappings flow action => IR code are defined in flow-actions.js.
// Flow cards with selection list are providing IR code as selection is (args.value).
// These flow card wil be marked as "deprecated" because all is selectable with autocomplete
// list in new IRCC flow card.
SonyBraviaFlowActions.forEach(flow => {
try {
let flowCard = this.homey.flow.getActionCard(flow.action);
flowCard.registerRunListener(async (args, state) => {
try {
flow.parsedCommand = flow.command;
if (flow.command instanceof Function) {
flow.parsedCommand = flow.command(args, state)
}
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version}: starting flow: ${flow.action} and sending command: `, flow.parsedCommand);
return await SonyBraviaAndroidTvCommunicator.sendCommand(args.device, flow.parsedCommand);
} catch (err) {
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version}: flow command could not be executed: `, flow, err);
throw err;
}
});
} catch (err) {
this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version}: flow command could not be registered: `, flow, err);
}
});
// Flow Listener for REST-API and IRCC calls
// These are "new" flow actions and "replacing" actions to replace deprecated IRCC flow action with only one autocomplete card.
this._flowActionStartApp = this.homey.flow.getActionCard('app_start')
this._flowActionStartApp.registerRunListener(async (args, state) => {
try{
await SonyBraviaAndroidTvCommunicator.startApp(args.device, args.app.uri );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'app_start': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionStartApp.registerArgumentAutocompleteListener('app', async (query, args) => {
this._autocompleteAppList = await args.device.getAutocompleteAppList();
return this._autocompleteAppList.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
});
this._flowActionSelectInput = this.homey.flow.getActionCard('input_select')
this._flowActionSelectInput.registerRunListener(async (args, state) => {
try{
await SonyBraviaAndroidTvCommunicator.selectInput(args.device, args.input.uri );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'input_select': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionSelectInput.registerArgumentAutocompleteListener('input', async (query, args) => {
this._autocompleteInputList = await args.device.getAutocompleteInputList();
return this._autocompleteInputList.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
});
this._flowActionSelectTvChannel = this.homey.flow.getActionCard('tv_channel_select')
this._flowActionSelectTvChannel.registerRunListener(async (args, state) => {
try{
await SonyBraviaAndroidTvCommunicator.selectInput(args.device, args.channel.uri );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'input_tv_channel': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionSelectTvChannel.registerArgumentAutocompleteListener('source', async (query, args) => {
this._autocompleteTvSourceList = await args.device.getAutocompleteTvSourceList();
return this._autocompleteTvSourceList.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
});
this._flowActionSelectTvChannel.registerArgumentAutocompleteListener('channel', async (query, args) => {
try{
if (args && args.source && args.source.uri){
this._autocompleteTvChannelList = await args.device.getAutocompleteTvChannelList(args.source.uri);
return this._autocompleteTvChannelList.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
}
else{
return [];
}
}
catch{
return [];
}
});
this._flowActionIrccCommand = this.homey.flow.getActionCard('ircc_command')
this._flowActionIrccCommand.registerRunListener(async (args, state) => {
try{
await SonyBraviaAndroidTvCommunicator.sendCommand(args.device, args.ircc.ircc );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'ircc_command': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionIrccCommand.registerArgumentAutocompleteListener('ircc', async (query, args) => {
this._autocompleteIrccList = await args.device.getAutocompleteIrccList();
return this._autocompleteIrccList.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
});
this._flowActionAudioOutput = this.homey.flow.getActionCard('audio_output')
this._flowActionAudioOutput.registerRunListener(async (args, state) => {
try{
await SonyBraviaAndroidTvCommunicator.setSoundSettings(args.device, args.output );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'audio_output': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionScreenOffTrue = this.homey.flow.getActionCard('screen_off_true')
this._flowActionScreenOffTrue.registerRunListener(async (args, state) => {
try{
await args.device.screenOff( true );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'screen_off_true': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionScreenOffFalse = this.homey.flow.getActionCard('screen_off_false')
this._flowActionScreenOffFalse.registerRunListener(async (args, state) => {
try{
await args.device.screenOff( false );
await args.device.checkDevice();
return true;
}
catch(error){
this.error("Error executing flowAction 'screen_off_false': "+ error.message);
throw new Error(error.message);
}
});
// APP FLOW ACTIONS =========================================================================================
this._flowActionWakeOnLan = this.homey.flow.getActionCard('wake_on_lan');
// wakeOnLanAction.registerRunListener(async (args, state) => {
// return args.device.wakeOnLan();
// });
this._flowActionWakeOnLan.registerRunListener(async (args, state) => {
try{
return await this._wakeOnLanAction(args, state);
}
catch(error){
this.error("Error executing flowAction 'wake_on_lan': "+ error.message);
throw new Error(error.message);
}
});
this._flowActionWakeOnLan.registerArgumentAutocompleteListener('device', async (query, args) => {
let results = [];
let devices = this.homey.drivers.getDriver('sony-bravia-android-tv').getDevices();
for (let i=0; i<devices.length; i++){
results.push(
{
name: devices[i].getName(),
id: devices[i].getData().id
}
);
}
// filter based on the query
return results.filter((result) => {
return result.name.toLowerCase().includes(query.toLowerCase());
});
});
// FLOW CONDITIONS ===============================================================================================
this._flowConditionInput = this.homey.flow.getConditionCard('input').registerRunListener(async (args, state) => {
await args.device.checkDevice();
return (args.device.getCapabilityValue('input') == args.input);
})
this._flowConditionInput = this.homey.flow.getConditionCard('audio_output').registerRunListener(async (args, state) => {
await args.device.checkDevice();
return (args.device.getCapabilityValue('audio_output') == args.output);
})
this._flowConditionAvailable = this.homey.flow.getConditionCard('available').registerRunListener(async (args, state) => {
try{
await SonyBraviaAndroidTvCommunicator.getDevicePowerState(args.device);
return true;
}
catch(err){
return false;
}
})
this._flowConditionScreenOff = this.homey.flow.getConditionCard('screen_off').registerRunListener(async (args, state) => {
await args.device.checkDevice();
return (args.device.getCapabilityValue('screen_off') == args.output);
})
}
async _wakeOnLanAction(args, state){
let devices = this.homey.drivers.getDriver('sony-bravia-android-tv').getDevices();
for (let i=0; i<devices.length; i++){
if (devices[i].getData().id == args.device.id){
await this.wakeOnLan(devices[i].getSettings().macAddress);
await this.checkDeviceAvailability(devices[i]);
}
}
}
async wakeOnLan(mac){
const self = this;
return new Promise( ( resolve, reject ) =>
{
try
{
wol.wake(mac, ( response ) =>{
self.log("WakeOnLan request sent.");
resolve(response);
});
}
catch ( err )
{
self.log("WakeOnLan Error:"+err.message);
// DiagnosticLog
self.writeLog("WakeOnLan Error:"+err.message);
reject( new Error( "WakeOnLan error: " + err.message ) );
}
});
}
async checkDeviceAvailability(device){
for (let i=1; i<=10; i++){
this.log("Check device availability #", (i));
// await device.checkDevice();
// if (device.getAvailable()){
// this.log("Device is available.");
// return true;
// }
// if (i<10){
// await this.sleep(2000);
// }
if( await device._checkDeviceAvailability() ){
this.log("Device is available.");
return true;
}
else{
if (i<10){
await this.sleep(2000);
}
}
}
this.log("WoL sent, but device is still unavailable.");
throw new Error("WoL sent, but device is still unavailable.");
}
sleep(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
}
module.exports = SonyBraviaAndroidTvApp;