2019-02-05 10:02:27 +01:00
|
|
|
import datetime
|
2018-11-13 20:20:55 +01:00
|
|
|
import re
|
2019-06-27 23:52:40 +02:00
|
|
|
import time
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-02-11 00:55:20 +01:00
|
|
|
from platypush.context import get_plugin, get_bus
|
2019-06-27 23:52:40 +02:00
|
|
|
from platypush.plugins import action
|
2019-02-05 02:30:20 +01:00
|
|
|
from platypush.plugins.media import MediaPlugin
|
2019-02-06 11:51:44 +01:00
|
|
|
from platypush.utils import get_mime_type
|
2019-02-11 00:55:20 +01:00
|
|
|
from platypush.message.event.media import MediaPlayEvent, MediaPlayRequestEvent, \
|
2019-06-27 23:52:40 +02:00
|
|
|
MediaStopEvent, MediaPauseEvent, NewPlayingMediaEvent, MediaVolumeChangedEvent, MediaSeekEvent
|
|
|
|
|
|
|
|
|
|
|
|
def convert_status(status):
|
|
|
|
attrs = [a for a in dir(status) if not a.startswith('_')
|
|
|
|
and not callable(getattr(status, a))]
|
|
|
|
|
|
|
|
renamed_attrs = {
|
|
|
|
'current_time': 'position',
|
|
|
|
'player_state': 'state',
|
|
|
|
'supports_seek': 'seekable',
|
|
|
|
'volume_level': 'volume',
|
|
|
|
'volume_muted': 'mute',
|
|
|
|
'content_id': 'url',
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = {}
|
|
|
|
for attr in attrs:
|
|
|
|
value = getattr(status, attr)
|
|
|
|
if attr == 'volume_level':
|
|
|
|
value *= 100
|
|
|
|
if attr == 'player_state':
|
|
|
|
value = value.lower()
|
|
|
|
if value == 'paused':
|
|
|
|
value = 'pause'
|
|
|
|
if value == 'playing':
|
|
|
|
value = 'play'
|
|
|
|
if isinstance(value, datetime.datetime):
|
|
|
|
value = value.isoformat()
|
|
|
|
|
|
|
|
if attr in renamed_attrs:
|
|
|
|
ret[renamed_attrs[attr]] = value
|
|
|
|
else:
|
|
|
|
ret[attr] = value
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def post_event(evt_type, **evt):
|
|
|
|
bus = get_bus()
|
|
|
|
bus.post(evt_type(player=evt.get('device'), plugin='media.chromecast', **evt))
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
class MediaChromecastPlugin(MediaPlugin):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
Plugin to interact with Chromecast devices
|
|
|
|
|
2018-11-13 20:20:55 +01:00
|
|
|
Supported formats:
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
* HTTP media URLs
|
2018-11-13 20:20:55 +01:00
|
|
|
* YouTube URLs
|
|
|
|
* Plex (through ``media.plex`` plugin, experimental)
|
|
|
|
|
2018-11-12 16:50:20 +01:00
|
|
|
Requires:
|
|
|
|
|
|
|
|
* **pychromecast** (``pip install pychromecast``)
|
|
|
|
"""
|
|
|
|
|
|
|
|
STREAM_TYPE_UNKNOWN = "UNKNOWN"
|
|
|
|
STREAM_TYPE_BUFFERED = "BUFFERED"
|
|
|
|
STREAM_TYPE_LIVE = "LIVE"
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
class MediaListener:
|
|
|
|
def __init__(self, name, cast):
|
|
|
|
self.name = name
|
|
|
|
self.cast = cast
|
|
|
|
self.status = convert_status(cast.media_controller.status)
|
|
|
|
self.last_status_timestamp = time.time()
|
|
|
|
|
|
|
|
def new_media_status(self, status):
|
|
|
|
status = convert_status(status)
|
|
|
|
|
|
|
|
if status.get('url') and status.get('url') != self.status.get('url'):
|
|
|
|
post_event(NewPlayingMediaEvent, resource=status['url'],
|
|
|
|
title=status.get('title'), device=self.name)
|
|
|
|
if status.get('state') != self.status.get('state'):
|
|
|
|
if status.get('state') == 'play':
|
|
|
|
post_event(MediaPlayEvent, resource=status['url'], device=self.name)
|
|
|
|
elif status.get('state') == 'pause':
|
|
|
|
post_event(MediaPauseEvent, resource=status['url'], device=self.name)
|
|
|
|
elif status.get('state') in ['stop', 'idle']:
|
|
|
|
post_event(MediaStopEvent, device=self.name)
|
|
|
|
if status.get('volume') != self.status.get('volume'):
|
|
|
|
post_event(MediaVolumeChangedEvent, volume=status.get('volume'), device=self.name)
|
|
|
|
if abs(status.get('position') - self.status.get('position')) > time.time() - self.last_status_timestamp + 5:
|
|
|
|
post_event(MediaSeekEvent, position=status.get('position'), device=self.name)
|
|
|
|
|
|
|
|
self.last_status_timestamp = time.time()
|
|
|
|
self.status = status
|
|
|
|
|
|
|
|
class SubtitlesAsyncHandler:
|
|
|
|
def __init__(self, mc, subtitle_id):
|
|
|
|
self.mc = mc
|
|
|
|
self.subtitle_id = subtitle_id
|
|
|
|
self.initialized = False
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def new_media_status(self, status):
|
|
|
|
if self.subtitle_id and not self.initialized:
|
|
|
|
self.mc.update_status()
|
|
|
|
self.mc.enable_subtitle(self.subtitle_id)
|
|
|
|
self.initialized = True
|
|
|
|
|
2018-11-12 16:50:20 +01:00
|
|
|
def __init__(self, chromecast=None, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
:param chromecast: Default Chromecast to cast to if no name is specified
|
|
|
|
:type chromecast: str
|
|
|
|
"""
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
self._is_local = False
|
2018-11-12 16:50:20 +01:00
|
|
|
self.chromecast = chromecast
|
|
|
|
self.chromecasts = {}
|
2019-06-27 23:52:40 +02:00
|
|
|
self._media_listeners = {}
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2020-07-25 01:35:06 +02:00
|
|
|
@staticmethod
|
|
|
|
def _get_chromecasts(*args, **kwargs):
|
|
|
|
import pychromecast
|
|
|
|
chromecasts = pychromecast.get_chromecasts(*args, **kwargs)
|
|
|
|
if isinstance(chromecasts, tuple):
|
|
|
|
return chromecasts[0]
|
|
|
|
return chromecasts
|
|
|
|
|
2018-11-12 16:50:20 +01:00
|
|
|
@action
|
2019-06-18 18:14:24 +02:00
|
|
|
def get_chromecasts(self, tries=2, retry_wait=10, timeout=60,
|
|
|
|
blocking=True, callback=None):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
Get the list of Chromecast devices
|
2019-06-18 18:14:24 +02:00
|
|
|
|
|
|
|
:param tries: Number of retries (default: 2)
|
|
|
|
:type tries: int
|
|
|
|
|
|
|
|
:param retry_wait: Number of seconds between retries (default: 10 seconds)
|
|
|
|
:type retry_wait: int
|
|
|
|
|
|
|
|
:param timeout: Timeout before failing the call (default: 60 seconds)
|
|
|
|
:type timeout: int
|
|
|
|
|
|
|
|
:param blocking: If true, then the function will block until all the Chromecast
|
|
|
|
devices have been scanned. If false, then the provided callback function will be
|
|
|
|
invoked when a new device is discovered
|
|
|
|
:type blocking: bool
|
|
|
|
|
|
|
|
:param callback: If blocking is false, then you can provide a callback function that
|
|
|
|
will be invoked when a new device is discovered
|
|
|
|
:type callback: func
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
|
2019-01-20 15:16:16 +01:00
|
|
|
self.chromecasts.update({
|
|
|
|
cast.device.friendly_name: cast
|
2020-07-25 01:35:06 +02:00
|
|
|
for cast in self._get_chromecasts(tries=tries, retry_wait=retry_wait,
|
|
|
|
timeout=timeout, blocking=blocking,
|
|
|
|
callback=callback)
|
2019-01-20 15:16:16 +01:00
|
|
|
})
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
for name, cast in self.chromecasts.items():
|
|
|
|
self._update_listeners(name, cast)
|
|
|
|
|
|
|
|
return [{
|
2018-11-12 16:50:20 +01:00
|
|
|
'type': cc.cast_type,
|
|
|
|
'name': cc.name,
|
|
|
|
'manufacturer': cc.device.manufacturer,
|
|
|
|
'model_name': cc.model_name,
|
|
|
|
'uuid': str(cc.device.uuid),
|
2020-11-11 03:07:42 +01:00
|
|
|
'address': cc.host if hasattr(cc, 'host') else cc.uri.split(':')[0],
|
|
|
|
'port': cc.port if hasattr(cc, 'port') else int(cc.uri.split(':')[1]),
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
'status': ({
|
|
|
|
'app': {
|
|
|
|
'id': cc.app_id,
|
|
|
|
'name': cc.app_display_name,
|
|
|
|
},
|
|
|
|
|
2019-02-06 09:46:57 +01:00
|
|
|
'media': self.status(cc.name).output,
|
2018-11-12 16:50:20 +01:00
|
|
|
'is_active_input': cc.status.is_active_input,
|
|
|
|
'is_stand_by': cc.status.is_stand_by,
|
|
|
|
'is_idle': cc.is_idle,
|
|
|
|
'namespaces': cc.status.namespaces,
|
|
|
|
'volume': round(100*cc.status.volume_level, 2),
|
|
|
|
'muted': cc.status.volume_muted,
|
|
|
|
} if cc.status else {}),
|
2019-06-27 23:52:40 +02:00
|
|
|
} for cc in self.chromecasts.values()]
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
def _update_listeners(self, name, cast):
|
|
|
|
if name not in self._media_listeners:
|
|
|
|
cast.start()
|
|
|
|
self._media_listeners[name] = self.MediaListener(name=name, cast=cast)
|
|
|
|
cast.media_controller.register_status_listener(self._media_listeners[name])
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-06-18 18:14:24 +02:00
|
|
|
def get_chromecast(self, chromecast=None, n_tries=2):
|
2019-12-01 23:35:05 +01:00
|
|
|
import pychromecast
|
2019-02-05 02:30:20 +01:00
|
|
|
if isinstance(chromecast, pychromecast.Chromecast):
|
|
|
|
return chromecast
|
|
|
|
|
2018-11-12 16:50:20 +01:00
|
|
|
if not chromecast:
|
|
|
|
if not self.chromecast:
|
|
|
|
raise RuntimeError('No Chromecast specified nor default Chromecast configured')
|
|
|
|
chromecast = self.chromecast
|
|
|
|
|
|
|
|
if chromecast not in self.chromecasts:
|
2019-02-05 02:30:20 +01:00
|
|
|
casts = {}
|
|
|
|
while n_tries > 0:
|
|
|
|
n_tries -= 1
|
|
|
|
casts.update({
|
|
|
|
cast.device.friendly_name: cast
|
2020-07-25 01:35:06 +02:00
|
|
|
for cast in self._get_chromecasts()
|
2019-02-05 02:30:20 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if chromecast in casts:
|
|
|
|
self.chromecasts.update(casts)
|
|
|
|
break
|
2018-11-13 20:20:55 +01:00
|
|
|
|
|
|
|
if chromecast not in self.chromecasts:
|
|
|
|
raise RuntimeError('Device {} not found'.format(chromecast))
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
cast = self.chromecasts[chromecast]
|
|
|
|
cast.wait()
|
|
|
|
return cast
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def play(self, resource, content_type=None, chromecast=None, title=None,
|
2019-06-27 23:52:40 +02:00
|
|
|
image_url=None, autoplay=True, current_time=0,
|
|
|
|
stream_type=STREAM_TYPE_BUFFERED, subtitles=None,
|
|
|
|
subtitles_lang='en-US', subtitles_mime='text/vtt',
|
|
|
|
subtitle_id=1):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
Cast media to a visible Chromecast
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
:param resource: Media to cast
|
|
|
|
:type resource: str
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
:param content_type: Content type as a MIME type string
|
2018-11-12 16:50:20 +01:00
|
|
|
:type content_type: str
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
|
|
|
|
:param title: Optional title
|
|
|
|
:type title: str
|
|
|
|
|
|
|
|
:param image_url: URL of the image to use for the thumbnail
|
|
|
|
:type image_url: str
|
|
|
|
|
|
|
|
:param autoplay: Set it to false if you don't want the content to start playing immediately (default: true)
|
|
|
|
:type autoplay: bool
|
|
|
|
|
|
|
|
:param current_time: Time to start the playback in seconds (default: 0)
|
|
|
|
:type current_time: int
|
|
|
|
|
|
|
|
:param stream_type: Type of stream to cast. Can be BUFFERED (default), LIVE or UNKNOWN
|
|
|
|
:type stream_type: str
|
|
|
|
|
|
|
|
:param subtitles: URL of the subtitles to be shown
|
|
|
|
:type subtitles: str
|
|
|
|
|
|
|
|
:param subtitles_lang: Subtitles language (default: en-US)
|
|
|
|
:type subtitles_lang: str
|
|
|
|
|
|
|
|
:param subtitles_mime: Subtitles MIME type (default: text/vtt)
|
|
|
|
:type subtitles_mime: str
|
|
|
|
|
|
|
|
:param subtitle_id: ID of the subtitles to be loaded (default: 1)
|
|
|
|
:type subtitle_id: int
|
|
|
|
"""
|
|
|
|
|
2019-12-01 23:35:05 +01:00
|
|
|
from pychromecast.controllers.youtube import YouTubeController
|
2018-11-13 20:20:55 +01:00
|
|
|
if not chromecast:
|
|
|
|
chromecast = self.chromecast
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
post_event(MediaPlayRequestEvent, resource=resource, device=chromecast)
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2018-11-13 20:20:55 +01:00
|
|
|
|
2018-11-12 16:50:20 +01:00
|
|
|
mc = cast.media_controller
|
2019-02-05 02:30:20 +01:00
|
|
|
yt = self._get_youtube_url(resource)
|
2018-11-13 20:20:55 +01:00
|
|
|
|
|
|
|
if yt:
|
|
|
|
self.logger.info('Playing YouTube video {} on {}'.format(
|
|
|
|
yt, chromecast))
|
|
|
|
|
|
|
|
hndl = YouTubeController()
|
|
|
|
cast.register_handler(hndl)
|
|
|
|
hndl.update_screen_id()
|
|
|
|
return hndl.play_video(yt)
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
resource = self._get_resource(resource)
|
2019-02-06 11:51:44 +01:00
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
if not content_type:
|
2019-02-06 11:51:44 +01:00
|
|
|
content_type = get_mime_type(resource)
|
2019-02-05 02:30:20 +01:00
|
|
|
|
2018-11-13 20:20:55 +01:00
|
|
|
if not content_type:
|
|
|
|
raise RuntimeError('content_type required to process media {}'.
|
2019-02-05 02:30:20 +01:00
|
|
|
format(resource))
|
2018-11-13 20:20:55 +01:00
|
|
|
|
2019-02-07 14:26:10 +01:00
|
|
|
if not resource.startswith('http://') and \
|
|
|
|
not resource.startswith('https://'):
|
2019-02-06 11:51:44 +01:00
|
|
|
resource = self.start_streaming(resource).output['url']
|
2019-02-07 14:26:10 +01:00
|
|
|
self.logger.info('HTTP media stream started on {}'.format(resource))
|
2019-02-06 11:51:44 +01:00
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
self.logger.info('Playing {} on {}'.format(resource, chromecast))
|
2018-11-13 01:29:24 +01:00
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
mc.play_media(resource, content_type, title=title, thumb=image_url,
|
2018-11-12 16:50:20 +01:00
|
|
|
current_time=current_time, autoplay=autoplay,
|
|
|
|
stream_type=stream_type, subtitles=subtitles,
|
2019-02-11 00:55:20 +01:00
|
|
|
subtitles_lang=subtitles_lang,
|
|
|
|
subtitles_mime=subtitles_mime, subtitle_id=subtitle_id)
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
if subtitles:
|
|
|
|
mc.register_status_listener(self.SubtitlesAsyncHandler(mc, subtitle_id))
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
mc.block_until_active()
|
2020-02-21 18:40:46 +01:00
|
|
|
|
|
|
|
if self.volume:
|
|
|
|
self.set_volume(volume=self.volume, chromecast=chromecast)
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 20:20:55 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_youtube_url(cls, url):
|
|
|
|
m = re.match('https?://www.youtube.com/watch\?v=([^&]+).*', url)
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
|
|
|
|
m = re.match('youtube:video:(.*)', url)
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-11-13 23:09:19 +01:00
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def load(self, *args, **kwargs):
|
|
|
|
return self.play(*args, **kwargs)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
|
|
|
def pause(self, chromecast=None):
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
|
|
|
cast = self.get_chromecast(chromecast)
|
|
|
|
|
2018-11-13 23:17:51 +01:00
|
|
|
if cast.media_controller.is_paused:
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.media_controller.play()
|
2019-02-05 11:02:31 +01:00
|
|
|
elif cast.media_controller.is_playing:
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.media_controller.pause()
|
2018-11-13 23:09:19 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.wait()
|
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
|
|
|
def stop(self, chromecast=None):
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
|
|
|
cast = self.get_chromecast(chromecast)
|
|
|
|
cast.media_controller.stop()
|
|
|
|
cast.wait()
|
2018-11-13 23:09:19 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
|
|
|
def rewind(self, chromecast=None):
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
|
|
|
cast = self.get_chromecast(chromecast)
|
|
|
|
cast.media_controller.rewind()
|
|
|
|
cast.wait()
|
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def set_position(self, position, chromecast=None):
|
2019-06-27 23:52:40 +02:00
|
|
|
cast = self.get_chromecast(chromecast or self.chromecast)
|
|
|
|
cast.media_controller.seek(position)
|
|
|
|
cast.wait()
|
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
@action
|
|
|
|
def seek(self, position, chromecast=None):
|
|
|
|
return self.forward(chromecast=chromecast, offset=position)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
2018-11-13 23:23:14 +01:00
|
|
|
@action
|
2021-02-04 01:56:28 +01:00
|
|
|
def back(self, chromecast=None, offset=30):
|
2019-06-27 23:52:40 +02:00
|
|
|
cast = self.get_chromecast(chromecast or self.chromecast)
|
|
|
|
mc = cast.media_controller
|
2018-11-13 23:23:14 +01:00
|
|
|
if mc.status.current_time:
|
2019-06-27 23:52:40 +02:00
|
|
|
mc.seek(mc.status.current_time-offset)
|
|
|
|
cast.wait()
|
2018-11-13 23:23:14 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 23:23:14 +01:00
|
|
|
|
|
|
|
@action
|
2021-02-04 01:56:28 +01:00
|
|
|
def forward(self, chromecast=None, offset=30):
|
2019-06-27 23:52:40 +02:00
|
|
|
cast = self.get_chromecast(chromecast or self.chromecast)
|
|
|
|
mc = cast.media_controller
|
2018-11-13 23:23:14 +01:00
|
|
|
if mc.status.current_time:
|
2019-06-27 23:52:40 +02:00
|
|
|
mc.seek(mc.status.current_time+offset)
|
|
|
|
cast.wait()
|
2018-11-13 23:23:14 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-13 23:23:14 +01:00
|
|
|
|
2018-11-13 23:09:19 +01:00
|
|
|
@action
|
|
|
|
def is_playing(self, chromecast=None):
|
2018-11-13 23:17:51 +01:00
|
|
|
return self.get_chromecast(chromecast or self.chromecast).media_controller.is_playing
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
|
|
|
def is_paused(self, chromecast=None):
|
2018-11-13 23:17:51 +01:00
|
|
|
return self.get_chromecast(chromecast or self.chromecast).media_controller.is_paused
|
2018-11-13 23:09:19 +01:00
|
|
|
|
2019-02-05 09:49:50 +01:00
|
|
|
@action
|
|
|
|
def is_idle(self, chromecast=None):
|
|
|
|
return self.get_chromecast(chromecast or self.chromecast).media_controller.is_idle
|
|
|
|
|
2018-11-13 23:09:19 +01:00
|
|
|
@action
|
2019-02-11 00:55:20 +01:00
|
|
|
def list_subtitles(self, chromecast=None):
|
|
|
|
return self.get_chromecast(chromecast or self.chromecast) \
|
|
|
|
.media_controller.subtitle_tracks
|
|
|
|
|
|
|
|
@action
|
|
|
|
def enable_subtitles(self, chromecast=None, track_id=None):
|
|
|
|
mc = self.get_chromecast(chromecast or self.chromecast).media_controller
|
|
|
|
if track_id is not None:
|
|
|
|
return mc.enable_subtitle(track_id)
|
|
|
|
elif mc.subtitle_tracks:
|
|
|
|
return mc.enable_subtitle(mc.subtitle_tracks[0].get('trackId'))
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-11 00:55:20 +01:00
|
|
|
def disable_subtitles(self, chromecast=None, track_id=None):
|
|
|
|
mc = self.get_chromecast(chromecast or self.chromecast).media_controller
|
2019-06-18 18:14:24 +02:00
|
|
|
if track_id:
|
|
|
|
return mc.disable_subtitle(track_id)
|
2019-02-11 00:55:20 +01:00
|
|
|
elif mc.current_subtitle_tracks:
|
|
|
|
return mc.disable_subtitle(mc.current_subtitle_tracks[0])
|
|
|
|
|
|
|
|
@action
|
|
|
|
def toggle_subtitles(self, chromecast=None):
|
|
|
|
mc = self.get_chromecast(chromecast or self.chromecast).media_controller
|
|
|
|
all_subs = mc.status.subtitle_tracks
|
|
|
|
cur_subs = mc.status.status.current_subtitle_tracks
|
|
|
|
|
|
|
|
if cur_subs:
|
2019-06-18 18:14:24 +02:00
|
|
|
return self.disable_subtitles(chromecast, cur_subs[0])
|
2019-02-11 00:55:20 +01:00
|
|
|
else:
|
2019-06-18 18:14:24 +02:00
|
|
|
return self.enable_subtitles(chromecast, all_subs[0].get('trackId'))
|
2018-11-13 23:09:19 +01:00
|
|
|
|
|
|
|
@action
|
|
|
|
def status(self, chromecast=None):
|
2019-06-27 23:52:40 +02:00
|
|
|
cast = self.get_chromecast(chromecast or self.chromecast)
|
|
|
|
status = cast.media_controller.status
|
|
|
|
return convert_status(status)
|
2018-11-13 23:09:19 +01:00
|
|
|
|
2018-11-12 16:50:20 +01:00
|
|
|
@action
|
|
|
|
def disconnect(self, chromecast=None, timeout=None, blocking=True):
|
|
|
|
"""
|
|
|
|
Disconnect a Chromecast and wait for it to terminate
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
|
|
|
|
:param timeout: Number of seconds to wait for disconnection (default: None: block until termination)
|
|
|
|
:type timeout: float
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param blocking: If set (default), then the code will wait until disconnection, otherwise it will return
|
|
|
|
immediately.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type blocking: bool
|
|
|
|
"""
|
|
|
|
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
cast.disconnect(timeout=timeout, blocking=blocking)
|
|
|
|
|
|
|
|
@action
|
2019-06-27 23:52:40 +02:00
|
|
|
def join(self, chromecast=None, timeout=None):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
Blocks the thread until the Chromecast connection is terminated.
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
|
|
|
|
:param timeout: Number of seconds to wait for disconnection (default: None: block until termination)
|
|
|
|
:type timeout: float
|
|
|
|
"""
|
|
|
|
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.join(timeout=timeout)
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def quit(self, chromecast=None):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
Exits the current app on the Chromecast
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
"""
|
|
|
|
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
cast.quit_app()
|
|
|
|
|
|
|
|
@action
|
|
|
|
def reboot(self, chromecast=None):
|
|
|
|
"""
|
|
|
|
Reboots the Chromecast
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
"""
|
|
|
|
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
cast.reboot()
|
|
|
|
|
|
|
|
@action
|
|
|
|
def set_volume(self, volume, chromecast=None):
|
|
|
|
"""
|
|
|
|
Set the Chromecast volume
|
|
|
|
|
|
|
|
:param volume: Volume to be set, between 0 and 100
|
|
|
|
:type volume: float
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
"""
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
cast.set_volume(volume/100)
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.wait()
|
|
|
|
status = self.status(chromecast=chromecast)
|
|
|
|
status.output['volume'] = volume
|
|
|
|
return status
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def volup(self, chromecast=None, step=10):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
2019-02-05 02:30:20 +01:00
|
|
|
Turn up the Chromecast volume by 10% or step.
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
:param step: Volume increment between 0 and 100 (default: 100%)
|
|
|
|
:type step: float
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2019-02-05 02:30:20 +01:00
|
|
|
step /= 100
|
|
|
|
cast.volume_up(min(step, 1))
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.wait()
|
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def voldown(self, chromecast=None, step=10):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
2019-02-05 02:30:20 +01:00
|
|
|
Turn down the Chromecast volume by 10% or step.
|
2018-11-12 16:50:20 +01:00
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
|
2019-02-05 02:30:20 +01:00
|
|
|
:param step: Volume decrement between 0 and 100 (default: 100%)
|
|
|
|
:type step: float
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2019-02-05 02:30:20 +01:00
|
|
|
step /= 100
|
|
|
|
cast.volume_down(max(step, 0))
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.wait()
|
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
@action
|
2019-02-05 02:30:20 +01:00
|
|
|
def mute(self, chromecast=None):
|
2018-11-12 16:50:20 +01:00
|
|
|
"""
|
|
|
|
Toggle the mute status on the Chromecast
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
:param chromecast: Chromecast to cast to. If none is specified, then the default configured Chromecast
|
|
|
|
will be used.
|
2018-11-12 16:50:20 +01:00
|
|
|
:type chromecast: str
|
|
|
|
"""
|
|
|
|
|
2019-06-27 23:52:40 +02:00
|
|
|
chromecast = chromecast or self.chromecast
|
2018-11-13 01:29:24 +01:00
|
|
|
cast = self.get_chromecast(chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
cast.set_volume_muted(not cast.status.volume_muted)
|
2019-06-27 23:52:40 +02:00
|
|
|
cast.wait()
|
|
|
|
return self.status(chromecast=chromecast)
|
2018-11-12 16:50:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|