Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active February 3, 2024 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpruss/b984115c3332e301d9ffc60a70051dcf to your computer and use it in GitHub Desktop.
Save arpruss/b984115c3332e301d9ffc60a70051dcf to your computer and use it in GitHub Desktop.
relay menu button from FireTV remote to ChromeCast home button
#!/usr/bin/python3
import time
import os
import subprocess
import sys
import shlex
TV_IP = "XXX.XXX.XXX.XXX"
CC_IP = "XXX.XXX.XXX.XXX"
# you may need to adjust the "dst" field to match your setup
LOGCAT_CMD = "adb -s "+TV_IP+" logcat -T 1 *:S HDMI:D -e '<User Control Pressed> src: [0-9]+, dst: 4, params: 0B'"
HOME_CMD = "adb -s "+CC_IP+" shell input keyevent 3"
quiet = False
def log(*args,**kwargs):
if not quiet:
print(*args,**kwargs)
def pipe(cmd):
log("pipe:",cmd)
return subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
def run(cmd,timeout=5):
p = pipe(cmd)
try:
v=p.wait(timeout=timeout)
msg=p.communicate()[0]
log("->",v,msg,end='')
return v,msg
except subprocess.TimeoutExpired:
log(" timed out")
p.kill()
return -1,""
def connect(ip):
run("adb reconnect offline")
while True:
run("adb disconnect "+ip)
log("attempting to connect to "+ip)
_,msg = run("adb connect "+ip,timeout=61)
if 'connected to' in msg:
return
log("failure")
time.sleep(10)
quiet = "q" in sys.argv[1:]
connect(CC_IP)
log("ChromeCast connected")
while True:
connect(TV_IP)
log("TV connected")
tvPipe = pipe(LOGCAT_CMD)
for line in tvPipe.stdout:
log(">"+line,end='')
if "waiting for device" in line:
log("device wants to be waited for; I won't")
tvPipe.kill()
time.sleep(10)
break
if "HDMI" not in line:
continue
r,h = run(HOME_CMD)
if r:
t = time.time()
connect(CC_IP)
if time.time() < t + 1.5:
log("retrying home")
run(HOME_CMD)
else:
log("home")
log("disconnected")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment