4.2.0 • Published 4 months ago

lgtv-ip-control v4.2.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 months ago

LG TV IP Control

This is a JS library that implements TCP network control for LG TVs manufactured since 2018. It utilizes encryption rules based on a guide found on the internet. A non-encrypted mode is provided for older models, but hasn't been tested.

This library is not provided by LG, and it is not a complete implementation for every TV model.

Check compatibility and TV setup instructions on the root's README.md.

Requirements

  • Node 16+ (at least ES2017)
  • NPM or Yarn Classic

Installation

# Using NPM
npm install lgtv-ip-control


# Using Yarn
yarn add lgtv-ip-control

Usage

Here's a very basic example of how to control the TV:

import { Inputs, LGTV } from 'lgtv-ip-control';

const lgtv = new LGTV('192.168.1.100', '1a:2b:3c:4d:5e:6f', 'KEY1C0DE');

lgtv
  .connect()
  .then(async () => {
    console.log('Disconnect now');
    await new Promise((resolve) => setTimeout(resolve, 5000));
    console.log('Unmutting...');
    await lgtv.setVolumeMute(false);
    console.log('Select HDMI1 input...');
    await lgtv.setInput(Inputs.hdmi1);
    console.log('Done!');
  })
  // Log any errors
  .catch(console.error)
  // Tries disconnecting once done
  .finally(() => lgtv.disconnect());

To use import, you need to make sure to save the file as .mjs or to specify "type": "module" on your package.json. Learn more about Node.js' support for ESM.

Otherwise replace the first line with:

const { Inputs, LGTV } = require('lgtv-ip-control');

new LGTV()

Returns a new instance to control a TV.

const lgtv = new LGTV(
  /**
   * TV IP Address
   */
  '192.168.1.100',

  /**
   * TV MAC Address for being able to turn the TV on remotely, `null` otherwise
   */
  '1a:2b:3c:4d:5e:6f',

  /**
   * Encryption Keycode, as generated during "Setting Up the TV" above.
   * If not provided, uses clear text, but is required by most models.
   */
  'KEY1C0DE',

  /**
   * Additional options (optional)
   *
   * See src/constants/DefaultSettings.ts file.
   */
  {
    ...DefaultSettings,
  },
);

.connect(): Promise<void>

Connects to the TV using TCP.

Required before sending any commands.

await lgtv.connect();

.disconnect(): void

Disconnects from the TV.

lgtv.disconnect();

.getCurrentApp(): Promise<Apps | string | null>

Gets the current app. May be one of the Apps enum or an arbitrary string if the app type is unknown. Might return null if the TV is powered off but still responding.

const currentApp = await lgtv.getCurrentApp();

.getCurrentVolume(): Promise<number>

Gets the current volume as an integer from 0 to 100.

const currentVolume = await lgtv.getCurrentVolume();

.getIpControlState(): Promise<boolean>

Gets the ip control state.

const ipControlState = await lgtv.getIpControlState();

.getMacAddress(type: 'wired' | 'wifi'): Promise<string>

Gets the MAC address by network interface.

const macAddress = await lgtv.getMacAddress('wired');

.getMuteState(): Promise<boolean>

Gets the mute state.

const muteState = await lgtv.getMuteState();

.getPowerState(): Promise<PowerStates>

Gets the current TV power state.

Because the TV might be offline, you should call this function before calling .connect(), otherwise you can get a TimeoutError.

const powerState = await lgtv.getPowerState();

See PowerStates for available states.

.powerOff(): Promise<void>

Powers the TV off.

await lgtv.powerOff();

.powerOn(): void

Powers the TV on, using Wake On Lan. Requires MAC address to be set when creating the LGTV instance.

lgtv.powerOn();

.powerOnAndConnect(): Promise<void>

Powers the TV on, using Wake On Lan, and connects to it. Requires MAC address to be set when creating the LGTV instance. Returns a promise that resolves once the connection is established, or rejects after a number of retries.

await lgtv.powerOnAndConnect();

.sendKey(key: Keys): Promise<void>

Sends a key, as if it was pressed on the TV remote control.

await lgtv.sendKey(Keys.menu);

See Keys for available keys.

.setEnergySaving(level: EnergySavingLevels): Promise<void>

Sets the current energy saving level. Note that screenOff is known not to work for some models.

await lgtv.setEnergySaving(EnergySavingLevels.maximum);

See EnergySavingLevels for available levels.

.setInput(input: Inputs): Promise<void>

Sets the current TV input.

await lgtv.setInput(Inputs.hdmi1);

See Inputs for available inputs.

.setVolume(volume: number): Promise<void>

Sets the volume level as an integer from 0 to 100.

await lgtv.setVolume(15);

.setVolumeMute(isMuted: boolean): Promise<void>

Sets the volume mute state.

await lgtv.setVolumeMute(false);

.setScreenMute(mode: ScreenMuteModes): Promise<void>

Sets the current screen mute mode. This can be used to either completely blank the screen or just blank the video feed while leaving the OSD visible. Returns a promise.

await lgtv.setScreenMute(ScreenMuteModes.screenmuteon);

See ScreenMuteModes for available modes.

Available Lists

EnergySavingLevels

KeyBrightness Level
autoAutomatic
screenOffScreen Off
maximumLow
mediumMedium
minimumHigh
offMaximum

Inputs

KeyInput
dtvDigital TV
atvAnalog TV
cadtvCable Digital TV
catvCable TV
avAV Composite
componentComponent
hdmi1HDMI 1
hdmi2HDMI 2
hdmi3HDMI 3
hdmi4HDMI 4

Keys

KeyRemote Button
arrowDownArrow Down
arrowLeftArrow Left
arrowRightArrow Right
arrowUpArrow Up
aspectRatioAspect Ratio Toggle
audioModeAudio Mode Toggle
backBack
blueButtonBlue Button
captionSubtitle
channelDownChannel Down
channelListChannel List
channelUpChannel Up
deviceInputDevice Input Toggle
energySavingEnergy Saving Toggle
fastForward
greenButtonGreen Button
homeHome
infoInfo
liveTVLive TV
menuOpen Menu
number0Number 0
number1Number 1
number2Number 2
number3Number 3
number4Number 4
number5Number 5
number6Number 6
number7Number 7
number8Number 8
number9Number 9
okOk
play
previousChannelPrevious Channel
programGuideShow Program Guide
record
redButtonRed Button
rewind
sleepTimerSleep Timer Toggle
userGuideOpen User Guide
videoModeVideo Mode Toggle
volumeDownVolume Down
volumeMuteMute Toggle
volumeUpVolume Up
yellowButtonYellow Button

PowerStates

KeyState
onThe TV is on and responding to connections
offThe TV is off or powering off
unknownThe state of the TV is unknown, possibly off

ScreenMuteModes

KeyEffect
screenMuteOnBlank screen
videoMuteOnBlank video, OSD visible
allMuteOffNormal Operation
4.2.0

4 months ago

4.1.0

4 months ago

4.1.1

4 months ago

4.0.1

5 months ago

4.0.0

5 months ago

4.0.2

5 months ago

3.1.0

5 months ago

3.0.1

5 months ago

3.0.0

1 year ago

2.0.2

2 years ago

2.0.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.4.0-beta.1

2 years ago

1.4.0-beta.0

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago