Control your Amazon Fire TV (or every Android Device) from openHAB

As I just have the Amazon FireTV as my Only Media Player in my House I got the need of controlling this device from openHAB. As it turns out, this isn’t as difficult as it seems.

You just need to enable the adb debugging feature on the FireTV and use shell scripts to send key codes.

Example

Here is one example script, which simulates a press of the Play/Pause button:

pressFireTVbutton.sh:

adb tcpip 5555
adb connect firetv-office:5555
adb shell input keyevent 85
adb disconnect

In an openHAB rule you now just need to execute this script:

executeCommandLine("/path/to/pressFireTVbutton.sh")

You can get an overview of the possible key codes on this site:

Maybe there is a better solution (maybe a virtual Player item and parameterising the shell script), but for now it works.

Cheers
regnets

11 Likes

This sounds great. But I don’t think it’s possible to switch on/wakeup the Fire TV?

You can do so many things. Like running Kodi:
adb shell am start -n org.xbmc.kodi/.Splash

EDIT: Switch on/off
adb shell input keyevent KEYCODE_POWER

3 Likes

I followed the example above but can’t get it working. My FireTV doesn’t power on.

What I have checked so far:

  • ADB debugging is enabled
  • execute permissions for the shell script granted
  • tested the shell input keyevent on my Windows computer and it work’s
adb tcpip 5555
adb connect firetv-office:5555
adb shell input keyevent KEYCODE_POWER
adb disconnect

Did I miss something?

Interesting: when I execute the shell script via Putty, the command line says that it can connect to the FireTV. But thats all, nothing happens! I’m also confused about the different behaviour when I execute these commands directly via Putty:

first command: sudo adb connect firetv-office:5555
after connect: sudo adb shell input keyevent KEYCODE_POWER

After that my FireTV responds. What the hell is going on here?

Are there any log entries in openhab.log when your rule is triggered?
When all is OK, you should see something like this:

executed commandLine '/etc/openhab2/scripts/fire-control.sh 85'

If entries like that are there, maybe i would start with with a command that is easier to track. Like start a stream and use keyevent 85 to pause the stream.
BTW here is the script “fire-control.sh” i use that can be fed parameters:

#!/bin/bash
#works without next line, dunno what it's good for
#adb tcpip 5555 
adb connect YOUR-FIRETV-IP-OR-DNSNAME-HERE:5555
adb shell input keyevent $1
adb disconnect

can be called like so with any keycode you fancy:

executeCommandLine("/etc/openhab2/scripts/fire-control.sh 85")//85=code for play/pause
2 Likes

@Castileo I have the same issue. Did you ever resolve this?

@Oli
My logs shows the script being executed:

2017-09-09 14:18:05.084 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'sudo /etc/openhab2/scripts/FTV/ftv_playpause.sh'

I have a guess. Ive been tinkering alot with android devices, and i would suspect that you need to install ADB drivers for windows to recognize the adb command. You can either get it official or the easy way by downloading adb drivers

I dont know if this is the issue but hey, it might

I’ve installed ADB in my RPI using:

sudo apt-get install android-tools-adb

I can send commands via terminal and it works, just not working with the executeCommandLine function.

1 Like

OK,

I figured this one out - just navigate to the folder of you.sh file and execute
sudo chmod +x fire-control.sh (or whatever your file name is), to grant execute rights.
My problem is that navigation is ppiiiiiiig slow :frowning:

You might also check out this project for controlling Fire TV’s. https://github.com/happyleavesaoc/python-firetv

The server component provides a rest interface for controlling as well as getting the state. The state portion seems to be broken on my version of Fire OS as it will only return “play” unless I am sitting at the home screen, then it will return “standby”.

I did that and it did not resolve it. I ran sudo visudo and added some privileges to the openhab user, rebooted and it worked!

I don’t have too many speed issues, it’s not very snappy, but very tolerable. I’m using a fireTV connected via Ethernet, not wifi. Maybe that helps. Maybe it has to do with the script making a connection each time it’s called. Maybe there’s a way to test if there is a connection and then send the command? If no connection, then connect and keep the connection.

@BulletProofFool1 try this script and see if it speeds up your commands. It checks for a connection based on the name of the firetv connection string($2) and establishes a connection if it doesn’t exist. I don’t know if there is a downside to maintaining the connection between OH and the firetv. Maybe someone else can comment on that.

#!/bin/bash
#adb tcpip 5555
#echo $1 the command
#echo $2 the connection string
con=$(adb devices | grep $2 -c -i)
#echo "con count" $con
if [ "$con" = "0" ]
then
	adb connect $2
fi
adb shell input keyevent $1

Here’s a modified version of the shell script if you have more than one firetv/adb connection:

#!/bin/bash
#echo $1 //the command/keystroke
#echo $2 //the connection string
con=$(adb devices | grep $2 -c -i)
conName=$(echo $2 | tr '[:upper:]' '[:lower:]' )
#echo "lcase:" $conName
if [ "$con" = "0" ]
then
	adb connect $2
fi
adb -s $conName shell input keyevent $1

For some reason

adb devices

is now returning the proper case of the connection name(prior ADB seemed to convert connection names to lowercase regardless of input string), so feeding the correct case for the connection name is required when sending a command.

I added a little logic to handle this:

#!/bin/bash
echo $1 //the command/keystroke
echo $2 //the connection string
con=$(sudo adb devices | grep $2 -c -i)
conName=$(echo $2 | tr ‘[:upper:]’ ‘[:lower:]’ )
echo “lcase:” $conName
if [ “$con” = “0” ]
then
echo “connecting…”
adb connect $2
fi
sudo adb -s $2 shell input keyevent $1
#er=$? //last error
if [ $? -ne “0” ]
then
echo “trying LCase connection”
sudo adb -s $conName shell input keyevent $1
fi

I tested it on windows, no problems at all. But I am running OpenHAB on a synology NAS, where adb is not available. I tried to install it with ipkg, but there is no package for adb. Does anyone run adb with openhab on a NAS?

update: managed to get it working using adb from entware-ng!

I try to open kodi in a script. It works if I execute the line
[12:10:13] openhabian@openHABianPi:~$ adb shell am start -n org.xbmc.kodi/.Splash

but when I put in a shell:
adb connect 192.168.2.34:5555
adb shell am start -n org.xbmc.kodi/.Splash
adb disconnect

I get the error
[12:09:57] openhabian@openHABianPi:~$ /etc/openhab2/scripts/StartFireTV2.sh
connected to 192.168.2.34:5555
}arting: Intent { cmp=org.xbmc.kodi/.Splash
Error type 3
} does not exist.lass {org.xbmc.kodi/org.xbmc.kodi.Splash

Does any one know what causes this error?

I found that it was possible to start what ever application by first running
adb shell pm list packages -f

and search for the app and launch it by
shell monkey -p com.spotify.tv.android 1

however, if I use a shell
adb connect 192.168.2.34:5555
adb shell monkey -p org.xbmc.kodi 1
adb disconnect

I just get an error
[18:03:12] openhabian@openHABianPi:~$ /etc/openhab2/scripts/StartFireTV2.sh
connected to 192.168.2.34:5555
** Error: Count is not a number

Why am I not sucessful in running the shells?

1 Like

works great, thank you!

My problems was cause by me using notepad as text editor. By using Notepad++ and doing
settings->preferences
new document / default directory tab
select the format as unix and close
create a new document and enter the script

the scripts could be run

Hi Frest,

can you please share how to get openhab working on entware. I am planning to install it on my mi router 3G using entware.

Thx

Very interesting post.
Originally I was looking for a way to get the currently playing video on the fire stick, but with these commands doesn’t seem to possible.

Do you know a different way to let OH know what the fire stick is currently playing?

thanks