Open Android Tv app as an action for home assistant automation

Good day
I am just getting started with Home assistant, so fairly new to everything.
I would like to set up automation such that the trigger will open an app on Android tv.
I am using Hass.io installed as docker on Ubuntu.
I also have Tasker installed on my Shield tv.
I d not want to use any cloud service and want to run everything local.
I found YouTube tutorial on how to use tasker to trigger something on home assistant but didn’t find anything the other way around.
I would appreciate any pointers or guides that would help.

Thanks for looking.

You can use adb tools from another system to directly control the shield.

import datetime
import pytz
import json
import os.path
import random
import re
import string
import sys
import os
import subprocess
import time
import getopt
#import kodi

ipaddress = "192.168.2.40"

def main(argv):
        application = ''
        restart = ''
        channel = ''

        try:
                opts, args = getopt.getopt(argv,"a:r:c:",["application=","restart=","channel="])
        except getopt.GetoptError:
                print 'netflix.py -a <application> -r <restart> -c <channel>'
                sys.exit(2)

        for opt, arg in opts:
                if opt == '-h':
                        print 'netflix.py -a <application> -r <restart> -c <channel>'
                        sys.exit()
                elif opt in ("-a", "--application"):
                        application = arg
                elif opt in ("-r", "--restart"):
                        restart = arg
                elif opt in ("-c", "--channel"):
                        channel = arg


        if application == 'com.amazon.amazonvideo.livingroom.nvidia':
                output = 1
                count = 0
                while (output != 0 and count < 10):
                        output = subprocess.call("sudo adb connect " + ipaddress, shell=True)
                        if output == 0:
                                output = subprocess.call("sudo adb shell monkey -p com.amazon.amazonvideo.livingroom.nvidia  -c android.intent.category.LEANBACK_LAUNCHER 1",  shell=True)
                        if output == 0:
                                output = subprocess.call("sudo adb disconnect", shell=True)
                        time.sleep(3)
                        count += 1
        elif application != '':
                print application
                sys.stdout.flush()
                SendADB(application, restart, channel)

def SendADB(app, restart=0, channel=""):
        output = 1
        count = 0
        while (output != 0 and count < 10):
                output = subprocess.call("sudo adb connect " + ipaddress, shell=True)
                if (output == 0 and restart == 1):
                        subprocess.call("sudo adb shell am force-stop " + app, shell=True)
                if output == 0:
                        output = subprocess.call("sudo adb shell monkey -p " + app + " -c android.intent.category.LAUNCHER 1",  shell=True)
                if (output == 0 and channel != ""):
                        subprocess.call("sudo adb shell input text '" + channel + "'", shell=True)
                if output == 0:
                        output = subprocess.call("sudo adb disconnect", shell=True)
                time.sleep(3)
                count += 1



if __name__ == "__main__":
   main(sys.argv[1:])

I call it like this for a live channel

python shield.py -a com.google.android.tv -r 0 -c 5.1

or like this for an app

python shield.py -a com.netflix.ninja -r 0

There are a couple of special cases in the script for apps that don’t use the default intents to launch.

In HA, just use command line switches to call the script.

2 Likes

Thanks for the guide.
Can I install adb tools via ssh addon in hass.io?
Or will I have to install it on host machine and link it to hass somehow?

I have it installed on a totally separate machine as I am running HA in a docker container and didn’t want to pollute that system or container. I call it over ssh. I’m not familiar with hass.io and modifying the host system underneath.

1 Like

I’m new to homeassistant but running hassio. I have a an android TV which I would love to jump to netflix etc.

Is it possible to create a hass.io docker container which has adb and which can listen to requests from homeassistant? Eg, I’d like to expose a new service in HA called send_over_adb or something which pushes a message over the bus, is picked up by the adb-enabled docker container and then sends that message to the TV.

1 Like

Check the skill and android tv app aberto sonorus. It’s an alexa skill for android TV. Launching apps, pausing media, volume etc.

1 Like

@wiphye worked it out. Need to allow adb debugging in the Shield. Thing is the developer console is not readily available. I found out you need to click 10 times in about device or similar…

thank you