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
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:
-
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"}
-
The key service: used to input a key, described by its number as in here
{ "entity_id" :"media_player.mibox3", "key": "127"}
-
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
-
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
andadb 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! -
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!
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)