Issue ADB Command from Indigo

Posted on
Fri Nov 30, 2018 8:20 pm
johnfdl offline
Posts: 136
Joined: May 18, 2017
Location: Atlanta, GA USA

Issue ADB Command from Indigo

So I have a new Amazon Fire TV cube. It's great as it makes my TV viewing voice controlled. But there are times I want to control my Fire TV Cube via Indigo. For example, at 9 PM, my Indigo locks the doors and arms the alarm and announces the lockdown and battery level thru my Sonos Beam soundbar. But I am often watching TV at that time so I want to pause my TV before issuing the Sonos announcement.

From what I've learned, i can install Adroid Debugging (ADB) on my mac and via a terminal command from my mac connect to the Fire and pause it. I tried this on my Macbook (not where Indigo runs) and it worked just fine. So I'd like to install ADB on my Indigo Mac and then wrap this simple command in a script (either apple or python I assume), but I am not a programmer.

The terminal command looks like this:

adb connect 192.168.2.91
adb shell input keyevent KEYCODE_MEDIA_PLAY_PAUSE
adb disconnect

This toggles the pause so I'd issue the same command after the sonos announcement.

I'm guessing this is trivial to do in script, but I know nothing about python or applescript so wondering if someone can help with how to wrap these commands into a batch file of sorts that can be called via Indigo (and advise how to call it).

Thanks in advance for guidance.

Posted on
Sat Dec 01, 2018 4:43 pm
johnfdl offline
Posts: 136
Joined: May 18, 2017
Location: Atlanta, GA USA

Re: Issue ADB Command from Indigo

I was able to figure it out in applescript as follows:

tell application "Terminal"
do script "adb connect 192.168.2.91"
delay 2
do script "adb shell input keyevent KEYCODE_MEDIA_PLAY_PAUSE"
end tell

but I know applescript is not as strategic as python.....but it gets the job done. Happy to hear if there is an easy way to do this in python so I future-proof this.

Posted on
Sat Dec 01, 2018 7:05 pm
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Issue ADB Command from Indigo


I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Dec 01, 2018 8:07 pm
johnfdl offline
Posts: 136
Joined: May 18, 2017
Location: Atlanta, GA USA

Re: Issue ADB Command from Indigo

Thanks....I had seen that before I posted here. I tried it but was unsuccessful with it. Likely something simple that I am missing. I am not a coder, so it's not easy to fully understand how to take the logic defined in your link and translate it for my commands.

Posted on
Sat Dec 01, 2018 8:25 pm
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Issue ADB Command from Indigo

johnfdl wrote:
Thanks....I had seen that before I posted here. I tried it but was unsuccessful with it. Likely something simple that I am missing. I am not a coder, so it's not easy to fully understand how to take the logic defined in your link and translate it for my commands.

Post what you have and we'll work it out! We're happy to help.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Dec 02, 2018 7:55 am
johnfdl offline
Posts: 136
Joined: May 18, 2017
Location: Atlanta, GA USA

Re: Issue ADB Command from Indigo

Thanks. Here's what I tried:
Image
import os
os.system("adb connect 192.168.2.91")
os.system("adb shell input keyevent KEYCODE_MEDIA_PLAY_PAUSE")

It does not pause the FireTV as expected, and nothing in the Event Log

Posted on
Sun Dec 02, 2018 8:09 am
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Issue ADB Command from Indigo

I'm not able to test this, but it should be in the neighborhood. Please post back with your results so that we can get you going if this doesn't work out of the gate.

Code: Select all
import subprocess
from time import sleep

adb_path = "full/path/to/adb"

connect = subprocess.Popen([adb_path, 'connect', '192.168.2.91'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
indigo.server.log(unicode(connect.communicate[0]))
time.sleep(2)
key_event = subprocess.Popen([adb_path, 'shell', 'input', 'keyevent', 'KEYCODE_MEDIA_PLAY_PAUSE'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
indigo.server.log(unicode(key_event.communicate[0]))

If you find the whole thing takes longer than 10 seconds to run, try saving it to a file and running it as a linked script.

EDIT: updated to allow for full path to adb executable and to correct the capitalization of the executable name..
EDIT 2: corrects syntax errors.
Last edited by DaveL17 on Sun Dec 02, 2018 12:12 pm, edited 2 times in total.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Dec 02, 2018 8:21 am
FlyingDiver offline
Posts: 3594
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Issue ADB Command from Indigo

For either of the scripts in the last two posts, I recommend changing the 'adb' argument to include the complete path to the executable. Search paths are customized per user and shell, and using these libraries doesn't always include the same search path as you would get in a terminal window.

Also, Dave's script should not have ADB in upper case.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Dec 02, 2018 10:09 am
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Issue ADB Command from Indigo

FlyingDiver wrote:
For either of the scripts in the last two posts, I recommend changing the 'adb' argument to include the complete path to the executable. Search paths are customized per user and shell, and using these libraries doesn't always include the same search path as you would get in a terminal window.

Also, Dave's script should not have ADB in upper case.

Thanks. Edited the above post to account for both things.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Dec 02, 2018 11:05 am
johnfdl offline
Posts: 136
Joined: May 18, 2017
Location: Atlanta, GA USA

Re: Issue ADB Command from Indigo

Thanks for the code.....I would have gotten nowhere near that code by myself (and google). It produces a syntax error when I try to compile:

A couple points....in the terminal window, I actually don't type 'connect 192.168.2.91', I actually have to issue the adb prefix. So it's this "adb connect 192.168.2.91". I did adjust your syntax to include the prefix, but I get the same error. Also, the location where adb is has been added to my path (but i put in the location anyway).
Image

Posted on
Sun Dec 02, 2018 11:09 am
FlyingDiver offline
Posts: 3594
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Issue ADB Command from Indigo

Both of the lines with subprocess.Popen have an extra ')' at the end of the line.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Dec 02, 2018 12:18 pm
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Issue ADB Command from Indigo

FlyingDiver wrote:
Both of the lines with subprocess.Popen have an extra ')' at the end of the line.

I fixed that too.

The adb_path variable needs to link to the actual adb executable.
Code: Select all
path/to/executable/adb

If it's in your PATH, you should be able to just use 'adb'

I actually don't type 'connect 192.168.2.91', I actually have to issue the adb prefix. So it's this "adb connect 192.168.2.91".

I'm afraid I'm not following you. What *exactly* do you type in the terminal?

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Dec 02, 2018 12:23 pm
johnfdl offline
Posts: 136
Joined: May 18, 2017
Location: Atlanta, GA USA

Re: Issue ADB Command from Indigo

never mind, i was saying I need to type adb connect 192.168.2.91
but when I looked at your command, I didn't see the 'adb' part of it, but I think I need to add that to my path (which I've now done). I now can compile but get a permission error when it executes:

Action Group Test Python call to FireTV
Script Error embedded script: [Errno 13] Permission denied
Script Error Exception Traceback (most recent call shown last):

embedded script, line 6, at top level
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied

Posted on
Sun Dec 02, 2018 12:30 pm
DaveL17 offline
User avatar
Posts: 5102
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Issue ADB Command from Indigo

Please open an Indigo scripting shell and type your commands in by hand and post the results.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Dec 02, 2018 6:06 pm
jay (support) offline
Site Admin
User avatar
Posts: 15870
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Issue ADB Command from Indigo

Another option that might be easier is to just write a shell script to do it, then execute the shell script from the Run Shell Script action. That would remove any oddness that the subprocess call might cause.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Who is online

Users browsing this forum: No registered users and 0 guests