supersaiyanmode / PyWebOSTV

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

Script on Win10 #48

Closed ckorodenis closed 3 years ago

ckorodenis commented 3 years ago

I would like to use your script on Win10-64bit to turn the TV on and off. Is it possible to invent it? I tried different procedures, I have Python 3.9.0 installed, but still unsuccessful. Theoretically, a script executable on Win10 would be enough for me to turn off the TV

I have turned on the TV with WakeOnLan

supersaiyanmode commented 3 years ago

Hi there, as called out in the README, this library doesn't support WakeOnLAN calls as a part of it. This is because we don't want to our clients to have to pull in an optional transitive dependency. But implementing this should be pretty trivial, IMO.

This library, however, will provide you the IP address post-discovery. You can use that eventually be able to send a WakeonLAN packet to your TV.

ckorodenis commented 3 years ago

There was a misunderstanding. I don't need a script containing WakeOnLan. I need a script that turns off LG tv WebOS and runs on Win10. Thank you for your willingness 👍

Kvanrooyen commented 3 years ago

@ckorodenis This might not be exactly what you are looking for, but it could be a starting point. You will need to change it slightly to make it work like a script, but the waking of the TV logic is there. If you need help converting it into a script let me know

ckorodenis commented 3 years ago

Thank you for your offer. :) If there is a possibility of your help I will be happy. I'm afraid this would be beyond my capabilities as an amateur enthusiast.

I'm still not sure we fully understand each other. I would like to create a script (.bat/.exe/.ps ...) for Win10 that will turn off LG tv WebOS Run script => turn off LG tv

... Script to turn on LG tv I have already written and created functional.

Kvanrooyen commented 3 years ago

In my program that I wrote, when I want to turn the TV off, the following line is run system.power_off. If you connect to the TV and then have that as the line that is run/executed, then all you need is for it to be a .bat/.exe etc.

There is a PIP package - I forget the name now - that convert your Python package to a executable file. When I remember it's name I will let you know.

If I get a chance tomorrow after work, I can help you out with creating the script

supersaiyanmode commented 3 years ago

@ckorodenis Please show your code and attempt so far, we might be able to better help you..

ckorodenis commented 3 years ago

I use this script to turn on the TV. Written according to the template on the website https://gallery.technet.microsoft.com/. Subsequently, I converted it from a .ps1 file to an .exe file.

# Script to wake up a computer.
#
# v1.0 - 2012-07-30 Pedro Castro
# usage:
#       .\Ligar-Maquina.ps1 -mac 00:1D:92:51:4C:41

param(
    [string]$mac = '64:95:6C:78:14:9E' #address of the network card (MAC address)
)

#checks the syntax of MAC address
if (!($mac -like "*:*:*:*:*:*") -or ($mac -like "*-*-*-*-*-*")){
    write-error "mac address not in correct format"
    break
}

#build magic package http://en.wikipedia.org/wiki/Wake-on-LAN#Magic_packet 
$string=@($mac.split(":""-") | foreach {$_.insert(0,"0x")})
$target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])
# The magic packet is a broadcast frame containing anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal)
$packet = [byte[]](,0xFF * 102)
# followed by sixteen repetitions of the target computer's 48-bit MAC address, for a total of 102 bytes.
6..101 |% { $packet[$_] = $target[($_%6)]}

# .NET framework lib para sockets
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$UDPclient.Send($packet, $packet.Length) | out-null
`

I do not use the IP address of the TV in the script ... IP address of the TV: 192.168.1.89

supersaiyanmode commented 3 years ago

Hi @ckorodenis I don't follow. I can surely help you with any issue that relates to usage of this library or even python in general. But I haven't used Windows in a long long time (I just read up on PS1 and the script, from a quick glance, looks okay).

If you want to turn off your TV: 1) Install this library (pip install pywebostv) to your system or virtualenv 2) Copy paste the code in the README 3) Copy over the section from "System Controls" section 4) Execute the code via > C:\path\to\python.exe path\to\your\script.py. 5) Hopefully, if you did everything right, you should see your TV turning off.

BTW, everything related to PS1, conversion from PS1 to EXE (or py to exe), are out of scope for this repository. May be a quick question at stackoverflow.com would help?

ckorodenis commented 3 years ago

Honestly, I have a problem right at the beginning. The pip install pywebostv command reports a syntax error. I tried to install different versions of python. But without success.

And should I use the code in this state? Where should this code be placed? Right after establishing a connection with the TV? I'm a little disoriented, sorry.

system = SystemControl(client)
system.power_off()                                # Turns off the TV. There is no way to turn it

I'll handle the file conversion somehow, or I'll run .py directly.

supersaiyanmode commented 3 years ago

I just verified that the pip install pywebostv works just fine. Can you copy-paste the output here? It seems like the problem you're having is more to do with the python installation itself rather than this project.

As for your second question, yes, you should place that block of code after the connection with the TV has been established.

Kvanrooyen commented 3 years ago

@ckorodenis I haven't been able to test this code yet - I can test it works when I get back home - but it should still give you a understanding of how it would work using this library.

import sys
from wakeonlan import send_magic_packet
from pywebostv.discovery import discover
from pywebostv.connection import WebOSClient

# Send a wake on lan broadcast to your TV.
# This will ensure the TV is on, before trying to connect.
send_magic_packet('<TV MAC ADDRESSES>')

# 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.
store = {}

client = WebOSClient("<TV IP>")
client.connect()
# This for statement is optional since it will only write out to the console if it connected or if it failed - I would suggest keeping it
for status in client.register(store):
    if status == WebOSClient.PROMPTED:
        print("\nPlease accept the connect on the TV!")
    elif status == WebOSClient.REGISTERED:
        print("\nRegistration successful!")
        # print(store)

As for the section on turning the TV off, you can use a lot of the same code. Only difference being that we will not be using the wake on lan - since this is to turn the TV on.

import sys
from pywebostv.discovery import discover
from pywebostv.connection import WebOSClient

# 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.
store = {}

client = WebOSClient("<TV IP>")
client.connect()
# This for statement is optional since it will only write out to the console if it connected or if it failed - I would suggest keeping it
for status in client.register(store):
    if status == WebOSClient.PROMPTED:
        print("\nPlease accept the connect on the TV!")
    elif status == WebOSClient.REGISTERED:
        print("\nRegistration successful!")
        # print(store)

# After we have connected to the TV, we the below line will run and turn the TV off
system.power_off()
ckorodenis commented 3 years ago

I tried to install different versions of python, it always ends the same. PIP should be installed along with python, but I don't know if this is the case. I don't know where I'm making a mistake, unfortunately.

pip install pywebostv
   File "<stdin>", line 1
     pip install pywebostv
         ^
SyntaxError: invalid syntax

It will be enough to add only the IP address of the TV in the code, am I right? I can ask exactly what it does or how the "store = {}" command works?

Gustry commented 3 years ago

I can ask exactly what it does or how the "store = {}" command works?

You should have a look to the readme, with these comments about the store. https://github.com/supersaiyanmode/PyWebOSTV#establishing-the-connection

supersaiyanmode commented 3 years ago

I tried to install different versions of python, it always ends the same. PIP should be installed along with python, but I don't know if this is the case. I don't know where I'm making a mistake, unfortunately.

pip install pywebostv
 File "<stdin>", line 1
   pip install pywebostv
       ^
SyntaxError: invalid syntax

Where are you typing this command? You should execute it at a command prompt (cmd) not at a python prompt. I'd suggest asking the question at Stackoverflow if you still have problems (with perhaps a screenshot)

Kvanrooyen commented 3 years ago

I tried to install different versions of python, it always ends the same. PIP should be installed along with python, but I don't know if this is the case. I don't know where I'm making a mistake, unfortunately.

pip install pywebostv
 File "<stdin>", line 1
   pip install pywebostv
       ^
SyntaxError: invalid syntax

Your answer to this question can be found here. I did a quick Google search for "Python PIP package" and it was the third option.

ckorodenis commented 3 years ago

Yes, I enter the command in cmd ... the pip package was successfully installed, but another command does not want to run correctly.

Could I use a local installation from the archive instead of the "pip install pywebostv" command?

Kvanrooyen commented 3 years ago

but another command does not want to run correctly.

Which command? Can you post a screenshot or an error message. If it isn't related to this package, I would suggest asking or looking on stackoverflow

supersaiyanmode commented 3 years ago

The formatting of the error message, esp: SyntaxError: invalid syntax seems to be identical to how python throws it -- this leads me to believe that you're entering the pip install command on a python prompt instead of a regular command prompt. You'd be better off with a question at Stackoverflow with the screenshot preferably since this doesn't seem to be related to the library at all.

I'm closing this issue for now. Please feel free to re-open if you have pywebostv specific question. And feel free to post the link to the Stackoverflow question here if you'd like.