postlund / pyatv

A client library for Apple TV and AirPlay devices
https://pyatv.dev
MIT License
891 stars 100 forks source link

Reboot via API? #176

Open mschwartz opened 5 years ago

mschwartz commented 5 years ago

Hello and thanks for this code...

I have a harmony remote, and when it is programmed to control my apple tv, there is a "reboot" button I can press that remotely reboots the ATV. Seems to me the protocol is there, somehow.

I tried going through all the USB HID values here: https://github.com/Daij-Djan/DDHidLib/blob/master/usb_hid_usages.txt

No luck.

I actually do use the reboot function on the remote from time to time. Sometimes iTunes library from shared computer gets out of sync and whacky and the fix is the reboot. Sometimes apps are buggy and the fix is a reboot. The apps generally get fixed with updates at some point, but still need the workaround.

Thanks for reading.

postlund commented 5 years ago

The remote is IR, right?

mschwartz commented 5 years ago

The remote is IR, RF, and the hub is on WiFi.

I assume it is doing WiFi to the ATV.

postlund commented 5 years ago

I would be very surprised if it did. Did you do the pairing process when setting up the harmony (I.e. entering a 4 digit code)?

mschwartz commented 5 years ago

No, but there are no IR blasters set up, near the ATV.

The API is reverse engineered. I expect Logitech might have had better access?

postlund commented 5 years ago

This seems magical to me. Would it be possible for you to obtain network traffic dump with tcpdump or wireshark?

mschwartz commented 5 years ago

https://www.cnet.com/how-to/how-to-force-restart-the-new-apple-tv/

Maybe that helps?

mschwartz commented 5 years ago

While I have your attention, the node js Apple TV module doesn’t perform as well as pyatv, but they do return a lot more interesting information. For example, currently playing app, title, artist, duration, elapsed time, etc. The data are obviously there in the stream from the ATV.

It would also be awesome to have an MQTT bridge using pyatv. When you receive information from the ATV, post an MQTT message (retained). When you receive messages over MQTT, you control the ATV.

postlund commented 5 years ago

I have a proxy that can intercept traffic and decrypt it, it might be possible to use it with the real remote to figure out what is sent. I'm thinking of the "hold down menu and home button"-scenario. Can look into that.

pyatv will support everything the node library does (and maybe more). Not all base functionality is in place yet, so that's why currently you see less info.

Yeah, that would be cool. It would be a separate application built using pyatv though, not part of the core library. A simple bridge would probably not be long and could be bundled as an example even.

mschwartz commented 5 years ago

There's a nodejs module that wraps pyatv.

The only thing keeping me from using pyatv is the lack of the extra useful information.

Also, I note with the nodejs atv module (not the wrapper one), when the ATV actually shuts down (after the 1 hour or whatever timeout), it sends a protocol buffer message for disconnect. The repo with the protocol buffer definition files doesn't have one for it, and the nodejs module doesn't handle it either.

So I basically exit the ATV MQTT bridge I wrote and the forever command line program restarts it. I also have to fork() one process per ATV because there's about a 5 second startup cost for scanning the bonjour devices, and a lot of the time, one of the ATVs isn't found.

postlund commented 4 years ago

To get back to topic a bit late... But I believe I read somewhere that Harmony uses Bluetooth to trigger shutdown. Maybe that is how it works. Not sure if it helps (maybe if the traffic can be intercepted, in case they use some protocol that has an analog implementation in MRP, like HID).

mschwartz commented 3 years ago

I successfully made a pyatv <-> MQTT bridge. It's not general purpose enough, but it is 99% of what anyone might need.

https://github.com/mschwartz/pyatv-microservice

I will be moving it to:

https://github.com/RoboDomo/pyatv-microservice

Runs in docker.

postlund commented 2 years ago

I believe they use the reboot method described here:

https://support.apple.com/en-us/HT201836

The same thing does not seem to work over any of the IP based protocols though.