I have a Xiaomi Mi Box 4K with Android TV. Unfortunately, there is no way in HA to integrate with Android TV devices. I wanted to be able to run the application on the device straight from HA and receive information what the application is displayed on the screen.
I used Tasker and the MQTT Client for integration. You need to download the applications on your phone and save them as apk files, which you can install on your Android TV device using a network or pendrive. The mouse will be necessary to configure the application (I used the Bluetooth mouse).
I assume you have configured the MQTT broker and you know the basics of Tasker.
After installing MQTT Client, configure it according to the screenshots below.
Adding MQTT broker (I use Mosquitto running on my Raspberry Pi):
After connecting with MQTT broker send a message to check if everything works. I use the topic homeassistant/androidtv/run
with the application name as a message to run the application.
Now we go to Tasker and create a profile that run applications:
This profile use event Message Arrived from plugin MQTT Client:
appname and action will be local variables, appname will contain MQTT message, action - MQTT topic.
Task for this profile (Run Netflix if %action
contains run
and %appname
contains Netflix
):
Now we create a profile sending to HA the name of the application that is on the screen.
The profile will be activated when the event Variable Set for the %WIN
variable occurs. Variable %WIN
is standard Tasker variable with name of the displaying screen/application. You have to enable accessibility service for Tasker (in system settings) so it can collect data about the currently displayed screen/application.
Task for this profile use Publish MQTT Message action from plugin MQTT Client:
The messaage is variable %WIN
and I use topic homeassistant/sensor/androidtv-activity
:
Now You have to add mqtt sensor to your HA configuration:
- platform: mqtt
name: 'AndroidTV Activity'
state_topic: 'homeassistant/sensor/androidtv-activity'
To run application on Android TV from HA you have to use mqtt.publish
service:
- service: mqtt.publish
data:
topic: 'homeassistant/androidtv/run'
payload: 'Netflix'
If you want to use the discovery for the sensor you need to create a profile with the event Device Boot, which will send message {"name": "AndroidTV Activity"}
in the topic homeassistant/sensor/androidtv-activity/config
with enabled option Retained
and change topic for sensor state to homeassistant/sensor/androidtv-activity/state
with enabled option Retained
. This example use discovery_prefix: homeassistant
.