klattimer / LGWebOSRemote

Command line webOS remote for LGTVs
MIT License
490 stars 97 forks source link

Hammerspoon stuck after powering on TV because `lgtv ... on` does not return. #133

Closed xane256 closed 7 months ago

xane256 commented 7 months ago

I am experiencing an issue similar to #124, using an LG C3 42", model OLED42C3PUA The command lgtv --name c3 --ssl on works and turns on the TV but doesn't return / exit. So Hammerspoon is able to turn on the TV but it hangs afterwards and requires force quitting.

The output of the above command is

DEBUG:root:Initiating handshake
DEBUG:root:Received response
DEBUG:root:{"type":"registered","id":"register_0","payload":{"client-key":"d83083dcb1f148b03a4c18496018064d"}}
DEBUG:root:Handshake complete

The config ~/.lgtv/config.json has the correct MAC address of the TV and running wakeonlan 84:90:0a:95:02:18 in a shell correctly turns on the TV and exits.

Here is a watcher I added to lgtv_init.lua while troubleshooting. I replaced the provided watcher with this one. The print statements before the exec_command("on") sometimes don't print even though that's the line that freezes.

simplewatcher = hs.caffeinate.watcher.new(function(event_type)
    event_name = event_type_description(event_type)
    log_d("Received event: " .. (event_type or "") .. " " .. (event_name))
    if lgtv_disabled() then
        log_d("LGTV feature disabled. Skipping.")
        return
    end
    if (event_type == hs.caffeinate.watcher.screensDidWake or event_type ==
        hs.caffeinate.watcher.systemDidWake or event_type ==
        hs.caffeinate.watcher.screensDidUnlock) then
        sw_info = exec_command("swInfo")
        log_d("got info: " .. sw_info)
        if not tv_is_connected() then
            log_d("TV was not turned on because it is not connected")
            return
        end
        log_d("before power on")
        exec_command("on")
        log_d("after power on")
    end

end)
Tenzer commented 7 months ago

Unaware of this ticket, I just made an improvement to the "on" command so it no longer tries to create a websocket connection to the TV when we want it to turn on: https://github.com/klattimer/LGWebOSRemote/pull/141. It has been merged in now. It's possible that might help you out.