|
38 | 38 |
|
39 | 39 |
|
40 | 40 | class LG_COMMAND(object): |
41 | | - """LG TV remote control commands.""" |
| 41 | + """LG TV remote control commands for ROAP protocol.""" |
42 | 42 |
|
43 | 43 | POWER = 1 |
44 | 44 | NUMBER_0 = 2 |
@@ -105,6 +105,64 @@ class LG_COMMAND(object): |
105 | 105 | SWITCH_VIDEO = 416 |
106 | 106 | APPS = 417 |
107 | 107 |
|
| 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 | + |
108 | 166 |
|
109 | 167 | class LG_QUERY(object): |
110 | 168 | """LG TV data queries.""" |
@@ -149,9 +207,14 @@ def __exit__(self, exc_type, exc_val, exc_tb): |
149 | 207 | """Context manager method to support with statement.""" |
150 | 208 | self._session = None |
151 | 209 |
|
152 | | - def send_command(self, command): |
| 210 | + def send_command(self, command, command_protocol=LG_PROTOCOL.ROAP): |
153 | 211 | """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 | + |
155 | 218 | message = self.COMMAND % ( |
156 | 219 | self._session, |
157 | 220 | LG_HANDLE_KEY_INPUT, |
@@ -274,6 +337,32 @@ def _send_to_tv(self, message_type, message=None, payload=None, udap=False): |
274 | 337 | ) |
275 | 338 | return response |
276 | 339 |
|
| 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 | + |
277 | 366 |
|
278 | 367 | class LgNetCastError(Exception): |
279 | 368 | """Base class for all exceptions in this module.""" |
|
0 commit comments