Closed zakkhoyt closed 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 :)
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 😉
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.
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.
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
function log_d(message)
to handle printing debug messages. The function will print the message if isdebug
is enabled, otherwise do nothing. Replaced several occurrences ofif debug then ___
.function event_type_description(event_type)
to convert the caffeinate watcher event_type (int
) into a description (string
). Instead of:We get something like:
function exec_command(command)
anddebug
is enabled, thejson
response from the API call is printed before being returned."on"
or"screenOn"
, but not both (matches how"off"
and"screenOff"
work). I noticed that"on"
followed by"screenOn"
was resulting in an error for the later:state must be "screenOff"
. Why bother making the call if it's going to fail?.gitignore
to make developing a bit easier.~on
command was issued when the TV is not connected to the computer.