Native support for Android TV / Android devices

This component is used to control and get the different states of any Android Tv / Android device with Home-Assistant.
The component can be found here: androidtv.py
You can take a look at the original pull request here

Applications are:

  • Controlling the volume and media player
  • Turning a dumb TV into a smart TV, when turning on the Android box also turns on the TV
  • Get the playing state of the device (playing, paused…)
  • Start a Youtube video, a Spotify playlist, open an app… with an intent service

image

Thanks a lot to @JeffLIrion and @caffeinatedmike for their awesome help!


Services usage
While the Android device is controlled like any media player, there are 3 services used to interact with it:

  1. The action service: used to send common action like home, down button, sleep, known actions are registered here
    { "entity_id" :"media_player.mibox3", "action": "home"}

  2. The key service: used to input a key, described by its number as in here
    { "entity_id" :"media_player.mibox3", "key": "127"}

  3. The intent service: used to execute an intent, for example playing a Spotify playlist
    { "entity_id" :"media_player.mibox3", "intent": spotify:playlist:413xJ2K0PqxRHzHXVHsGbB:play"}


ADB Authentication

First enable USB Debugging in the developper options of your device.
Then for this component to work you’ll have to run an ADB server at least once, on the machine where HA is running.
This will generate authentication keys for your device, this is the only way for now:

  • On Linux, Debian, Raspbian…
    apt-get install android-tools-adb
    adb connect [device-IP]

  • On Windows/Mac
    Guide

  • If running the Raspberry Pi Docker image (alpine)
    docker exec -it [HA-container] /bin/bash
    apk --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ add android-tools
    adb connect [device-IP]


Installation
There are 2 versions of this component, each using a different approach. Either way copy the androidtv.py file in /config/custom_components/media_player/androidtv.py

  1. Client version: this is the better working version. It requires an ADB server running on the same machine as HA, which means running adb start-server and adb connect IP when the device disconnected because it was idle for too long. @Mister_Slowhand created an automation here to automatically reconnect the device, thanks to him!

  2. Standalone version, which doesn’t require another ADB server to be running, but problems in the upstream library make the connection to the device break often, making the device unavailable

You should try the second version first, because you don’t need to make sure everytime that the external ADB server is connected to the device, although with my Mi box 3 I cannot use the second version more than 10 minutes. Please report back here to tell me how it goes! :wink:


Configuration

media_player:
  - platform: androidtv
    host: 192.168.1.37
    name: MIBOX3 ANDROID TV
    apps:
      "amazon": "Amazon Premium Video"

Known working devices

There are a lot of differences between Android devices, they might behave differently and have specific state detection. Hopefully more devices can be tested and the list below can stay updated.

  • Xiaomi Mi Box 3, tested by me, working as expected (client version)
23 Likes

This is great, will this in any way be available for hass.io?

You can add support for launching apps and detecting which current app (activity) is running using adb…pyadb. I have a Sony Android TV; through bravia api you can launch apps, however you cannot detect running app. I’m working in my fork of it to add pyadb for this.

@M203 Thanks! :slight_smile: Unfortunately, no. I’ve been trying to for the past months but it seems that we hit a wall here - the real ADB server has to connect once to the Android device to generate authentication keys between the machine and the device, and it’s not possible to do that inly in Python. There’s an open issue in the upstream lib so hopefully one day someone can figure out and the component will be available in hass.io.

@ralfaro The current app and activity are already implemented and working, if the app is present in this list then its name is displayed. Then with this component you can launch apps using the intent service, but I guess I could add a service that specifically opens an app. Thanks for the suggestion!

@a1ex4 Cool, I’ll have to try out the androidtv component then. I’d love the union of features to coexist in one component for my Bravia / Android TV…of course it has to be two-headed.

Are you saying python-adb cannot solely be used?

It seems that the connection made to the device using python-adb cannot handle how quick HA sends command to the Android device, it becomes saturated and basically dead, as far as I understand.

If you have an SSH access to the HA machine then I’d recommend running the normal ADB server and installing the client version of the component, that’ll work just fine! In either case, running the server at least once is required for the two versions anyway.

2 Likes

I only have experience with using the Python adb package for the Fire TV component, not for Android TV devices, but here’s the deal.

In the Fire TV component, all ADB commands are wrapped in a function that checks if an ADB command is already running. If so, in the official component the command will be skipped. (I have a custom component that will wait until the command finishes, but this was not approved for the official component because it is blocking.) If a command fails, the ADB connection will be re-established in the next update. For me and many others, it works flawlessly. Some people do have some issues, though, and I’m not sure why.

And yes, you do have to connect first using the ADB binaries, tell your device to always trust this computer, and then copy the key files to your HA configuration. It’s all detailed in the Fire TV documentation, and if you read it thoroughly it’s really not that difficult to get it setup and working.

2 Likes

This looks great. Perfect for the mi box that’s in the mail right now! I love the adb approach.

1 Like

Thank you for this fantastic component - have been waiting for this for a long time! :slight_smile:
I am testing this on my Nvidia Shield TV.

First enable USB Debugging in the developper options of your device.

On the Shield TV, I had to enable Network Debugging

The current app and activity are already implemented and working, if the app is present in this list then its name is displayed.

I added (in my local code) Zattoo and Amazon Prime Video to the list. Perhaps you could let the user define his own set of apps in the configuration that overwrites the default one? Something like

media_player:
  - platform: androidtv
    host: 192.168.1.37
    apps:
      "kodi": "Kodi"
      "zattoo": "Zattoo"
      "amazon": "Amazon Prime Video"
    name: MIBOX3 ANDROID TV

So far everything works as expected. Will keep on testing and report back!

1 Like

So glad you like this component! I needed too, and learned so much making it :wink:

So which version did you go with, client or standalone ?

That’s a great idea! I just finished implementing it, at least in the client version, right here. I also added Zattoo and Amazon to the list. Thanks again for the suggestion and the kind words!

Great news here !
I’m using this for a while now and it works really good for my needs. As you said, with the ‘adb connect’ command issued as a script.

But I must say I don’t demand a lot to the media players, mostly because I expect a more interactive experience from it. This is a problem from HA generic component, not from your implementation for Android devices.
I’ve enhanced the Denon component so that it can control the network music player/Internet radio, but the only way I could make the arrows in the media player is to define them as entries to select. Never actually use it, it’s a pain ! :cry:
I wish it looked more like a remote control, full featured.

Anyway… So I’m using this component mostly to monitor the TV use, change the volume when I’m not in the room. Sad.

What are your plans ? Have you ideas about features to add in, or are you looking for testers ?

I run this component (client version) now for two Nvidia Shield TV devices. Since they always remain online, I have not faced any disconnects, not even when I restart the HA service. Only booting the RPi requires to “adb connect” again, but that is absolutely acceptable.

The player state updates after a few seconds (3-7 sec) which is OK for my purpose.

Services usage
While the Android device is controlled like any media player, there are 3 services used to interact with it:

You should update the documentation, the key names are “action”, “key” and “intent”, not “androidtv_action” etc. I managed to execute the services for actions and keys, but intents are still unclear to me… Could you please elaborate how to e. g. open a specific app?

@Mister_Slowhand That’s great if you’ve been using for this long, thanks again for sharing your automation! I get what you’re saying, although for the media players I use Home Assistant to extend their actions and make them interact with other items, like lights, or in this case with services.

Definitely looking for testers! Their are so many different Android box around, I hope most of them can be handled. For the future I kind of gave up trying to get rid of the initial ADB setup, I wanted the component to be available for everyone (including hass.io) and easy as possible, but I don’t have the time to dive in the official adb lib. So my focus is going to be on the ‘client’ version, and I have a couple of things I want to add, more services and states. Also create a better doc! :sweat_smile:

@cicero222 Awesome, I’m very glad this is working for you! Yep, good catch for the services, I still had them like that on my Nodered so I was confused, I’ll update the post.
Don’t hesitate if you have more ideas or suggestions :slight_smile:

EDIT: as for the intents, they are basically ‘urls’ that when opened on Android will execute an action within a specific app. For example when you click on a location and that opens Maps with the location, or when you want to add a calendar link, it will open the app and you can confirm if you want to add it.

You can look them up on Google, ‘app android intent/actions’. For Youtube simply passing the video/playlist URL will start the app and the video. For Spotify the URL is spotify:playlist:PLAYLIST-ID:play. You can do some pretty cool stuff with it, originally I created this component so I can wake up to a Spotify playlist playing on random :wink:

@a1ex4 (or anyone else interested), how about this idea…

  1. Fork the pure-python-adb repo and call it something other than adb, such as adb_pure, so that it can coexist with the python-adb package. Host this modified library on pypi.
  2. Modify the androidtv package. Specifically, add a keyword argument adb_client (and adb_device?) to the AndroidTV class such that if it is provided, it will execute its ADB commands using the “pure-python-adb” library instead of the “python-adb” library. Ideally, do it in a way such that the syntax for sending ADB commands is the same either way (e.g., self._adb.Shell(cmd)).
  3. The HA component will use the androidtv package, which will use “pure-python-adb” if the adb_client configuration entry is provided and the “python-adb” library otherwise.

And then I can copy this for the firetv package and HA component :smile:!

That’s EXACTLY what I was thinking too! As we speak I’m writing ha-androidtv, which is my first Pypi package, so I’m taking my time and learning how to do that, mostly from yours :wink:
So I was thinking about it but as I don’t know enough about Pypi packaging I decided to go one baby step at a time, but clearly this is the ideal solution :+1:

I got a basic chinese box, with Android (not Android TV) 6. N95X something.
I run Plex (local, no registration, manual connect to my server), Netflix (from local APK install…), TinyCam for the cameras.

Tell me if you want me to try anything, I don’t mind breaking its integration into HA for a while.

What I’d love to have : interact with Plex server, choose a movie, a song, an album and play it on the box ! No Plex component offers that right now.

Awesome idea. Is exactly what i’m looking for.
I will try it !

1 Like

How to run app from HA with intent service? For example how to run Netflix?

on my mibox no network debugging :frowning:
so adb connect : connexion refused :frowning:
Any help ?
EDIT : mistakes on the IP :slight_smile:

i try to add app “tv.molotov.app” but not working. :confused:

@Mister_Slowhand This Plex integration seems way too specific to be part of this component, such tool should work on any Plex instance, not only on an Android TV.

@Bieniu I’m planning on adding another service, that will be used to open apps. I’ll try to add a dropdown to the card, similar to the modes on a light, to open installed app.

@max5962 What’s your config like ? This should be:

media_player:
  - platform: androidtv
    host: 192.168.1.37
    apps:
      "molotov": "Molotov"

Right now I’m working on having the best code possible to interact with Home Assistant, adding other features will come later :wink:

1 Like