All Projects → home-assistant-libs → Pychromecast

home-assistant-libs / Pychromecast

Licence: mit
Library for Python 3 to communicate with the Google Chromecast.

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to Pychromecast

chromecast-mqtt-connector
Make your Chromecast devices discoverable and controllable via MQTT.
Stars: ✭ 40 (-98.23%)
Mutual labels:  chromecast, internet-of-things, cast
punchtop
A power hour written in rust with Chromecast support
Stars: ✭ 26 (-98.85%)
Mutual labels:  chromecast, cast
Pi-OpenCast
📺 Transform your Raspberry Pi into an awesome streaming device.
Stars: ✭ 29 (-98.72%)
Mutual labels:  chromecast, cast
vlitejs
🦋 vLitejs is a fast and lightweight Javascript library for customizing video and audio player in Javascript with a minimalist theme (HTML5, Youtube, Vimeo, Dailymotion)
Stars: ✭ 162 (-92.84%)
Mutual labels:  chromecast, cast
chromecast-api
📺 Chromecast Node.js module
Stars: ✭ 122 (-94.61%)
Mutual labels:  chromecast, cast
android-cast-remote-display-sample
📻 Google Cast's Remote Display Sample for Android
Stars: ✭ 38 (-98.32%)
Mutual labels:  chromecast, cast
casita
A macOS menubar app to control media playing on your Google Cast enabled devices. 🏡
Stars: ✭ 22 (-99.03%)
Mutual labels:  chromecast, cast
Rhythmbox-Chromecast
Send all your music to your Chromecast (Audio) within Rhythmbox
Stars: ✭ 17 (-99.25%)
Mutual labels:  chromecast, chromecast-audio
Mkchromecast
Cast macOS and Linux Audio/Video to your Google Cast and Sonos Devices
Stars: ✭ 1,837 (-18.79%)
Mutual labels:  chromecast, chromecast-audio
Kapua
Stars: ✭ 169 (-92.53%)
Mutual labels:  internet-of-things
Siricontrol System
Control anything with Siri voice commands.
Stars: ✭ 180 (-92.04%)
Mutual labels:  internet-of-things
Airconnect
Use AirPlay to stream to UPnP/Sonos & Chromecast devices
Stars: ✭ 2,349 (+3.85%)
Mutual labels:  chromecast
Make
📖📖📖📖📖 写给软件工程师看的硬件编程指南
Stars: ✭ 170 (-92.48%)
Mutual labels:  internet-of-things
Home Assistant
Home-Assistant-Config
Stars: ✭ 182 (-91.95%)
Mutual labels:  internet-of-things
Homeassistant
Example Home Assistant Configs
Stars: ✭ 168 (-92.57%)
Mutual labels:  internet-of-things
Libpurecoollink
Dyson Pure Cool link python library
Stars: ✭ 186 (-91.78%)
Mutual labels:  internet-of-things
Rapidbay
Self-hosted torrent video streaming service compatible with Chromecast and AppleTV deployable in the cloud
Stars: ✭ 163 (-92.79%)
Mutual labels:  chromecast
Designiot
教你设计物联网系统。构建自己的Internet of Things 。
Stars: ✭ 1,983 (-12.33%)
Mutual labels:  internet-of-things
Aiocoap
The Python CoAP library
Stars: ✭ 185 (-91.82%)
Mutual labels:  internet-of-things
Enmasse
EnMasse - Self-service messaging on Kubernetes and OpenShift
Stars: ✭ 185 (-91.82%)
Mutual labels:  internet-of-things

pychromecast Build Status

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

>> # List chromecasts on the network, but don't connect
>> services, browser = pychromecast.discovery.discover_chromecasts()
>> # Shut down discovery
>> pychromecast.discovery.stop_discovery(browser)

>> # Discover and connect to chromecasts named Living Room
>> chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=["Living Room"])
>> [cc.device.friendly_name for cc in chromecasts]
['Living Room']

>> cast = chromecasts[0]
>> # 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()

>> # Shut down discovery
>> pychromecast.discovery.stop_discovery(browser)

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!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].