How to control `Android TV` from Home Assistant

How to control Android TV from Home Assistant.

I have Android 6. TV: Xiaomi MiTV 4A.
It took me a long time to achieve all my dreams.

Today I figured out how to call up the main menu of the TV, where you can switch inputs, enable ADB Debug, and control the equalizer.

Today I finally found how to call this menu via ADB:
adb shell input keyevent --longpress 82

If you worked with ADB from the console, then do adb disconnect (otherwise Home Assistant will not be able to connect to the TV).

Then I installed the integration Link to Integrations: add integration – My Home Assistant in Home Assistant .

Then at http://homass_ip:8123/developer-tools/service you can try to see if the function of long pressing the menu button works:

service: androidtv.adb_command
target:
  entity_id: media_player.android_tv_10_2_90_12
data:
  command: input keyevent --longpress 82

Если работает, то эту команду можно встроить сразу в lovelace dashboard card:

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: androidtv.adb_command
  target:
    entity_id: media_player.android_tv_10_2_90_12
  data:
    command: input keyevent --longpress 82
icon: mdi:menu
name: SuperMenu

TV Xiaomi MiTV 4A has port 6095 open, but only if the commands are correct.

By going to http://mitv_ip:6095/controller?action=keyevent&keycode=power the power management window will open.
This is the only useful command. I didn’t like the rest. I don’t remember why.

I integrated it into Home Assistant like this:
In configuration.yaml:

rest_command:
  mitv_power:
    url: "http://10.2.90.12:6095/controller?action=keyevent&keycode=power"
    method: get

After rebooting Home Assistant, you can call the Power menu from the lovelace dashboard card like this:

show_name: true
show_icon: true
type: button
icon: mdi:power
tap_action:
  action: call-service
  service: rest_command.mitv_power
  service_data: {}
  target: {}

Sending commands to other remote control buttons:

The most useful program is GitHub - ilker-aktuna/androidTV_keyboard_withRestAPI: This is a modified Android TV keyboard with a REST API which can receive certain commands from network

Installing a keyboard in Android. And it opens port 5000, to which you can send remote control commands.
I integrated it into Home Assistant like this:
In configuration.yaml:

rest_command:
  # port 5000 virtual keyboard: https://github.com/ilker-aktuna/androidTV_keyboard_withRestAPI
  mitv_up:
    url: "http://10.2.90.12:5000/up"
    method: get
  mitv_down:
    url: "http://10.2.90.12:5000/down"
    method: get
  mitv_left:
    url: "http://10.2.90.12:5000/left"
    method: get
  mitv_right:
    url: "http://10.2.90.12:5000/right"
    method: get
  mitv_enter:
    url: "http://10.2.90.12:5000/center"
    method: get
  mitv_home:
    url: "http://10.2.90.12:5000/home"
    method: get
  mitv_back:
    url: "http://10.2.90.12:5000/back"
    method: get
  mitv_volumeup:
    url: "http://10.2.90.12:5000/volumeup"
    method: get
  mitv_volumedown:
    url: "http://10.2.90.12:5000/volumedown"
    method: get
  mitv_volume_mute:
    url: "http://10.2.90.12:5000/keycode=KEYCODE_VOLUME_MUTE"
    method: get
  mitv_playpause:
    url: "http://10.2.90.12:5000/keycode=KEYCODE_MEDIA_PAUSE"
    method: get
  mitv_channel_up:
    url: "http://10.2.90.12:5000/keycode=KEYCODE_CHANNEL_UP"
    method: get
  mitv_channel_down:
    url: "http://10.2.90.12:5000/keycode=KEYCODE_CHANNEL_DOWN"
    method: get

After rebooting Home Assistant, you can press the UP button from the lovelace dashboard like this:


show_name: true
show_icon: true
type: button
icon: mdi:power
tap_action:
  action: call-service
  service: rest_command.mitv_up
  service_data: {}
  target: {}

I implemented the missing part via Tasker on Android.
The TV connects via MQTT to the Home Assistant broker and this is how communication occurs.
It would be desirable if there was a broker on the TV and Home Assistant would connect to the TV, and not vice versa. But the broker is not installing on my TV.
TV in MQTT sends which application is currently running.
Home Assistant sends a request to launch a specific application and Tasker processes it.
Volume control works faster and richer through MQTT.

To activate one of the HDMI inputs, I installed special applications.
And via MQTT I send a command to launch the application HDMI1, HDMI2, HDMI3.

Plus, I implemented TV Ping via MQTT.
In Home Assistant, I made an automation that if there is no ping for more than 10 seconds, it means the TV is completely turned off.

Not implemented:
I would like to turn on the TV when it is deeply turned off.
It seems possible to send commands via Bluetooth BLE via the ESP32 ESPHome project.
I’ll figure it out little by little…
If anyone succeeded, tell me how to implement it.

Both of these are very useful for android TV…maybe they can help you out.

and

2 Likes