Skip to content

Commit eab8ad3

Browse files
committed
Add specific commands for HDCP protocol, allow translation between the two.
This helps in keeping upstream functionality simple. You can use ROAP commands and they will be translated to HDCP automatically (as far as supported). Use `client.send_command(2, command_protocol=LG_PROTOCOL.HDCP)` when you're sending native HDCP commands.
1 parent bea87f6 commit eab8ad3

1 file changed

Lines changed: 92 additions & 3 deletions

File tree

pylgnetcast/pylgnetcast.py

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939

4040
class LG_COMMAND(object):
41-
"""LG TV remote control commands."""
41+
"""LG TV remote control commands for ROAP protocol."""
4242

4343
POWER = 1
4444
NUMBER_0 = 2
@@ -105,6 +105,64 @@ class LG_COMMAND(object):
105105
SWITCH_VIDEO = 416
106106
APPS = 417
107107

108+
class LG_COMMAND_HDCP(object):
109+
"""LG TV remote control commands for HDCP protocol."""
110+
111+
POWER = 8
112+
NUMBER_0 = 16
113+
NUMBER_1 = 17
114+
NUMBER_2 = 18
115+
NUMBER_3 = 19
116+
NUMBER_4 = 20
117+
NUMBER_5 = 21
118+
NUMBER_6 = 22
119+
NUMBER_7 = 23
120+
NUMBER_8 = 24
121+
NUMBER_9 = 25
122+
MUTE_TOGGLE = 9
123+
HOME_MENU = 67
124+
DASH = 76
125+
FLASHBACK = 26
126+
CHANNEL_LIST = 83
127+
OK = 68
128+
CHANNEL_UP = 0
129+
CHANNEL_DOWN = 1
130+
VOLUME_UP = 2
131+
VOLUME_DOWN = 3
132+
UP = 64
133+
DOWN = 65
134+
LEFT = 7
135+
RIGHT = 6
136+
BACK = 40
137+
EXIT = 91
138+
CONFIRM = 68
139+
QUICK_MENU = 69
140+
RED = 114
141+
GREEN = 113
142+
YELLOW = 99
143+
BLUE = 97
144+
LIVE_TV = 158
145+
STOP = 177
146+
PLAY = 176
147+
PAUSE = 186
148+
SKIP_BACKWARD = 143
149+
SKIP_FORWARD = 142
150+
RECORD = 189
151+
EPG = 169
152+
ENERGY_SAVING = 149
153+
AV_MODE = 48
154+
EXTERNAL_INPUT = 11
155+
FAVORITE_CHANNEL = 30
156+
SIMPLINK = 126
157+
ASPECT_RATIO = 121
158+
PROGRAM_INFORMATION = 170
159+
NETCAST = 89
160+
GUIDE = 169
161+
SHOW_SUBTITLE = 57
162+
TELE_TEXT = 32
163+
TEXT_OPTION = 33
164+
AUDIO_DESCRIPTION = 145
165+
108166

109167
class LG_QUERY(object):
110168
"""LG TV data queries."""
@@ -149,9 +207,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
149207
"""Context manager method to support with statement."""
150208
self._session = None
151209

152-
def send_command(self, command):
210+
def send_command(self, command, command_protocol=LG_PROTOCOL.ROAP):
153211
"""Send remote control commands to the TV."""
154-
_LOGGER.debug(f"send_command: command={command}")
212+
_LOGGER.debug(f"send_command: {command=} {command_protocol=}")
213+
if self.protocol != command_protocol:
214+
command = self._translate_command(command, command_protocol, self.protocol)
215+
if command is None:
216+
return
217+
155218
message = self.COMMAND % (
156219
self._session,
157220
LG_HANDLE_KEY_INPUT,
@@ -274,6 +337,32 @@ def _send_to_tv(self, message_type, message=None, payload=None, udap=False):
274337
)
275338
return response
276339

340+
@staticmethod
341+
def _translate_command(command, from_protocol, to_protocol):
342+
"""
343+
Translate command from one protocol to another.
344+
345+
It would be better if commands were previously implemented as strings instead of integers,
346+
then this would a lot less ugly. But yay for legacy :)
347+
"""
348+
_LOGGER.debug(f"translate_command: {command=}, {from_protocol=}, {to_protocol=}")
349+
if from_protocol == LG_PROTOCOL.ROAP and to_protocol == LG_PROTOCOL.HDCP:
350+
source = LG_COMMAND
351+
target = LG_COMMAND_HDCP
352+
else:
353+
source = LG_COMMAND_HDCP
354+
target = LG_COMMAND
355+
356+
for attr in dir(target):
357+
if not attr.startswith("__"):
358+
if getattr(source, attr, None) == command:
359+
translated_command = getattr(target, attr, None)
360+
_LOGGER.info(f"Translated {from_protocol} command {command} to {to_protocol}: {translated_command}")
361+
return translated_command
362+
363+
_LOGGER.error(f"Command {command} not supported for {to_protocol} protocol.")
364+
return None
365+
277366

278367
class LgNetCastError(Exception):
279368
"""Base class for all exceptions in this module."""

0 commit comments

Comments
 (0)