PyChromecast Documentation

repository·master·Indexed 25 days ago

https://github.com/home-assistant-libs/pychromecast

A Python library for communicating with Google Chromecast devices. It provides functionality for device discovery via mDNS, media playback control using MediaController, multi-room audio setups, and the ability to implement custom app support by subclassing BaseController.

Tokens
794
Snippets
3
Records
6
Agent score
33%

What's inside PyChromecast

  1. Discover and connect to Chromecasts

    master

    You can discover Chromecasts on your network using pychromecast.get_listed_chromecasts(). You can filter by friendly_names and adjust the discovery_timeout if devices are not being found.

    To stop discovery, use pychromecast.discovery.stop_discovery(browser).

  2. Networking requirements for mDNS discovery

    master

    PyChromecast uses mDNS for device discovery, which relies on multicast UDP on port 5353. For discovery to work, ensure:

    • Multicast UDP is forwarded by your Wi-Fi router.
    • The device running PyChromecast allows inbound and outbound traffic on port 5353.
    • The device is on the same subnet as the Chromecast devices.

    If these conditions cannot be met, you can pass a list of known IP addresses or hostnames directly to the discovery functions.

  3. Add support for extra namespaces using BaseController

    master

    To communicate with custom apps on a Chromecast, you must implement a custom controller by subclassing BaseController. You define the namespace in the constructor, implement receive_message to handle incoming data, and use send_message to send data to the device. Register the controller using cast.register_handler().

    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'})
    
    # Register the controller to a cast instance
    cast.register_handler(MyController())
  4. Ignore CEC Data for active input detection

    master

    If your Chromecast reports incorrect is_active_input status due to CEC incompatibilities, you can instruct PyChromecast to ignore this value by appending a Linux-style wildcard or a specific device name to the pychromecast.IGNORE_CEC list.

    import pychromecast
    
    # Ignore CEC on all devices
    pychromecast.IGNORE_CEC.append('*')
    
    # Ignore CEC on a specific Chromecast by name
    pychromecast.IGNORE_CEC.append('Living Room')