cmer / lg-tv-control-macos

Automatically wake/sleep and change the input of your LG TV when used as a monitor on macOS
123 stars 12 forks source link

Debug improvements and bug fixes. #9

Closed zakkhoyt closed 1 year ago

zakkhoyt commented 1 year ago

About

I found myself trying to do some debugging for my set up and ended up modifying lgtv_init.lua. I thought I'd put up a PR with some of the changes.

Summary of Changes

zakkhoyt commented 1 year ago

Thanks for this. Could you remove .gitignore?

Hey @cmer Sorry for the delay. File removed.

BTW, apologies that this PR kind of ballooned. Ideally this should have been submitted as a series of smaller/focused PRs. I know this can make PRs hard to review and make reviewers reluctant to prioritize the PR.

Will do better next time :)

MaxWinterstein commented 1 year ago

Thanks @zakkhoyt - this one does finally work for me ❤️

I had some issues, as I use an M1 MacBook and mixed some arch stuff. I created the python venv using an rosetta terminal, therefore it was i386. Hammerspoons runs native on arm64.

Calls to the lgtv app did not raise any error:

2023-05-25 10:57:41: -- Loading ~/.hammerspoon/init.lua
2023-05-25 10:57:41: TV name: MyTV
2023-05-25 10:57:41: TV input: HDMI_1
2023-05-25 10:57:41: LGTV path: ~/opt/lgtv/bin/lgtv
2023-05-25 10:57:41: LGTV command: ~/opt/lgtv/bin/lgtv MyTV
2023-05-25 10:57:41: SSL: true
2023-05-25 10:57:41: App ID: com.webos.app.hdmi1
2023-05-25 10:57:41: lgtv_disabled: false
2023-05-25 10:57:41: Executing command: ~/opt/lgtv/bin/lgtv MyTV swInfo ssl
2023-05-25 10:57:41: 
2023-05-25 10:57:41: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:57:41: 
2023-05-25 10:57:41: -- Loading extension: screen
2023-05-25 10:57:41: Connected screens: { [1] = hs.screen: LG TV SSCR2 (0x600003ab6eb8),} 
2023-05-25 10:57:41: LG TV is connected
2023-05-25 10:57:41: TV is connected? true
2023-05-25 10:57:41: -- Loading extension: caffeinate
2023-05-25 10:57:41: -- Done.
2023-05-25 10:57:52: Received event: 1 systemWillSleep
2023-05-25 10:57:52: LG TV is connected
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV off ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: TV screen was turned off with command `off`.
2023-05-25 10:57:52: Received event: 3 screensDidSleep
2023-05-25 10:57:52: LG TV is connected
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV off ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: TV screen was turned off with command `off`.
2023-05-25 10:57:52: Received event: 10 screensDidLock
2023-05-25 10:57:52: Event ignored
2023-05-25 10:58:09: Received event: 4 screensDidWake
2023-05-25 10:58:09: LG TV is connected
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV on ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV was turned on
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV startApp ssl com.webos.app.hdmi1
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV input switched to com.webos.app.hdmi1
2023-05-25 10:58:09: Event ignored
2023-05-25 10:58:09: Received event: 0 systemDidWake
2023-05-25 10:58:09: LG TV is connected
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV on ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV was turned on
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV startApp ssl com.webos.app.hdmi1
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV input switched to com.webos.app.hdmi1
2023-05-25 10:58:09: Event ignored
2023-05-25 10:58:20: Received event: 11 screensDidUnlock
2023-05-25 10:58:20: LG TV is connected
2023-05-25 10:58:20: Executing command: ~/opt/lgtv/bin/lgtv MyTV on ssl
2023-05-25 10:58:20: 
2023-05-25 10:58:20: TV was turned on
2023-05-25 10:58:20: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:58:20: 
2023-05-25 10:58:20: Executing command: ~/opt/lgtv/bin/lgtv MyTV startApp ssl com.webos.app.hdmi1
2023-05-25 10:58:20: 
2023-05-25 10:58:20: TV input switched to com.webos.app.hdmi1
2023-05-25 10:58:20: Event ignored

( I enabled debug flag and removed the input selection check at that point).

No lua knowledge here, but I think a good improvement might be to check the return/status code of hs.execute? At least would have brought me faster to recognize my mistake 😉

zakkhoyt commented 1 year ago

Thanks @zakkhoyt - this one does finally work for me ❤️

I had some issues, as I use an M1 MacBook and mixed some arch stuff. I created the python venv using an rosetta terminal, therefore it was i386. Hammerspoons runs native on arm64.

Calls to the lgtv app did not raise any error:

2023-05-25 10:57:41: -- Loading ~/.hammerspoon/init.lua
2023-05-25 10:57:41: TV name: MyTV
2023-05-25 10:57:41: TV input: HDMI_1
2023-05-25 10:57:41: LGTV path: ~/opt/lgtv/bin/lgtv
2023-05-25 10:57:41: LGTV command: ~/opt/lgtv/bin/lgtv MyTV
2023-05-25 10:57:41: SSL: true
2023-05-25 10:57:41: App ID: com.webos.app.hdmi1
2023-05-25 10:57:41: lgtv_disabled: false
2023-05-25 10:57:41: Executing command: ~/opt/lgtv/bin/lgtv MyTV swInfo ssl
2023-05-25 10:57:41: 
2023-05-25 10:57:41: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:57:41: 
2023-05-25 10:57:41: -- Loading extension: screen
2023-05-25 10:57:41: Connected screens: { [1] = hs.screen: LG TV SSCR2 (0x600003ab6eb8),} 
2023-05-25 10:57:41: LG TV is connected
2023-05-25 10:57:41: TV is connected? true
2023-05-25 10:57:41: -- Loading extension: caffeinate
2023-05-25 10:57:41: -- Done.
2023-05-25 10:57:52: Received event: 1 systemWillSleep
2023-05-25 10:57:52: LG TV is connected
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV off ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: TV screen was turned off with command `off`.
2023-05-25 10:57:52: Received event: 3 screensDidSleep
2023-05-25 10:57:52: LG TV is connected
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: Executing command: ~/opt/lgtv/bin/lgtv MyTV off ssl
2023-05-25 10:57:52: 
2023-05-25 10:57:52: TV screen was turned off with command `off`.
2023-05-25 10:57:52: Received event: 10 screensDidLock
2023-05-25 10:57:52: Event ignored
2023-05-25 10:58:09: Received event: 4 screensDidWake
2023-05-25 10:58:09: LG TV is connected
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV on ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV was turned on
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV startApp ssl com.webos.app.hdmi1
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV input switched to com.webos.app.hdmi1
2023-05-25 10:58:09: Event ignored
2023-05-25 10:58:09: Received event: 0 systemDidWake
2023-05-25 10:58:09: LG TV is connected
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV on ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV was turned on
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:58:09: 
2023-05-25 10:58:09: Executing command: ~/opt/lgtv/bin/lgtv MyTV startApp ssl com.webos.app.hdmi1
2023-05-25 10:58:09: 
2023-05-25 10:58:09: TV input switched to com.webos.app.hdmi1
2023-05-25 10:58:09: Event ignored
2023-05-25 10:58:20: Received event: 11 screensDidUnlock
2023-05-25 10:58:20: LG TV is connected
2023-05-25 10:58:20: Executing command: ~/opt/lgtv/bin/lgtv MyTV on ssl
2023-05-25 10:58:20: 
2023-05-25 10:58:20: TV was turned on
2023-05-25 10:58:20: Executing command: ~/opt/lgtv/bin/lgtv MyTV getForegroundAppInfo ssl
2023-05-25 10:58:20: 
2023-05-25 10:58:20: Executing command: ~/opt/lgtv/bin/lgtv MyTV startApp ssl com.webos.app.hdmi1
2023-05-25 10:58:20: 
2023-05-25 10:58:20: TV input switched to com.webos.app.hdmi1
2023-05-25 10:58:20: Event ignored

( I enabled debug flag and removed the input selection check at that point).

No lua knowledge here, but I think a good improvement might be to check the return/status code of hs.execute? At least would have brought me faster to recognize my mistake 😉

@MaxWinterstein Thanks for the feedback. Great idea. I can look into that in a follow up PR as this PR is pretty bloated already.

cmer commented 1 year ago

Great work! Just for the record, I had issues where my computer would think the TV was not connected when TV was turned off. It seems like the TV has multiple "off" states that behave differently. ie: recently off vs been off for a while.