Binding for Apple-TV

OK, got Python3.5 installed via https://www.cyberciti.biz/faq/debian-ubuntu-linux-python-h-file-not-found-error-solution/ and
sudo apt-get install python3.5-dev

Then pyatv could be installed with
sudo /usr/bin/python -m pip install pyatv

So upstart now is:

2019-04-02 12:43:51.928 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpyLib=lib/jpy/lib.synology-x86-64-3.5
2019-04-02 12:43:51.934 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.pythonExecutable: /usr/bin/python3
2019-04-02 12:43:51.938 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.pythonLib: /usr/lib/x86_64-linux-gnu/libpython3.5m.so
2019-04-02 12:43:51.946 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.jpyLib: /var/lib/openhab2/tmp/ohlib-8371605431178960950/jpy.so
2019-04-02 12:43:51.950 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.jdlLib: /var/lib/openhab2/tmp/ohlib-8371605431178960950/jdl.so
2019-04-02 12:43:51.964 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.pythonPrefix: /usr
2019-04-02 12:43:57.098 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpyLib=lib/jpy/lib.synology-x86-64-3.5
2019-04-02 12:43:57.108 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.pythonExecutable: /usr/bin/python3
2019-04-02 12:43:57.118 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.pythonLib: /usr/lib/x86_64-linux-gnu/libpython3.5m.so
2019-04-02 12:43:57.123 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.jpyLib: /var/lib/openhab2/tmp/ohlib-5864833516873744518/jpy.so
2019-04-02 12:43:57.124 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.jdlLib: /var/lib/openhab2/tmp/ohlib-5864833516873744518/jdl.so
2019-04-02 12:43:57.129 [DEBUG] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: jpy.pythonPrefix: /usr
2019-04-02 12:43:58.415 [ERROR] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: Unable to start Python (jpy): Error in Python interpreter:
2019-04-02 12:43:58.417 [ERROR] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: Unable to start Python (jpy): Error in Python interpreter:

And

root@robin:/var/lib/openhab2/etc# tail -f /var/log/openhab2/openhab.log | grep appletv

2019-04-02 12:53:25.530 [DEBUG] [org.openhab.binding.appletv         ] - BundleEvent [unknown:512] - org.openhab.binding.appletv
2019-04-02 12:53:25.547 [DEBUG] [org.openhab.binding.appletv         ] - ServiceEvent REGISTERED - {org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory}={service.id=154, service.bundleid=255, service.scope=bundle, component.name=org.openhab.binding.appletv.internal.AppleTVHandlerFactory, component.id=63} - org.openhab.binding.appletv
2019-04-02 12:53:27.600 [DEBUG] [org.openhab.binding.appletv         ] - BundleEvent STARTING - org.openhab.binding.appletv
2019-04-02 12:53:27.603 [DEBUG] [org.openhab.binding.appletv         ] - BundleEvent STARTED - org.openhab.binding.appletv

Sending Top_Menu:

2019-04-02 12:54:53.325 [INFO ] [letv.internal.handler.AppleTVHandler] - Apple-TV.Handler: Send command(s): top_menu
2019-04-02 12:54:53.327 [INFO ] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: Sending command top_menu to ip 10.0.0.157, lid *************************************
2019-04-02 12:54:53.331 [ERROR] [pletv.internal.AppleTVHandlerFactory] - Apple-TV.PyATV: Exception on PyATV call: null (class java.lang.NullPointerException)
2019-04-02 12:54:53.333 [INFO ] [letv.internal.handler.AppleTVHandler] - Apple-TV.Handler: SendKey failed!

DAH - Sigh…Fix one issue…next is awaiting around the corner

root@robin:/usr/bin# atvremote --address 10.0.0.157 --login_id ***************** top_menu
Traceback (most recent call last):
  File "/usr/local/bin/atvremote", line 7, in <module>
    from pyatv.__main__ import main
  File "/usr/local/lib/python3.5/dist-packages/pyatv/__init__.py", line 12, in <module>
    from pyatv.pairing import PairingHandler
  File "/usr/local/lib/python3.5/dist-packages/pyatv/pairing.py", line 14, in <module>
    import netifaces
ImportError: No module named 'netifaces'
root@robin:/usr/bin#

Try to fix:

root@robin:/usr/bin# sudo pip3.5 install netifaces
Collecting netifaces
  Downloading https://files.pythonhosted.org/packages/6f/66/2df3d5cb0e18eccf0f798f7a31601678fc3578ce560fa5b897d791e3f835/netifaces-0.10.9-cp35-cp35m-manylinux1_x86_64.whl
Installing collected packages: netifaces
Successfully installed netifaces-0.10.9
root@robin:/usr/bin# find /usr -name netifaces*
/usr/local/lib/python3.5/site-packages/netifaces-0.10.9.dist-info
/usr/local/lib/python3.5/site-packages/netifaces.cpython-35m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gnu.so
/usr/lib/python3/dist-packages/netifaces-0.10.4.egg-info

Still fails though…

And fix:

root@robin:/usr/bin# cp -R /usr/local/lib/python3.5/site-packages/netifaces* /usr/local/lib/python3.5/dist-packages/
root@robin:/usr/bin# atvremote --address 10.0.0.157 --login_id ******************** top_menu

root@robin:/usr/bin#

Now Openhab2 starts with no errors and I can send top_menu to the Apple TV4. Otherwise, nothing is shown in Paper UI, but:

b np@robin:~$ atvremote --address 10.0.0.157 --login_id ******************* playing
Media type: Unknown
Play state: Playing
Title: Arpegiateur (Arpegiator)
Artist: Jean-Michel Jarre
Album: Les concerts en Chine
Genre: Elektronisk
Position: 386/412s (93.7%)
Repeat: Off
Shuffle: False
bnp@robin:~$

Now, Paper UI looks like this (No data - not sure if they are supposed to be there?):

Hi Norman,

thanks for the info. Now could add Ubuntu 18 to the list of supported platforms

I’m wondering why you need to copy site-packages to dist-packages, because both will be added to the Python library path before starting Python

            cleanedExtraPaths.add("/usr/local/lib/python3.5/dist-packages");
            cleanedExtraPaths.add("/usr/local/lib/python3.5/site-packages");

so it should make no difference in which directory netifaces is located.

For now the alpha release is able to send keys. The status information and events are not yet implemented. I already add the channel definitions, but “run out of spare time” at that point. It’s on the list, but I’m pretty busy at the moment. A list of valid keys is included in the README.

1 Like

My problem was running “atvremote --address 10.0.0.157 --login_id ***************** top_menu” from the commandline - aka. native pyatv, that failed - I dont know if running atvremote through the binding will use the added paths(?)

In general, all my problems was python/OS related, not the binding itself…

that’s good new, because preparing the runtime setup is a little bit trick. So far the binding brings it’s own jpy module for various platforms, because it seems that OH uses them internally and it is not allowed to load the binary twice. So by having them with different names in a different path allows me using a private copy. However that’s a little bit stupid to have them installed twice.

Yes, the binding adds those load paths before starting Python, so it’s not the same when you run Python from the command line. AFAIK is there also a environment variable you can set to additional path for applications running from command line (can’t remember the name of the var)

Please always make sure to explicitly run python3.5 command. Otherwise you depend on the default Python version when using the python command without specifying the version. Same with pip: use pip3.5 instead of pip command.

Did you tried sending commands with the binding. This should work if you have the correct id in the binding configuration.

Please note: The alpha release creates a new folder with new copies of the modules each time you restart OH. This is one of the first things I go to fix with the next release (not sure if I have time to work on the binding before end of April).

fyi: I’m making progress

new features:

  • you could send more than one command, e.g. “top_menu right right”
  • automatic device discovery!

Now I try to capture the events and redirect them to the binding, but this will be a challenge

1 Like

More progress, I’m getting the status information - like event, but I need to poll them.

1 Like

Nice work!
Very happy to see this progress.

ok, here is beta 1:
https://raw.github.com/markus7017/org.openhab.binding.appletv/blob/beta1/target/org.openhab.binding.appletv-2.5.0-SNAPSHOT.jar

  • Automatic device discovery (background and from the Inbox)
  • Support for multiple Apple TVs (needs verification)
  • Periodic play status updates
  • Set position, shuffle and repeat mode
  • Relative positions (+10, -10, -30:00 etc.)
  • Special Keys movie,music,tvshows will be mapped to key sequences to select the media category from the main menu
  • Media type decoding in the embedded pyatv fixed (not with atvremote)
  • Prepared for binding configuration and pairing, but not yet implemented.
  • README updated
    and more.

Could someone please check if installed on 2.4 you still have the item in Paper UI’s Control section? Works fine with my 2.5 dev setup, but doesn’t shows up on my 2.4 prod setup.

Important: Existing things have to be deleted. You should shutdown openHAB before updating the jar, maybe you need to clear the cache.

Check out the README for details.

@JohnnyBravo @Normann_P_Nielsen @jacksteraz @Jorn_Reimerdes @crxporter @ei_Gelb_Geek @Heiko_Fanieng @HFM @Rickytr @ciscomike Feedback welcome.

2 Likes

Hi Markus
i am getting 404 error for this page

Strange, it works for me. You could also download the jar from

Both links give me 404:

https://github.com/markus7017/org.openhab.binding.appletv/tree/beta1/target
https://raw.github.com/markus7017/org.openhab.binding.appletv/blob/beta1/target/org.openhab.binding.appletv-2.5.0-SNAPSHOT.jar

try:
https://github.com/markus7017/org.openhab.binding.appletv/raw/master/target/org.openhab.binding.appletv-2.5.0-SNAPSHOT.jar

has anyone already installed the binding on 2.4?

Yes. I installed it on my RP3 with 2.4.

I was able get it set up on my gen 3 ATV. I was able to get artist and song information while playing some music. It doesn’t automatically fetch the album art, but I think that might be in a future release.

I hope to have more time later this week to test it out and to set it up on my ATV4.

I initially had trouble downloading the file. I changed the first part of the url from raw.github.com to gibhub.com and it worked. I suspect it has something to do with either my Windows 8 OS or the chrome browser, because on my ipad, it looked like it was available to download the original link.

When I cleared my cache, I lost access to paper UI. But I found a post in the forums to fix this.

Thanks, got it!

I have the 2.5 binding on OH2.4 - works ok it seems (have only glanced). I see some play info:

Apple TV 4


yes, album art is on the list. However the ay pyATV works is

  • connect to the ATV
  • fetch status and extract the url
  • close connection to ATV

Copy&paste the retrieved into the browser doesn’t work. It has a session id, so I assume that it’s only valid with that session. I need to dig into that some more, but first want to integrate pairing.

good to hear

Some wierdness, for the last 2 days my bedroom TV as been turned on by the ATV several times (until I take the big swich). Started after upgrading… it does not happen at daytime, but starts around 22/23 at night…