Open KibosJ opened 3 years ago
@justinmm2 Tks for that fragment, I added it to the README
One more for both @lukasroegner and @NorthernMan54 : in order to get it working now (on my ATV 4K/2017), I had to change the config to use "wakeup" and "suspend"; the example "wake"/"turn_off"/etc. commands did not work. There was also no need to do a second "suspend" with a sleep or anything.
I suspect these are the result of pyatv modifications, but can't comment on the propriety of doing these things widely; again, I include it for intrepid Googlers, and devs who may encounter the same thing(s). This was the result of going down a rabbit hole when I realized that the turn_off command was executing a select command; when I was trying to turn my TV off, I noticed it subtly going forward into menus and even playing things, depending on where my "cursor" was.
I suspect these are the result of pyatv modifications, but can't comment on the propriety of doing these things widely; again, I include it for intrepid Googlers, and devs who may encounter the same thing(s). This was the result of going down a rabbit hole when I realized that the turn_off command was executing a select command; when I was trying to turn my TV off, I noticed it subtly going forward into menus and even playing things, depending on where my "cursor" was.
If you only provide AirPlay credentials, then pyatv will use "navigation" to turn off the device as there's no support in the protocol to do it any other way. If you also provide Companion credentials, the pyatv will (automatically) use a proper method of turning off the device as that protocol supports it natively. It's the same way the Shortcuts app works. You should not use suspend
and wakeup
as they are deprecated and will be removed soon.
@postlund Tks for that tip, I finally went and updated my config and it now works better than before. For folks watching, my current config for my version of homebridge-cmd-television
"oncmd": "atvremote --id 08:66:98:D4:CF:79 --airplay-credentials `cat ~/.homebridge/atv_airplay.cred` --companion-credentials `cat ~/.homebridge/atv_companion.cred` turn_on",
"offcmd": "atvremote --id 08:66:98:D4:CF:79 --airplay-credentials `cat ~/.homebridge/atv_airplay.cred` --companion-credentials `cat ~/.homebridge/atv_companion.cred` turn_off",
"pausecmd": "atvremote --id 08:66:98:D4:CF:79 --airplay-credentials `cat ~/.homebridge/atv_airplay.cred` --companion-credentials `cat ~/.homebridge/atv_companion.cred` pause",
"playcmd": "atvremote --id 08:66:98:D4:CF:79 --airplay-credentials `cat ~/.homebridge/atv_airplay.cred` --companion-credentials `cat ~/.homebridge/atv_companion.cred` play",
"powerstatecmd": "atvremote --id 08:66:98:D4:CF:79 --airplay-credentials `cat ~/.homebridge/atv_airplay.cred` --companion-credentials `cat ~/.homebridge/atv_companion.cred` power_state"
Hello all, Thanks to your different workaround, I'm now abble to get the power on / off status of my apple TV in HOME. It's work well and very reactive in. But when I try to create an automation like (when ATV on, turn on my led strip OR when ATV off, turn of my led strip) the trigger doesn't work... I have nothing in the console of homebridge.
Some idea about this trigger issue? Thanks a lot
Hello @napo-leon :)
Which one of the proposed solutions are you using?
If I understood right, @NorthernMan54 solution (NorthernMan54/homebridge-cmd-television) is not retrieving the state of the Apple TV unless you call one of the commands from the plugin. In other words, if you use the plugin via Alexa to turn the Apple TV ON, then you will get the ON status and your led strip will turn ON with the automation. But if you simply use the remote to turn on the Apple TV, nothing will trigger the plugin to get the status and your automation will not work. Please, somebody correct me if what I just wrote is wrong.
If you use the CMD4 solution as proposed by @shemege and @njuart, then you can make the CMD4 plugin to read the Apple TV state every x seconds and use that information for your automation. This is the solution that I'm using because I had the same interest as you. I have the CMD4 plugin pooling the Apple TV state every 5s and all is working fine. I created this project just to remember what needs to be done (cristian5th/homebridge-appletv)
Hello @cristian5th I'm using an hybrid solution:
SH script (on my mac mini):
if atvremote -s IPADRESS --airplay-credentials CRED power_state | grep -q PowerState.On; then echo "1" else echo "0" fi
Json config in Homebridge:
{"platform": "Cmd4", "name": "Cmd4", "accessories": [ {"type": "Switch", "name": "Apple TV", "on": true, "state_cmd": "bash ~/Documents/Script/appletv.sh", "polling": [ { "on": true, "interval": 5, "timeout": 5000}], "interval": 5, "timeout": 5000}
This one work well to get status. But not for trigger. If someone could check this config to know if the issue is due to my script or if it's an Home limitation.
Thanks a lot
Yes, your script should be good to get the status.
In order to trigger, you need another script. pytv command for trigger will be something like this:
atvremote --id ${ATV_id} --companion-credentials ${companion_credentials} turn_off
As written by @postlund earlier, it is better to use companion-credentials to send commands and airplay-credentials to get the status. Depending if you do everything in one single sh file or not, then you will need to add more switches to your HB configuration or not.
Check my project. You have everything into one single script for power and play/pause command and status.
I'm not sur to understand very well (due to my english level :) ). My only needs is to have in home a kind of switch on/off of the actual state of apple tv (refreshed every 5 second). And use this switch only to trigger another action (I never use this switch to power on off my ATV). If I want the most simple script what do you advise?
Thanks a lot for your help!!!
Your script is enough if you just want to receive the status of the Apple TV power. If it is not working, then you may need to find some syntax error. At first sight, I cannot see any.
Based on what is working for me, I would modify your script like this, but again, I don't know why your script is not working.
_Note: replace THE_REAL_ID_OF_YOUR_ATV and THE_REAL_AIRPLAY_CREDENTIALS_OF_YOURATV with your data.
appletv.sh
#!/bin/bash
ATV_POWER_STATE=$(atvremote --id THE_REAL_ID_OF_YOUR_ATV --airplay-credentials THE_REAL_AIRPLAY_CREDENTIALS_OF_YOUR_ATV power_state)
if [ "${ATV_POWER_STATE}" = "PowerState.On" ]
then
printf "1\n"
else
printf "0\n"
fi
exit 0
Json config in Homebridge
{
"platform": "Cmd4",
"name": "Cmd4",
"interval": 5,
"timeout": 4000,
"debug": false,
"stateChangeResponseTime": 3,
"queueTypes": [
{
"queue": "A",
"queueType": "WoRm"
}
],
"accessories": [
{
"type": "Switch",
"displayName": "Apple TV",
"on": "FALSE",
"queue": "A",
"polling": [
{
"characteristic": "on"
}
],
"state_cmd": "bash ~/Documents/Script/appletv.sh"
}
}
I have test tour proposal.
when I run in terminal the script sh it’s Work Well. But in home bridge the Switch stay off.
If I Switch manually on… It’s work as trigger… but stay on Just few seconds. I think the cmd4 doesnt keep the on state.
Do you have idea?
Sorry for my previous message it’s my fault (path issue) now it’s work but the triggering doesn’t work.
When I turn on my Apple TV the switch go On value but do not start my led… I dont Know why this trigger mechanism doesn’t work in Home app…
Nevertheless thanks a lot for your support
I think you had some remaining of your previous scripts that were causing trouble. Now, you write that the switch goes on when you turn on the ATV. That means it is working correctly.
If your automation is still failing, it might also be because you have created it when you were using the previous script. Try to delete the automation and create it again. I can assure you that it works perfectly. This is how mine looks like:
I have found a solution. Just delete and my homebridge in home and now it’s work!! Im so happy. Thanks a lot
I just want to share my way of solution after playing around with you guys methods for hours. I think it's the easiest way for me and it's already suit all my use case. However, I just have to pre-inform first that my method will only work with 'On/Off' and also reporting the correct 'power_state' of the Apple TV no matter if you are turning in off/on in HomeKit or your Apple TV remote. As I don't need the 'Play/Pause' state for my implementation, I didn't spend time trying on that.
I use 'Homebridge Cmdswitch2 No Logs' plugin as it's the plugin I'm currently using for my PS5.
pip install pyatv
. Note that you may need 'sudo' in order to make it work.sudo atvremote scan
. Note down the Mac Address and IP Address of the Apple TV you want to connect to.atvremote -s {YOUR-APPLE-ID-IP-ADDRESS} --protocol companion pair
. You will be asked to enter the 6 digits showing on your tv onto the terminal window. After that, take note of the credential showing on the terminal screen.Name: Name your Apple TV
On Command: atvremote -s {YOUR-APPLE-TV-IP-ADDRESS} --airplay-credentials {YourAppleTVCredentialHere} turn_on
You may need to fill in the full path of your command if this doesn't work. For example, you may need to use /home/pi/.local/bin/atvremote instead of just atvremote
Off Command: atvremote -s {YOUR-APPLE-TV-IP-ADDRESS} --airplay-credentials {YourAppleTVCredentialHere} turn_off
State Command: atvremote -s {YOUR-APPLE-TV-IP-ADDRESS} --airplay-credentials {YourAppleTVCredentialHere} power_state | grep -i 'PowerState.On'
. You may notice that I have grep -i command after the power_state command. This is to make sure that HomeKit will only show 'On' status when the power_state of the Apple TV is really 'PowerState.On' status.
Polling Interval : (The interval of seconds you want homekit to check for your Apple TV state)
Polling Timeout: 10
With this, if you or someone happen to turn the Apple TV off via the physical remote control, the status state would return 'PowerState.Off' and Homekit would then report that the Apple TV is off automatically. You can now using the Apple TV state to control your automation as you wish.
With this workaround, you don't need to create a bash script to run the commands and it should be easier for people who are looking for solution, at least it's easier for me.
I know it's a beta, just wanted to give a heads up that this doesn't work in tvOS 15