Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sendButton command to send remote button presses #147

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions LGTV/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .scan import LGTVScan
from .remote import LGTVRemote
from .auth import LGTVAuth
from .cursor import LGTVCursor


search_config = [
Expand Down Expand Up @@ -158,9 +159,10 @@ def main():
else:
try:
kwargs = parseargs(args.command, args.args)
except Exception as e:
parser.print_help()
sys.exit(1)
except Exception:
if args.command not in {"sendButton"}:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite satisfied with this hack to add support for an extra command that isn't on the LGTVRemote class.

I wonder if we can do something to make exposing commands to the CLI cleaner, but that might be for future work.

parser.print_help()
sys.exit(1)

if args.name:
name = args.name
Expand All @@ -170,6 +172,12 @@ def main():
print("A TV name is required. Set one with -n/--name or the setDefault command.")
sys.exit(1)

if args.command == "sendButton":
cursor = LGTVCursor(name, **config[name], ssl=args.ssl)
cursor.connect()
cursor.execute(args.args)
return

try:
ws = LGTVRemote(name, **config[name], ssl=args.ssl)

Expand Down
53 changes: 48 additions & 5 deletions LGTV/cursor.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
# Stub for cursor support.

import inspect
from time import sleep

from .remote import LGTVRemote
from ws4py.client.threadedclient import WebSocketClient


class LGTVCursor(WebSocketClient):

def __finalize(self, response):
self.remote.close()
address = response['payload']['socketPath']
super(LGTVCursor, self).__init__(address, exclude_headers=["Origin"])

def __init__(self, name, ip=None, mac=None, key=None, hostname=None):
self.remote = LGTVRemote(name, ip, mac, key, hostname)
def __init__(self, name, ip=None, mac=None, key=None, hostname=None, ssl=False):
self.remote = LGTVRemote(name, ip, mac, key, hostname, ssl)
self.remote.connect()
self.remote.getCursorSocket(self.__finalize)
self.remote.execute("getCursorSocket", {"callback": self.__finalize})
self.remote.run_forever()

def _list_possible_buttons(self):
buttons = []
self_class = self.__class__.__name__

for name, method in inspect.getmembers(self, predicate=inspect.ismethod):
if name.startswith("_"):
continue

if name in {"execute"}:
continue

if not method.__qualname__.startswith(f"{self_class}."):
continue

buttons.append(name)

return buttons

def execute(self, buttons):
if not buttons:
print("Add button presses to perform. Possible options:", ", ".join(self._list_possible_buttons()))
return

for i, button in enumerate(buttons):
if not hasattr(self, button):
print(f"{button} is not a possible button press, skipped")
continue

if i != 0:
sleep(0.1)

getattr(self, button)()

def up(self):
self.send("type:button\nname:UP\n\n")
Expand All @@ -34,5 +72,10 @@ def back(self):
self.send("type:button\nname:BACK\n\n")

def enter(self):
self.remote.sendEnterKey()

self.send("type:button\nname:ENTER\n\n")

def home(self):
self.send("type:button\nname:HOME\n\n")

def exit(self):
self.send("type:button\nname:EXIT\n\n")
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ All devices with firmware major version 4, product name "webOSTV 2.0"
lgtv --name MyTV --ssl openYoutubeURL <url>
lgtv --name MyTV --ssl openYoutubeLegacyId <videoid>
lgtv --name MyTV --ssl openYoutubeLegacyURL <url>
lgtv --name MyTV --ssl sendButton <button>
lgtv --name MyTV --ssl serialise
lgtv --name MyTV --ssl setInput <input_id>
lgtv --name MyTV --ssl setSoundOutput <tv_speaker|external_optical|external_arc|external_speaker|lineout|headphone|tv_external_speaker|tv_speaker_headphone|bt_soundbar>
Expand Down