supersaiyanmode / PyWebOSTV

Python API for controlling LG TVs (Web OS)
MIT License
271 stars 50 forks source link

how to view current power status info #32

Open leifliddy opened 4 years ago

leifliddy commented 4 years ago

I'm trying find a way to verify whether the TV is turned on or off. Is there an function you can implement to return the current power status?

supersaiyanmode commented 4 years ago

I am not aware of any such API exposed by the TV. If there's one, it shouldn't be difficult for this library to provide one that is consistent with others.

That being said, I have used the status of discovery to deduce whether or not the TV is powered on. (Assuming the TV is listening when it is powered on)

benjamin-rousseau-shift commented 3 years ago

Yep exactly what @supersaiyanmode said. If you can't connect therefor it is down. You lose a little bit of time because it seems that there's quite a high timeout (I think 60 sec ?)

You can put the .connect() in a try statement. and do something if you stumble upon an exception. What I personally do is : Send a magic packet at the beginning whether I know if it's down or not because my function expect the TV to be up, and if it still can't connect it'll sleep 5 sec and resend a magic packet.

But really depends on what you're trying to achieve.

Gustry commented 3 years ago

Make a "ping" on the IP before using this python library.

benjamin-rousseau-shift commented 2 years ago

Just like @Gustry said you can just ping it. I did not come back here for a long time but lemme share a little piece of code I have for this :


from pywebostv.connection import WebOSClient
from wakeonlan import send_magic_packet
from time import sleep
import os

def connect(ip, mac):
    while True:
        # We try to access the TV.
        try:
            response = os.system("ping -n 1 -w 500 " + ip)
            if response == 0:
                print('[INFO] Connecting to IP %s' % ip)
                client = WebOSClient(ip)
                client.connect()
                break
            else:
                print('[INFO] IP still not responding. Sending Magic Packet to mac %s' % mac)
                send_magic_packet(mac)
                sleep(1)
        # If it's not up we turn it on via magic packet wooooo :)
        except:
            print('[INFO] Magic Packet sent. Let\'s sleep for 5 sec.')
            sleep(5)

    return client