Skip to main content

Library for Python 3.6+ to communicate with the Google Chromecast. It currently supports:

  • Auto discovering connected Chromecasts on the network

  • Start the default media receiver and play any online media

  • Control playback of current playing media

  • Implement Google Chromecast api v2

  • Communicate with apps via channels

  • Easily extendable to add support for unsupported namespaces

  • Multi-room setups with Audio cast devices

Check out Home Assistant for a ready-made solution using PyChromecast for controlling and automating your Chromecast or Cast-enabled device like Google Home.

Dependencies

PyChromecast depends on the Python packages requests, protobuf and zeroconf. Make sure you have these dependencies installed using pip install -r requirements.txt

How to use

>> import time
>> import pychromecast

>> chromecasts = pychromecast.get_chromecasts()
>> [cc.device.friendly_name for cc in chromecasts]
['Dev', 'Living Room', 'Den', 'Bedroom']

>> cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Living Room")
>> # Start worker thread and wait for cast device to be ready
>> cast.wait()
>> print(cast.device)
DeviceStatus(friendly_name='Living Room', model_name='Chromecast', manufacturer='Google Inc.', uuid=UUID('df6944da-f016-4cb8-97d0-3da2ccaa380b'), cast_type='cast')

>> print(cast.status)
CastStatus(is_active_input=True, is_stand_by=False, volume_level=1.0, volume_muted=False, app_id='CC1AD845', display_name='Default Media Receiver', namespaces=['urn:x-cast:com.google.cast.player.message', 'urn:x-cast:com.google.cast.media'], session_id='CCA39713-9A4F-34A6-A8BF-5D97BE7ECA5C', transport_id='web-9', status_text='')

>> mc = cast.media_controller
>> mc.play_media('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', 'video/mp4')
>> mc.block_until_active()
>> print(mc.status)
MediaStatus(current_time=42.458322, content_id='http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', content_type='video/mp4', duration=596.474195, stream_type='BUFFERED', idle_reason=None, media_session_id=1, playback_rate=1, player_state='PLAYING', supported_media_commands=15, volume_level=1, volume_muted=False)

>> mc.pause()
>> time.sleep(5)
>> mc.play()

Adding support for extra namespaces

Each app that runs on the Chromecast supports namespaces. They specify a JSON-based mini-protocol. This is used to communicate between the Chromecast and your phone/browser and now Python.

Support for extra namespaces is added by using controllers. To add your own namespace to a current chromecast instance you will first have to define your controller. Example of a minimal controller:

from pychromecast.controllers import BaseController

class MyController(BaseController):
    def __init__(self):
        super(MyController, self).__init__(
            "urn:x-cast:my.super.awesome.namespace")

    def receive_message(self, message, data):
        print("Wow, I received this message: {}".format(data))

        return True  # indicate you handled this message

    def request_beer(self):
        self.send_message({'request': 'beer'})

After you have defined your controller you will have to add an instance to a Chromecast object: cast.register_handler(MyController()). When a message is received with your namespace it will be routed to your controller.

For more options see the BaseController. For an example of a fully implemented controller see the MediaController.

Exploring existing namespaces

So you’ve got PyChromecast running and decided it is time to add support to your favorite app. No worries, the following instructions will have you covered in exploring the possibilities.

The following instructions require the use of the Google Chrome browser and the Google Cast plugin.

  • In Chrome, go to chrome://net-export/

  • Select ‘Include raw bytes (will include cookies and credentials)’

  • Click ‘Start Logging to Disk’

  • Open a new tab, browse to your favorite application on the web that has Chromecast support and start casting.

  • Go back to the tab that is capturing events and click on stop.

  • Open https://netlog-viewer.appspot.com/ and select your event log file.

  • Browse to https://netlog-viewer.appspot.com/#events&q=type:SOCKET, and find the socket that has familiar JSON data. (For me, it’s usually the second or third from the top.)

  • Go through the results and collect the JSON that is exchanged.

  • Now write a controller that is able to mimic this behavior :-)

Ignoring CEC Data

The Chromecast typically reports whether it is the active input on the device to which it is connected. This value is stored inside a cast object in the following property.

cast.status.is_active_input

Some Chromecast users have reported CEC incompatibilities with their media center devices. These incompatibilities may sometimes cause this active input value to be reported improperly.

This active input value is typically used to determine if the Chromecast is idle. PyChromecast is capable of ignoring the active input value when determining if the Chromecast is idle in the instance that the Chromecast is returning erroneous values. To ignore this CEC detection data in PyChromecast, append a Linux style wildcard formatted string to the IGNORE_CEC list in PyChromecast like in the example below.

pychromecast.IGNORE_CEC.append('*')  # Ignore CEC on all devices
pychromecast.IGNORE_CEC.append('Living Room')  # Ignore CEC on Chromecasts named Living Room

Thanks

I would like to thank Fred Clift for laying the socket client ground work. Without him it would not have been possible!

Metadata

Release files for PyChromecast 6.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyChromecast 6.0.0
File Size Uploaded
PyChromecast-6.0.0.tar.gz 48.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for PyChromecast 6.0.0
File Interpreter ABI Platform
PyChromecast-6.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 103.2 kB

Release files / PyChromecast-6.0.0.tar.gz

Download URL PyChromecast-6.0.0.tar.gz
Size 48.0 kB
Tags Source
SHA-256 checksum
How to use checksums
15beaafdb155885794443d99fa687a2787d8bad8ba440ecda10bb72bd6c8c815
BLAKE2b-256 checksum
How to use checksums
f2cf93bd9cdc82b0e34688c2ad7c57b82cbbdd6422de1d4563113a6126397b03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release files / PyChromecast-6.0.0-py2.py3-none-any.whl

Download URL PyChromecast-6.0.0-py2.py3-none-any.whl
Size 55.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ac9d615e4f1e96e3d824c8c52381bc172bb0b687d18c04f31b8f612a9bdee279
BLAKE2b-256 checksum
How to use checksums
9139bf05a100facafde4349a9a363bc4374200aa2453ac440811e6171db4a543
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release history Release notifications | RSS feed

14.0.9

2 release files

14.0.7

2 release files

14.0.6

2 release files

14.0.5

2 release files

14.0.1

2 release files

14.0.0

2 release files

13.1.0

2 release files

13.0.8

2 release files

13.0.6

2 release files

13.0.5

2 release files

13.0.4

2 release files

13.0.3

2 release files

13.0.1

2 release files

13.0.0

2 release files

12.1.4

2 release files

12.1.3

2 release files

12.0.0

2 release files

10.3.0

2 release files

10.2.2

2 release files

10.2.1

2 release files

10.2.0

2 release files

10.1.1

2 release files

10.1.0

2 release files

10.0.0

2 release files

9.4.0

2 release files

9.3.1

2 release files

9.3.0

2 release files

9.2.1

2 release files

9.2.0

2 release files

9.1.2

2 release files

9.1.1

2 release files

9.0.0

2 release files

8.1.2

2 release files

8.1.1

2 release files

8.1.0

2 release files

8.0.0

2 release files

7.7.2

2 release files

7.7.1

2 release files

7.7.0

2 release files

7.6.0

2 release files

7.5.1

2 release files

7.5.0

2 release files

7.4.1

2 release files

7.3.0

2 release files

7.2.1

2 release files

7.2.0

2 release files

7.1.2

2 release files

7.1.1

2 release files

7.1.0

2 release files

7.0.1

2 release files

This release

6.0.0 This release

2 release files

5.3.0

2 release files

5.2.0

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.2.3

2 release files

4.2.1

2 release files

4.2.0

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.2.3

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7

2 release files

0.6.14

2 release files

0.6.13

2 release files

0.6.12

2 release files

0.6.10

2 release files

0.6.9

1 release file

0.6.8.2

1 release file

0.6.8.1

1 release file

0.6.8

1 release file

0.6.7

1 release file

0.6.6

1 release file

0.6.5

1 release file

0.6.4

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0.3

1 release file

0.6.0.2

1 release file

0.6.0.1

1 release file

0.6

1 release file

0.5.1.1

1 release file

0.5.1

1 release file

0.5

1 release file

0.4.4

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page