lg webos via Python

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
V_J
Experienced User
Posts: 237
Joined: Tue Mar 04, 2014 9:00 am

lg webos via Python

Post by V_J » Thu May 14, 2020 5:22 pm

Hello,

I found this python library ( https://pypi.org/project/pywebostv/ ) to communicate with an LG TV. I manage to get some things done in scripts, but am not sure how to start writing a plugin...

In "C:\Program Files (x86)\EventGhost\lib27\site-packages", I added the folders pywebostv and ws4py with respectively the files in https://pypi.org/project/pywebostv/ and https://ws4py.readthedocs.io/en/latest/

Then, following the instructions on https://pypi.org/project/pywebostv/ , I managed to get the connection to the TV using this script:

Code: Select all

from pywebostv.discovery import *    # Because I'm lazy, don't do this.
from pywebostv.connection import *
from pywebostv.controls import *

# The 'store' gets populated during the registration process. If it is empty, a registration prompt
# will show up on the TV. You can pass any dictionary-like interface instead -- that when values are
# set, will persist to a DB, a config file or something similar.
store = {}

# Scans the current network to discover TV. Avoid [0] in real code. If you already know the IP,
# you could skip the slow scan and # instead simply say:
#    client = WebOSClient("<IP Address of TV>")
client = WebOSClient.discover()[0]
client.connect()
for status in client.register(store):
    if status == WebOSClient.PROMPTED:
        print("Please accept the connect on the TV!")
    elif status == WebOSClient.REGISTERED:
        print("Registration successful!")
(one change: I have to put the IP address of the TV, the discover function throws an error after some time)

After this script, I can e.g. query the volume with "media.get_volume() ".

The issues I have
  • The connection is not persistent: I need to find a way to store "client", otherwise the TV always pops up the pairing question. How do I store it between different executions of EventGhost?
  • How do I start structuring this into a plugin? (I've never written one, just dabbled a bit in existing ones)
Thanks!

User avatar
Sem;colon
Plugin Developer
Posts: 755
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: lg webos via Python

Post by Sem;colon » Thu May 14, 2020 8:27 pm

Hi Jörg,

Did you know there is already a plugin for LG TVs?
I remember that I've seen that, unfortunately I couldn't use it as my TV is too old (no smart tv yet)
Perhaps the work is already done ;)
If you like my work, Image me a drink :wink:

V_J
Experienced User
Posts: 237
Joined: Tue Mar 04, 2014 9:00 am

Re: lg webos via Python

Post by V_J » Fri May 15, 2020 7:27 am

Yes, there are several... There is this one ( http://eventghost.net/forum/viewtopic.php?f=5&t=5415 , which turned into the LG-2012IP plugin ( viewtopic.php?f=10&t=6144 ), but I cannot get it to accept the TV. I suspect that the authentication has changed, as the documentation mentions getting the key from the settings menu in the TV, but in mine (2017 model) there has to be a request sent to the TV, which prompts an accept dialog that has to be confirmed on the TV side.

The code I mentioned above works for my TV: when put in a single Python script in Eventghost, first the TV pops ups the accept dialog, and next I can call some media controller (to e.g. adjust volume or even request it).

All the pieces therefor seem present to make this code work: the connection, the actions and the events... I feel it is a matter of puzzling it together to get a plugin...

User avatar
Sem;colon
Plugin Developer
Posts: 755
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: lg webos via Python

Post by Sem;colon » Fri May 15, 2020 10:00 am

Hi Jörg,
In that case, I'd recommend you take a looks at the code from the old plugin to learn from it. That's how I created my first plugin as well ;)
If you like my work, Image me a drink :wink:

V_J
Experienced User
Posts: 237
Joined: Tue Mar 04, 2014 9:00 am

Re: lg webos via Python

Post by V_J » Fri May 15, 2020 5:04 pm

Yep... Would that be before or after adding the O-Mega interface.... :lol:

Now just finalizing the interfacing in Loxone and with the Squeezebox and amplifier.
Next software functions (which is easier), and then the TV... :)

User avatar
Sem;colon
Plugin Developer
Posts: 755
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: lg webos via Python

Post by Sem;colon » Fri May 15, 2020 8:31 pm

Haha, after that, of course! :lol:
If you like my work, Image me a drink :wink:

Post Reply