-
Notifications
You must be signed in to change notification settings - Fork 0
/
platformAccessory.ts
514 lines (450 loc) · 18.7 KB
/
platformAccessory.ts
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import {
Service,
PlatformAccessory,
CharacteristicValue,
CharacteristicSetCallback,
CharacteristicGetCallback,
} from 'homebridge';
import { LgNetcastPlatform, DeviceConfig, ChannelType } from './platform';
import { PLUGIN_NAME } from './settings';
import { Channel, NetcastClient, LG_COMMAND } from 'lg-netcast';
export class LgNetcastTV {
private service: Service;
private netcastClient: NetcastClient;
private currentChannel: Channel | null;
private channelUpdateInProgress: boolean;
private unknownChannelIdentifier: number;
private unknownChannelName: string;
private offTimeout: NodeJS.Timeout | null;
private offPause: boolean;
private accessory: PlatformAccessory;
constructor(private readonly platform: LgNetcastPlatform, private readonly deviceConfig: DeviceConfig) {
deviceConfig.accessToken = deviceConfig.accessToken || '';
deviceConfig.name = deviceConfig.name || 'LG TV';
deviceConfig.host = deviceConfig.host || '192.168.1.1';
deviceConfig.mac = deviceConfig.mac || '00:00:00:00:00';
deviceConfig.accessToken = deviceConfig.accessToken || '';
deviceConfig.channels = deviceConfig.channels || [];
deviceConfig.keyInputDelay = deviceConfig.keyInputDelay || 600; // delay between issuing commands
deviceConfig.offPauseDuration = deviceConfig.offPauseDuration || 600000; // how long to wait after turning off before polling again
// Append port if needed
if (deviceConfig.host.indexOf(':') === -1) {
deviceConfig.host = deviceConfig.host + ':8080';
}
for (let i = 0; i < deviceConfig.channels.length; i++) {
deviceConfig.channels[i].name = deviceConfig.channels[i].name || 'Unnamed Channel';
if ([ChannelType.EXTERNAL, ChannelType.TV].indexOf(deviceConfig.channels[i].type) === -1) {
platform.log.warn(
'Channel type not set, defaulting to "tv". You should change that if this channel is an HDMI device',
);
deviceConfig.channels[i].type = ChannelType.TV;
}
}
const uuid = platform.api.hap.uuid.generate(deviceConfig.mac + deviceConfig.host);
this.accessory = new platform.api.platformAccessory(
deviceConfig.name,
uuid,
platform.api.hap.Categories.TELEVISION,
);
this.service =
this.accessory.getService(this.platform.Service.Television) ||
this.accessory.addService(this.platform.Service.Television);
this.netcastClient = new NetcastClient(this.deviceConfig.host);
this.unknownChannelIdentifier = this.deviceConfig.channels.length;
this.unknownChannelName = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
this.offTimeout = null;
this.offPause = false;
this.currentChannel = null;
this.channelUpdateInProgress = false;
this.initTvService();
this.initTvAccessory();
this.initRemoteControlService();
this.initSpeakerService();
this.initInputSources();
// interval for updating the current active identifier
this.updateCurrentChannel();
setInterval(() => {
this.updateCurrentChannel();
}, 5000);
platform.api.publishExternalAccessories(PLUGIN_NAME, [this.accessory]);
}
/**
* Initializes the tv accessory itself
*/
initTvAccessory() {
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'LG')
.setCharacteristic(this.platform.Characteristic.Model, this.deviceConfig.model)
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.deviceConfig.mac);
}
initTvService() {
this.service
.setCharacteristic(this.platform.Characteristic.Active, this.platform.Characteristic.Active.ACTIVE)
.setCharacteristic(this.platform.Characteristic.ActiveIdentifier, 1)
.setCharacteristic(this.platform.Characteristic.ConfiguredName, this.deviceConfig.name)
.setCharacteristic(this.platform.Characteristic.Name, this.deviceConfig.name)
.setCharacteristic(
this.platform.Characteristic.SleepDiscoveryMode,
this.platform.Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE,
);
// register handlers for the On/Off Characteristic
this.service
.getCharacteristic(this.platform.Characteristic.Active)
.on('set', async (value: CharacteristicValue, callback: CharacteristicSetCallback) => {
if (value) {
this.platform.log.debug('TV state changed to on, however I cant turn it on. Just updating state isntead');
this.platform.log.debug('Use automations to turn the TV on, such as pinging an AppleTV.');
if (this.offTimeout !== null) {
clearTimeout(this.offTimeout);
this.offPause = false;
}
callback(null, true);
return;
}
// After turning off, issue a pause timeout for waiting with polling again
// Netcast TVs take a while before they deactivate their network card, so without the timeout
// it would just immediately appear as "on" again
await this.sendAuthorizedCommand(LG_COMMAND.POWER);
this.platform.log.debug(
`TV turned off. Going to wait ${this.deviceConfig.offPauseDuration}ms before starting to poll for status again.`,
);
this.offPause = true;
this.offTimeout = setTimeout(() => {
this.offPause = false;
this.platform.log.debug('Off pause timeout cleared. Going to start polling again.');
}, this.deviceConfig.offPauseDuration);
callback(null, false);
})
.on('get', (callback: CharacteristicGetCallback) => {
this.platform.log.debug('Querying TV state...');
if (this.currentChannel === null) {
return callback(null, false);
}
return callback(null, true);
});
}
initRemoteControlService() {
this.service.getCharacteristic(this.platform.Characteristic.RemoteKey).on('set', async (newValue, callback) => {
switch (newValue) {
case this.platform.Characteristic.RemoteKey.REWIND: {
await this.sendAuthorizedCommand(LG_COMMAND.REWIND);
break;
}
case this.platform.Characteristic.RemoteKey.FAST_FORWARD: {
await this.sendAuthorizedCommand(LG_COMMAND.FAST_FORWARD);
break;
}
case this.platform.Characteristic.RemoteKey.NEXT_TRACK: {
await this.sendAuthorizedCommand(LG_COMMAND.SKIP_FORWARD);
break;
}
case this.platform.Characteristic.RemoteKey.PREVIOUS_TRACK: {
await this.sendAuthorizedCommand(LG_COMMAND.SKIP_BACKWARD);
break;
}
case this.platform.Characteristic.RemoteKey.ARROW_UP: {
await this.sendAuthorizedCommand(LG_COMMAND.UP);
break;
}
case this.platform.Characteristic.RemoteKey.ARROW_DOWN: {
await this.sendAuthorizedCommand(LG_COMMAND.DOWN);
break;
}
case this.platform.Characteristic.RemoteKey.ARROW_LEFT: {
await this.sendAuthorizedCommand(LG_COMMAND.LEFT);
break;
}
case this.platform.Characteristic.RemoteKey.ARROW_RIGHT: {
await this.sendAuthorizedCommand(LG_COMMAND.RIGHT);
break;
}
case this.platform.Characteristic.RemoteKey.SELECT: {
await this.sendAuthorizedCommand(LG_COMMAND.OK);
break;
}
case this.platform.Characteristic.RemoteKey.BACK: {
await this.sendAuthorizedCommand(LG_COMMAND.BACK);
break;
}
case this.platform.Characteristic.RemoteKey.EXIT: {
await this.sendAuthorizedCommand(LG_COMMAND.EXIT);
break;
}
case this.platform.Characteristic.RemoteKey.PLAY_PAUSE: {
await this.sendAuthorizedCommand(LG_COMMAND.PLAY);
break;
}
case this.platform.Characteristic.RemoteKey.INFORMATION: {
await this.sendAuthorizedCommand(LG_COMMAND.PROGRAM_INFORMATION);
break;
}
}
// don't forget to callback!
callback(null);
});
}
initSpeakerService() {
const speakerService =
this.accessory.getService(this.platform.Service.TelevisionSpeaker) ||
this.accessory.addService(this.platform.Service.TelevisionSpeaker);
speakerService
.setCharacteristic(this.platform.Characteristic.Active, this.platform.Characteristic.Active.ACTIVE)
.setCharacteristic(
this.platform.Characteristic.VolumeControlType,
this.platform.Characteristic.VolumeControlType.RELATIVE,
);
// handle volume control
speakerService
.getCharacteristic(this.platform.Characteristic.VolumeSelector)
.on('set', async (newValue, callback) => {
if (newValue === 0) {
await this.sendAuthorizedCommand(LG_COMMAND.VOLUME_UP);
} else {
await this.sendAuthorizedCommand(LG_COMMAND.VOLUME_DOWN);
}
callback(null);
});
}
initInputSources() {
// Handler for when the user switches to a different input source
this.service
.getCharacteristic(this.platform.Characteristic.ActiveIdentifier)
.on('set', async (newValue, callback) => {
// the value will be the value you set for the Identifier Characteristic
// on the Input Source service that was selected - see input sources below.
this.platform.log.info('set Active Identifier => setNewValue: ' + newValue);
// if unknown identifier, just update it without doing anything
if (newValue === this.unknownChannelIdentifier) {
callback(null);
return;
}
const newChannel = this.deviceConfig.channels[newValue];
const currentChannel = this.currentChannel;
this.channelUpdateInProgress = true;
if (newChannel.channel.inputSourceIdx !== undefined) {
// if different inputsourceidx, we have to manually switch to the channel
if (newChannel.channel.inputSourceIdx !== currentChannel?.inputSourceIdx) {
await this.switchToSourceIdx(parseInt(newChannel.channel.inputSourceIdx));
await this.wait(3000);
}
}
if (newChannel.type === ChannelType.TV) {
const sessionId = await this.netcastClient.get_session(this.deviceConfig.accessToken);
await this.netcastClient.change_channel(newChannel.channel, sessionId);
}
this.channelUpdateInProgress = false;
callback(null);
});
// Init all configurated user channels if they don't exist yet
for (const [i, chan] of this.deviceConfig.channels.entries()) {
let existingChanService = this.findInputService(chan.name);
if (existingChanService === null) {
this.platform.log.info('Creating new input service: ', chan.name);
existingChanService = this.accessory.addService(this.platform.Service.InputSource, chan.name, chan.name);
}
// set characteristics
existingChanService
.setCharacteristic(this.platform.Characteristic.ConfiguredName, chan.name)
.setCharacteristic(this.platform.Characteristic.Identifier, i)
.setCharacteristic(
this.platform.Characteristic.InputSourceType,
this.platform.Characteristic.InputSourceType.HDMI,
)
.setCharacteristic(
this.platform.Characteristic.IsConfigured,
this.platform.Characteristic.IsConfigured.CONFIGURED,
);
this.service.addLinkedService(existingChanService);
}
// === Remove input sources that are no longer being used
// Create map for easier access
const channelNameMap = {};
for (const c of this.deviceConfig.channels) {
channelNameMap[c.name] = null;
}
// Iterate through all the currently linked services
// If the service displayName is not inside the access map, remove it
for (const ser of this.accessory.services) {
for (const linkedSer of ser.linkedServices) {
if (channelNameMap[linkedSer.displayName] === undefined) {
this.platform.log.info('Removed unused input service: ', linkedSer.displayName);
this.service.removeLinkedService(linkedSer);
this.accessory.removeService(linkedSer);
}
}
}
}
wait(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
/**
* Sends an authorized command to the TV
* authorized meaning a valid session will be used for issuing the command
*
* @param {LG_COMMAND} cmd The command
*/
async sendAuthorizedCommand(cmd: LG_COMMAND) {
this.platform.log.debug('Sending command to TV: ', cmd, LG_COMMAND[cmd]);
const sessionId = await this.netcastClient.get_session(this.deviceConfig.accessToken);
return this.netcastClient.send_command(cmd, sessionId);
}
/**
* Switches the TV to the given inputsource idx This will literally open the
* input source selector and click LEFT/RIGHT enough times to reach the target
* channel. This is a VERY hacky way of doing this, but the netcast API
* doesn't allow any other way You can't switch from HDMI to a channel and you
* can't switch from a channel back to HDMI
*
* Instead of this way, it would be much better to use Simplink and HDMI
* through an AppleTV or Chromecast
*
* @param {number} idx The index to switch to
*/
async switchToSourceIdx(idx: number) {
const currentIdxStr = this.currentChannel?.inputSourceIdx;
if (currentIdxStr === undefined) {
return;
}
const currentIdx = parseInt(currentIdxStr);
// nothing to do here
if (currentIdx === idx) {
return;
}
this.platform.log.debug('Request to switch to input source idx: ', idx);
this.platform.log.debug('Current source idx: ', currentIdx);
// Open input source switch menu and wait 2s
// 2s because sometimes this menu can be pretty slow to load...
this.platform.log.debug('Opening InputSource selection');
await this.sendAuthorizedCommand(LG_COMMAND.EXTERNAL_INPUT);
await this.wait(2000);
// Click LEFT/RIGHT enough times for us to reach the target channel
// Then click "OK" to select it
if (currentIdx > idx) {
const diff = currentIdx - idx - 1;
for (let i = 1; i <= diff; i++) {
await this.sendAuthorizedCommand(LG_COMMAND.LEFT);
await this.wait(this.deviceConfig.keyInputDelay);
}
await this.sendAuthorizedCommand(LG_COMMAND.OK);
}
if (currentIdx < idx) {
const diff = idx - currentIdx - 1;
for (let i = 1; i <= diff; i++) {
await this.sendAuthorizedCommand(LG_COMMAND.RIGHT);
await this.wait(this.deviceConfig.keyInputDelay);
}
await this.sendAuthorizedCommand(LG_COMMAND.OK);
}
}
/**
* Updates the current channel state
*/
async updateCurrentChannel() {
if (this.offPause) {
return;
}
try {
const sessionId = await this.netcastClient.get_session(this.deviceConfig.accessToken);
this.currentChannel = await this.netcastClient.get_current_channel(sessionId);
} catch (e) {
this.currentChannel = null;
}
if (this.currentChannel === null) {
return;
}
// Check if we are currently switching channels. If yes, don't do anything to not interfere
if (this.channelUpdateInProgress) {
return;
}
// check all existing channels and see if the current channel matches with either of them
// if it does, update the active input source to that
for (const [i, chan] of this.deviceConfig.channels.entries()) {
if (
(chan.type === ChannelType.EXTERNAL && chan.channel.inputSourceIdx === this.currentChannel.inputSourceIdx) ||
(chan.type === ChannelType.TV &&
chan.channel.major === this.currentChannel.major &&
chan.channel.minor === this.currentChannel.minor)
) {
const currentActiveIdentifier = this.service.getCharacteristic(this.platform.Characteristic.ActiveIdentifier)
.value;
this.platform.log.debug(`Potentially identified active channel as '${chan.name}'`);
if (currentActiveIdentifier !== i) {
this.service.setCharacteristic(this.platform.Characteristic.ActiveIdentifier, i);
}
this.hideWildcardChannel();
return;
}
}
let chanName = this.currentChannel.chname || '';
if (typeof chanName === 'object') {
chanName = this.currentChannel.labelName || '';
}
if (typeof chanName === 'object') {
chanName = this.currentChannel.inputSourceName || '';
}
this.updateWildcardChannel(chanName);
}
updateWildcardChannel(name: string) {
this.platform.log.debug(`Creating temporary channel with name '${name}'`);
// add extra accessory for UNKNOWN
let existingChanService = this.findInputService(this.unknownChannelName);
if (existingChanService === null) {
existingChanService = this.accessory.addService(
this.platform.Service.InputSource,
this.unknownChannelName,
this.unknownChannelName,
);
}
existingChanService
.setCharacteristic(this.platform.Characteristic.ConfiguredName, name)
.setCharacteristic(this.platform.Characteristic.Identifier, this.unknownChannelIdentifier)
.setCharacteristic(
this.platform.Characteristic.InputSourceType,
this.platform.Characteristic.InputSourceType.OTHER,
)
.setCharacteristic(
this.platform.Characteristic.IsConfigured,
this.platform.Characteristic.IsConfigured.CONFIGURED,
)
.setCharacteristic(
this.platform.Characteristic.CurrentVisibilityState,
this.platform.Characteristic.CurrentVisibilityState.SHOWN,
);
this.service.addLinkedService(existingChanService);
const currentActiveIdentifier = this.service.getCharacteristic(this.platform.Characteristic.ActiveIdentifier).value;
if (currentActiveIdentifier !== this.unknownChannelIdentifier) {
this.service.setCharacteristic(this.platform.Characteristic.ActiveIdentifier, this.unknownChannelIdentifier);
}
}
hideWildcardChannel() {
const existingChanService = this.findInputService(this.unknownChannelName);
if (existingChanService === null) {
return;
}
if (
existingChanService.getCharacteristic(this.platform.Characteristic.CurrentVisibilityState).value ===
this.platform.Characteristic.CurrentVisibilityState.HIDDEN
) {
return;
}
this.platform.log.debug('Hiding temporary channel');
existingChanService.setCharacteristic(
this.platform.Characteristic.CurrentVisibilityState,
this.platform.Characteristic.CurrentVisibilityState.HIDDEN,
);
}
findInputService(name: string) {
for (const ser of this.accessory.services) {
for (const linkedSer of ser.linkedServices) {
if (linkedSer.displayName === name) {
this.platform.log.debug('Found existing input service: ', linkedSer.displayName);
return linkedSer;
}
}
}
return null;
}
}