Back to Projects

I Stopped Pushing Movies to the TV

Alex Wilson••6 min read•Builder Journal
Apple TV remote on a couch arm before a glowing row of movie posters

I have a small movie app. It lives on one of my HeadstringWeb sites, it knows what we've watched, and every so often it hands us a fresh round of picks with trailers and where each one streams. It works well on a phone. The trouble is that at 7 p.m. nobody in my house is holding a phone. We're on the couch with the Apple TV remote.

So last Saturday I set out to build the obvious thing: a "Play on TV" button. Tap it in the app, and the movie starts in the back room.

It took the whole day to find out that button can't exist. Sunday went to building the thing I actually wanted.

Saturday: every door was already closed

The first door was Plex itself. Our movies live on a Plex server, and Plex used to let one app remote-control another. Phone says "play this," the Plex app on the TV plays it. I checked the current support pages before writing any code. The new Plex mobile apps cast only to Chromecast and AirPlay now. The new Apple TV and Fire TV apps dropped the "Advertise as player" setting. The server's /clients endpoint returned zero devices. That road was gone.

The second door was the Apple TV directly. There's a Python library, pyatv, that speaks Apple's Companion protocol, the same one the iPhone remote uses. My Mac Mini could pair with the Apple TV and send it commands, including "open this URL in this app."

Pairing turned into its own small project. The Apple TV shows a four-digit PIN on screen and waits. I'd read the PIN off the TV and type it into a chat with the agent on the Mini, which then typed it into the waiting pyatv prompt. The first attempts failed with PairingError: not connected. The debug log showed the Apple TV dropping the request about 30 seconds after showing the PIN, and the relay from my eyes to the prompt took longer than that. Restarting made it worse: each restart generates a new PIN, so the code I read out belonged to a dead request.

What finally worked was taking the human out of the timing. One script started the pair request and then polled the chat table every second for a four-digit message. When I typed the PIN, it landed in the prompt within a second. Ten seconds, start to finish.

Then came the deep links. Plex has unofficial plex://play and plex://preplay URLs that are supposed to open a specific movie. I sent them to the Apple TV for Monty Python and the Holy Grail. Every launch exited 0 with no output. Nothing played.

That exit code taught me the most useful thing of the day: atvremote launch_app returns 0 whether the URL does anything or not. Exit 0 proved only that the Apple TV received a message. So I stopped trusting it and watched the Plex server's /status/sessions instead, polling every three seconds. A real playback shows up there. It stayed at zero.

We kept narrowing it: Plex already open, auto-login on, then a cold start. My messages from the couch read like a lab notebook: "Ok Plex launched. No movie though." Then, on the last one: "Opens with side menus open on Home."

The new tvOS Plex app ignores everything after plex://. It opens Plex and stops. By 6:40 p.m. the Apple TV path was closed, and my plan for Sunday was the Fire TV Cube over adb.

Sunday: what I actually asked for

Sunday morning I reread what I'd said when this started: "I kinda picture just being able to see the recommendations, view the trailers, and launch from just the remote."

None of that says "a button on my phone." I had translated a remote-only request into a phone feature and then spent a day fighting the TV to make the phone feature work.

So I picked up the remote and checked what Plex already does on the Apple TV. A movie's page has the trailer. It has a "More ways to watch" row. I picked Tubi on Léon: The Professional, and Tubi opened on the movie's play page. Browse, trailer, launch, all from the remote. The only thing missing was our picks.

That turned the problem around. I didn't need to push anything to the TV. I needed to put the picks somewhere the TV already looks, and Plex has exactly that place: the Watchlist, which can sit as a row on the home screen.

The build

The Plex apps manage the Watchlist through Plex's Discover service. It isn't documented, but python-plexapi wraps the same calls, so I ran a short spike with my token first. I searched by TMDB id, added Léon, listed the Watchlist, removed Léon. All four worked, and the Watchlist went back to its original six.

The design rules were short:

  • Match by TMDB id, never by title. The app already stores a TMDB id for every movie, and Plex items carry tmdb:// guids.
  • Only remove what we added. A sync table records every item the app put on the Watchlist. Anything added by hand in Plex is never touched.
  • Fail soft. No token, or Plex is down, and the movie app carries on. The sync runs after the action returns, so it never slows a tap.

A new round of picks goes on the Watchlist. Logging a movie as watched or dismissing it takes it off. A pick nobody acted on stays until someone does.

The first full sync put 44 movies on Plex and reported one no_match. That one was the interesting part. Our saved "Primer" turned out to be a 1978 film, not the 2004 one. My TMDB lookup took the top search result, and TMDB ranks by popularity, so a short title loses to a famous longer one. An audit found two more: "Society" had resolved to Dead Poets Society, and "Boy" to Astro Boy. The lookup now prefers an exact title match within the release year, and those three rows are fixed. Matching by id was only ever as good as the id going in.

Then I looked at that row on the TV, and the scope shrank. The saved list is where we dump things we might watch someday. The picks on the main page are what we actually want to watch next. So Plex now gets only the main page's picks. One re-sync took the 37 saved movies back off, and the Watchlist row on the TV went down to seven picks plus three I'd added myself.

What I'm keeping from this

The Saturday work wasn't wasted. The PIN relay pattern (put the waiting on the machine, not on the person) will come up again.

But the real fix was a shorter path. I spent a day trying to make the TV take orders from my phone. The Sunday version doesn't ask the TV to do anything new. It puts the data where the TV's own software was already designed to look, and the remote does the rest.

Share: