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

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

my firetv/kodi command/notification implementation in jsr223




This seems to be working for me for the most part, however, I am trying to setup a rule to automatically show my ring doorbell feed on my firetv when the doorbell is pressed.

So far, I figured that com.amazon.cardinal seems to be the process used on the firetv but not sure how to launch this with my ring feed automatically. Any ideas?

Perfect - thanks

Written a script that does keycodes and app launching combining concepts from posts 5

and 16
Control your Amazon Fire TV (or every Android Device) from openHAB - #16 by patrikpatrik2.

FireTV_ADB.sh:

#!/bin/bash

# Concept From: https://community.openhab.org/t/control-your-amazon-fire-tv-or-every-android-device-from-openhab/17259/5
# and https://community.openhab.org/t/control-your-amazon-fire-tv-or-every-android-device-from-openhab/17259/16
# Can be called like so with any keycode you fancy, multiple keycodes work too:
# executeCommandLine("/etc/openhab2/scripts/FireTV_ADB.sh 85") //85=code for play/pause
# and to start app:
# executeCommandLine("/etc/openhab2/scripts/FireTV_ADB.sh com.simplona.vpn.proxy.server.fire.tablets.tv 1"
#
# DPAD_CENTER:          23
# DPAD_DOWN:            20
# DPAD_LEFT:            21
# DPAD_RIGHT:           22
# DPAD_UP:              19
# BACK:                 4
# MENU:                 82
# MEDIA_REWIND:         89
# MEDIA_PLAY_PAUSE:     85
# MEDIA_FAST_FORWARD:   90
# MUTE:                 91
# PAIRING:              225
# HOME:                 3

device="192.168.XXX.XXX:5555"

if [[ -z $1 ]]; then
        echo "Need Arguments to Function"
else
        adb connect ${device} >/dev/null 2>&1
fi

length=${#1}
sleep 1
if [[ ${length} -lt 5 ]]; then
        adb shell input keyevent $@ >/dev/null 2>&1
elif [[ ${length} -ge 5 ]]; then
        adb shell monkey -p $1 1 >/dev/null 2>&1
fi

adb disconnect >/dev/null 2>&1

and a rule to start my vpn for example:

rule "ADB VPN Start"
    when
        Item adb_VPN received update ON
    then
        var cmnd = "com.simplona.vpn.proxy.server.fire.tablets.tv"
        executeCommandLine("/etc/openhab2/scripts/FireTV_ADB.sh " + cmnd,3000)
        Thread::sleep(3000)
// down, enter and back.
        cmnd = "20 23 4"
        executeCommandLine("/etc/openhab2/scripts/FireTV_ADB.sh " + cmnd)
        adb_VPN.postUpdate("OFF")
end

Item definition:

Switch adb_VPN          "FireTV Start VPN [%s]"                         (fireTV)

I know Thread::sleep is not recommended, but I have so few rules it makes little difference to me. I will implement a proper timer when I get time. Hope this routine is helpful for others.

1 Like

Ok, added timer to the rule now, you might need to adjust the timeout length depending on how long the app takes to startup.

rule "ADB VPN Start"
    when
        Item adb_VPN received update ON
    then
        var cmnd = "com.simplona.vpn.proxy.server.fire.tablets.tv"
        executeCommandLine("/etc/openhab2/scripts/FireTV_ADB.sh " + cmnd)
        createTimer(now.plusSeconds(4), [|
                cmnd = "20 23 4"
                executeCommandLine("/etc/openhab2/scripts/FireTV_ADB.sh " + cmnd)
                adb_VPN.postUpdate("OFF")
        ])
end

Hey all you cool cats and kittens!
This has been a very useful thread. I have installed ADB on openhabian and connected to my fire stick. I can run several commands successfully from putty. I haven’t tried writing these into a script yet but that will come soon.

The power commands won’t turn it off (or standby or whatever it’s called when the remote powers off the screen)
adb shell input keyevent KEYCODE_POWER
adb shell input keyevent 26
Like I mentioned, other commands work fine including KEYCODE_SLEEP but that just turns the screen black, doesn’t turn it off.

If I press the power button on the remote to turn off the screen manually, I can later send other commands that will bring it back on, adb shell input keyevent 3 will power up and go to the home screen. KEYCODE_POWER will not turn it on.

Any reason why only the power commands don’t work? Any log for adb I can go look at? The putty command shows nothing.

As far as I’m aware the Fire TV/sticks don’t turn on or off, they just sleep? (not 100% sure on this?)

I’m also assuming that you have the fire stick with the power button and volume controls on the remote? Aren’t these just IR from the remote to the TV?

Powering the TV back on and to the correct channel should work because of CEC over the HDMI.

Some kind of smart IR blaster would allow you to send the power commands straight to the TV such as a Harmony Hub or a Broadlink RM Pro.

True it isn’t a full power off, but I don’t need that. Under settings there is a “sleep” that turns the screen black but the TV stays fully powered on. Pressing the “power” button on the remote powers off the TV. That’s what I am looking for.

Sending a adb shell input keyevent 3 will power on the TV and go to home. Just looking for a way to do the opposite, hopefully without more hardware.
Thanks!