Skip to content

Instantly share code, notes, and snippets.

@wrybread
Last active October 2, 2023 01:14
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 wrybread/780b67f9859891e9816b649e562ad10a to your computer and use it in GitHub Desktop.
Save wrybread/780b67f9859891e9816b649e562ad10a to your computer and use it in GitHub Desktop.
Keeps a Chromecast from going into standby
#!/usr/bin/python
'''
Sends an ADB command to a Chromecast (or whatever) to keep it from going
into standby after 4 hours. Currently does that by simulating a
screen touch every 5 minutes with the command "input tap -20 0". That
simulates a screen touch off screen so it doesn't affect apps (if on screen
YouTube will get affected for example)
Install adb_shell with pip install adb-shell
I actually used the version with USB support, but I don't think that's
needed. That installs with pip install adb-shell[usb]
Need to set the path to the adbkey_fname below (search for "adbkey_fname")
You'll also need to set the ip address of your chromecast (and enable
developer mode on the Chromecast and approve the connection the first
time).
To do: test cycling power on the Chromecast while loop is running, might need to
reconnect.
-wrybread@gmail.com, 10/1/2023
'''
import os, sys, time
from datetime import datetime
# pip install adb-shell[usb]
from adb_shell.adb_device import AdbDeviceTcp, AdbDeviceUsb
from adb_shell.auth.sign_pythonrsa import PythonRSASigner
from adb_shell.auth.keygen import keygen
# Where would you like to store your ADB key?
adbkey_fname = "c:\\temp\\adb_key"
# What's the ip address of your Chromecast or whatever device?
ip_address = "192.168.1.104"
# Create the public and private keys if they don't exist
if not os.path.exists(adbkey_fname):
try: os.makedirs(os.path.dirname(adbkey_fname))
except: pass
print ("Creating the key...")
keygen(adbkey_fname)
# Load the public and private keys
with open(adbkey_fname) as f:
priv = f.read()
with open(adbkey_fname + '.pub') as f:
pub = f.read()
signer = PythonRSASigner(pub, priv)
# Connect
while True:
try:
print ("Connecting to %s" % ip_address)
device1 = AdbDeviceTcp(ip_address, 5555, default_transport_timeout_s=9.)
device1.connect(rsa_keys=[signer], auth_timeout_s=0.1)
print ("Success!")
break
except Exception as e:
print ("Couldn't connect: %s" % e)
time.sleep(1)
# Simulate a screen touch off screen in a loop
# To do: test cycling power on Chromecast while loop is running, might need to reconnect
command = "input tap -20 0"
while True:
dt = datetime.now()
timestamp = dt.strftime('%Y-%m-%d %H:%M:%S')
print ("%s: Running ADB command '%s'" % (timestamp, command) )
response = device1.shell(command)
#print (response)
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment