Open Android Tv app as an action for home assistant automation

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.