hobbyquaker / lgtv2

Control LG WebOS TV using node.js :tv:
MIT License
332 stars 45 forks source link

A question about Energy Saving control #20

Open opep opened 6 years ago

opep commented 6 years ago

I have a question: Would it be possible to control energy saving mode (Off, Low, Mid, High, Screen off) in a LG TV (SJ8000), running WebOS 3.7.0? Thanks for your clarification.

MaxvandeLaar commented 5 years ago

Hi @opep

Today I took the time to figure this out as well. The closest I got was by just sending the buttons in the correct order. It isn't a perfect way, but for me it seems te work fine. See #22 for sending buttons.

It all comes down to timing, so to prevent a callback hell I used async/await. Here is my code-snippet:

lgtv.on('connect', () => {
    lgtv.getSocket(
        'ssap://com.webos.service.networkinput/getPointerInputSocket',
        async (err, sock) => {
            if (!err) {
                await sendButton(sock, 'EXIT');
                await sendButton(sock, 'MENU');
                await sendButton(sock, 'UP');
                await sendButton(sock, 'ENTER');
                await sendButton(sock, 'RIGHT', 5000);
                await sendButton(sock, 'DOWN');
                await sendButton(sock, 'DOWN');
                await sendButton(sock, 'ENTER');
                await sendButton(sock, 'DOWN', 3000);
                await sendButton(sock, 'DOWN');
                await sendButton(sock, 'DOWN');
                await sendButton(sock, 'DOWN');
                await sendButton(sock, 'DOWN');
                await sendButton(sock, 'ENTER');
            }
        }
    );
});

function sendButton(sock, button, timeout = 1000){
    return new Promise((resolve, reject) => {
         setTimeout(() => {
             sock.send('button', {name: button.toUpperCase()});
             resolve();
         }, timeout);
    });
}
opep commented 5 years ago

Great! I am looking forward to try your findings, but it seems that I have to learn something first. From where are you running this code? How will it know the TV IP? I would like to run it from a Raspberry Pi3 running Home Assistant automation software (http://home-assistant.io). Thanks for you valuable help.

MaxvandeLaar commented 5 years ago

Wow, that is a fast reply after what(?) a half year.

So some background of my 'not yet existing' setup.

Currently I am running Openhab2 for my home automation, however, I am planning to migrate to Athom Homey which runs on NodeJS. JavaScript is my preferred language to work with so developing my own plugins won't be too difficult. However, before I will invest, I want to make sure I can do whatever I want and my LG tv is one of those requirements. I have looked at Home Assistant before, but it just didn't make the cut for me personally.

So for the code-snippet, I just quickly set up an NodeJS 10 project and added LGTV2 as a dependency. I gave my TV a fixed IP address way back so I just had to look that up. You can find this in the network settings of your tv. I am not sure if Home Assistant can trigger a NodeJS script.

The code-snippet is just a small part of the overall code. The test.js is a good start for setting up the connection and when lgtv receives the event 'connect' you can subscribe, request, or get the Websocket connection. I included a EnergySaving example project. Just ensure you have NPM and NodeJS (8+ at least I think 8 should be good) installed. Unzip it, run npm install and when that is finished run node example-energy-saving.js. Don't forget to change the IP adres in the JS file and maybe change the timeout durations as they can be too long/short for you.

I hope it made things a bit clearer for you, if you need any help let me know and I'll try to find some time to help you out.

EnergySaving.zip

opep commented 5 years ago

Thank you very much for taking your time writing a more detailed explation. Yeah, it is a long time since I opened the thread, but we all live almost always connected nowadays rsrs. I will digest what you wrote. I also have Node-Red in the same Pi3, which has a palette for Home Assistant. I believe that it might be the way. I am not good on programming but I already have some automations running in Node-Red. If I need something I may contact you again. Thanks!

opep commented 5 years ago

Also, in Node-Red there is a palette for integration with LG TVs. There is a node called "Request" (Sends arbitrary requests to the API). Since you like JavaScript, do you play with Node-Red? Can it be done that way? There is also nodes: Control and Button.

opep commented 5 years ago

I had progresses by the Node-red path. It seems that I will be able to do the same. Node-Red will be a good solution since it interfaces with Home Assistant.

opep commented 5 years ago

Got it to work using node-red. Certainly not the best way but it works. Missing the integration with HA which should not be difficult, but node-red can control the buttons and timing. I Should have tryed this path before. Thanks for your reply.

MaxvandeLaar commented 5 years ago

Glad to hear you got things to work! Good luck with the HA integration.