Skip to content

Instantly share code, notes, and snippets.

@tamsky
Last active June 8, 2021 05:45
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 tamsky/ce3f7fb8856a7819b4ee76c6163e03bd to your computer and use it in GitHub Desktop.
Save tamsky/ce3f7fb8856a7819b4ee76c6163e03bd to your computer and use it in GitHub Desktop.
# —— by default, chromecast volume is not saved across reboots — dumb.
#
# UNTESTED
#
# A python 3 script to discover all the chromecast devices and save the volume.
# Then each time you run it the script will look for any CC that has a volume of 1.0 (100%) and reset it back to the saved value.
# Safe harbor: No warranty, no guarantees of any kind.
# Licenses of supporting modules are their own licenses.
# This is only an example of what you can do not real code.
#
# pip3 install pychromecast
------------------------------------------Start of code----------------------------------------------------
import time
import datetime
import pychromecast
#object to hold all needed data about a chromcast device
class castDevice:
friendly_name="unknown"
volume_level=0.3
#array of caseDevices
castDevices=[]
f=open("pycastvolume.csv","r")
listCastDevices = f.readlines()
print(datetime.datetime.now(),"Reading ",len(listCastDevices)," devices from file.")
for line in listCastDevices:
line=line[:-1]
items=line.split(",")
cd = castDevice()
if len(items)==2:
cd.friendly_name=items[0]
cd.volume_level=float(items[1])
castDevices.append(cd)
# chromecasts = pychromecast.get_chromecasts()
stop = False
while not stop:
print(datetime.datetime.now(),"---------Loop-----------")
chromecasts = pychromecast.get_chromecasts()
for cc in chromecasts:
# wait for the current chromecast data
cc.wait(10)
strFriendlyName = cc.device.friendly_name
bolFound=False
#look for the current cc in the known castDevices array
for cd in castDevices:
if cd.friendly_name == strFriendlyName:
bolFound=True
print(datetime.datetime.now(),"---------------------")
print(datetime.datetime.now(),cd.friendly_name)
print(datetime.datetime.now(),"Current volume: ",cc.status.volume_level)
print(datetime.datetime.now(),"Saved volume : ",cd.volume_level)
break
if not bolFound:
#this is a new device, need to save it
cd=castDevice()
cd.friendly_name=cc.device.friendly_name
cd.volume_level=cc.status.volume_level
if cd.volume_level == 1.0:
#max volume found, reset to default value
cd.volume_level = 0.3
print(datetime.datetime.now(),"Set ",cd.friendly_name," to default value: ",cd.volume_level)
print(datetime.datetime.now(),"Added new device",cd.friendly_name, " volume: ",cd.volume_level)
castDevices.append(cd)
# update volume if different
if cd.volume_level - cc.status.volume_level != 0 and cc.status.volume_level != 1.0:
cd.volume_level = cc.status.volume_level
print(datetime.datetime.now(),"Saved volume updated to: ",cd.volume_level)
#Check to see if volume is maxed out and reset it to saved value
if cc.status.volume_level == 1.0:
cc.set_volume(cd.volume_level)
print(datetime.datetime.now(),"Volume reset to saved value")
time.sleep(30)
cc.disconnect
del chromecasts
stop = True
# persist to file "pycastvolume.csv"
f = open("pycastvolume.csv","w")
print(datetime.datetime.now(),"Saving ",len(castDevices)," devices to file.")
for cd in castDevices:
f.write(str(cd.friendly_name)+","+str(cd.volume_level)+"\n")
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment