File: simple_listener_example.py

package info (click to toggle)
pychromecast 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 332 kB
  • sloc: python: 1,751; makefile: 6; sh: 3
file content (40 lines) | stat: -rw-r--r-- 1,096 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Example showing how to create a simple Chromecast event listener for
device and media status events
"""

import time
import pychromecast


class StatusListener:
    def __init__(self, name, cast):
        self.name = name
        self.cast = cast

    def new_cast_status(self, status):
        print('[',time.ctime(),' - ', self.name,'] status chromecast change:')
        print(status)


class StatusMediaListener:
    def __init__(self, name, cast):
        self.name = name
        self.cast= cast

    def new_media_status(self, status):
        print('[',time.ctime(),' - ', self.name,'] status media change:')
        print(status)


chromecasts = pychromecast.get_chromecasts()
chromecast = next(cc for cc in chromecasts
                  if cc.device.friendly_name == "Living Room Speaker")

listenerCast = StatusListener(chromecast.name, chromecast)
chromecast.register_status_listener(listenerCast)

listenerMedia = StatusMediaListener(chromecast.name, chromecast)
chromecast.media_controller.register_status_listener(listenerMedia)

input('Listening for Chromecast events...\n\n')