Google Cast Audio(Chromecast) control

Hi guys, I just wanna share my script to get status and control Chromecast audio devices. It is based on python library pychromecast.

from time import sleep
import socket
import pychromecast
import sys

sys.stderr = open('/var/log/chromecast.log', 'a')

bedroom = pychromecast.get_chromecast(friendly_name="Bedroom")
kitchen = pychromecast.get_chromecast(friendly_name="Kitchen")
bath = pychromecast.get_chromecast(friendly_name="Bath")


print("Chromcasts connected")

def device( name ):
     if name == "Bedroom":
         return bedroom
     elif name == "Kitchen":
         return kitchen
     elif name == "Bath":
         return bath
         print("Unknown name of device: \"", name, "\"")
         return False

def get_volume( name ):
     vl = round(device(name).status.volume_level,2) * 100
     return str(int(vl))

def set_volume( name, level ):
    vl = int(level) / 100
    return get_volume( name )

def status( name ):
     session_id = device(name).status.session_id
     if session_id == None:
         return "OFF"
         return "ON"

host = ''
port = 12345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))

while True:
    conn, addr = sock.accept()
    while True:
        data = conn.recv(1024).decode('ascii')

        if not data:
        print("Recived string: \"", data, "\" from ", addr)
        data = data.split(" ")

        if data[1] == "get_volume":
        elif data[1] == "set_volume":
        elif data[1] == "status":
            print("Unknown command")

You can add next line to rc.local to start it at boot time:

( sleep 3; sudo -u root python3 /scripts/ > /var/log/chromecast.log )&

Items should looks like this:

Switch  CastKitchenStatus 	"Cast Kitchen [MAP(]"	<chromecast>	(Cast,Kitchen)	{ exec="<[/scripts/ Kitchen status:60000:]" }
Dimmer 	CastKitchenVolume 	"Cast Kitchen Vol [%.1f %%]"			<volume>	(Cast,Kitchen)	{ exec="<[/scripts/ Kitchen get_volume:60000:] >[*:/scripts/ Kitchen set_volume %2$s]" }

echo -n "$@" | nc 12345

I’m using it for about two weeks and it is pretty stable.
Also, i’m going to add option to send link to chromecast device. It will make possible to play some notifications, alarms, etc.


I’ve been using the script this guy has posted on reddit:

It’s been pretty good for me and supports message queues etc.

The only thing I can’t figure out is how to detect and a device stops casting :confused:

1 Like

Has anyone got this working with TTS? That would be amazing…

TBH, all you need is TTS-> WAV then push that to chromecast as a stream. should be reasonable possible. Might be a little laggy though.

I’m using “.status.session_id” for that. If response is empty - nobody is connected to the device.

Is there an event you can subscribe to to know when that changes?

Hi i’m thinking about to get chromcast device to use for notifications only today I’ve got squeezelite players for notifications but doorbell notification gets womwhere between 5-30secoinds delay anyone have an idea about notification delays with chromecast. And could i test i with my vide chromecast or is it strictly working for audio cromecast.

Hi, like to find a way to turn off my stereo(with a xiaomi plug) when my chromecast is not reciving anything. Can I do this with this script or do you know of any other way to do it?

check out the new binding. it has an idling status and your can also check for a change in appid

1 Like

The new OH chromecast binding or an other?

Openhab binding. Its been updated.

The 2.2.0 snapshot file, doesn’t load, it’s the one from today (this evening):

2017-11-29 23:47:37.026 [WARN ] [org.apache.felix.fileinstall ] - Error while starting bundle: file:/usr/share/openhab2/addons/org.openhab.binding.chromecast-2.2.0-SNAPSHOT.jar
org.osgi.framework.BundleException: Could not resolve module: org.openhab.binding.chromecast [230]
Unresolved requirement: Import-Package: com.fasterxml.jackson.annotation
at org.eclipse.osgi.container.Module.start([org.eclipse.osgi-3.10.101.v20150820-1432.jar:]
at org.eclipse.osgi.internal.framework.EquinoxBundle.start([org.eclipse.osgi-3.10.101.v20150820-1432.jar:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle([4:org.apache.felix.fileinstall:3.5.6]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles([4:org.apache.felix.fileinstall:3.5.6]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess([4:org.apache.felix.fileinstall:3.5.6]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process([4:org.apache.felix.fileinstall:3.5.6]

I’m not sure about that one. it’s working for me.

I updated my openHAB installation and now the binding is working again

Will check the new features

1 Like