2015-03-04 07:50:54 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.media_player.chromecast
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to interact with Cast devices on the network.
|
|
|
|
|
|
|
|
WARNING: This platform is currently not working due to a changed Cast API
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
try:
|
|
|
|
import pychromecast
|
2015-05-29 20:19:42 +00:00
|
|
|
import pychromecast.controllers.youtube as youtube
|
2015-03-04 07:50:54 +00:00
|
|
|
except ImportError:
|
2015-05-30 17:54:19 +00:00
|
|
|
pychromecast = None
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_PLAYING, STATE_PAUSED, STATE_IDLE, STATE_OFF,
|
|
|
|
STATE_UNKNOWN)
|
2015-05-30 07:52:33 +00:00
|
|
|
|
2015-03-04 07:50:54 +00:00
|
|
|
from homeassistant.components.media_player import (
|
2015-06-03 22:17:03 +00:00
|
|
|
MediaPlayerDevice,
|
|
|
|
SUPPORT_PAUSE, SUPPORT_VOLUME_SET, SUPPORT_VOLUME_MUTE,
|
2015-06-09 05:49:32 +00:00
|
|
|
SUPPORT_TURN_ON, SUPPORT_TURN_OFF, SUPPORT_YOUTUBE,
|
|
|
|
SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK,
|
|
|
|
MEDIA_TYPE_MUSIC, MEDIA_TYPE_TVSHOW, MEDIA_TYPE_VIDEO)
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-05-31 07:38:14 +00:00
|
|
|
CAST_SPLASH = 'https://home-assistant.io/images/cast/splash.png'
|
2015-06-09 05:49:32 +00:00
|
|
|
SUPPORT_CAST = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
|
|
|
|
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_PREVIOUS_TRACK | \
|
|
|
|
SUPPORT_NEXT_TRACK | SUPPORT_YOUTUBE
|
2015-05-31 07:38:14 +00:00
|
|
|
|
2015-03-04 07:50:54 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
""" Sets up the cast platform. """
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-05-30 17:54:19 +00:00
|
|
|
if pychromecast is None:
|
|
|
|
logger.error((
|
|
|
|
"Failed to import pychromecast. Did you maybe not install the "
|
|
|
|
"'pychromecast' dependency?"))
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-05-30 17:54:19 +00:00
|
|
|
return False
|
2015-03-04 07:50:54 +00:00
|
|
|
|
|
|
|
if discovery_info:
|
|
|
|
hosts = [discovery_info[0]]
|
|
|
|
|
|
|
|
else:
|
2015-05-30 18:41:39 +00:00
|
|
|
hosts = (host_port[0] for host_port
|
|
|
|
in pychromecast.discover_chromecasts())
|
2015-03-04 07:50:54 +00:00
|
|
|
|
|
|
|
casts = []
|
|
|
|
|
|
|
|
for host in hosts:
|
|
|
|
try:
|
|
|
|
casts.append(CastDevice(host))
|
|
|
|
except pychromecast.ChromecastConnectionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
add_devices(casts)
|
|
|
|
|
|
|
|
|
|
|
|
class CastDevice(MediaPlayerDevice):
|
|
|
|
""" Represents a Cast device on the network. """
|
|
|
|
|
2015-06-09 05:49:32 +00:00
|
|
|
# pylint: disable=too-many-public-methods
|
|
|
|
|
2015-03-04 07:50:54 +00:00
|
|
|
def __init__(self, host):
|
2015-05-29 20:19:42 +00:00
|
|
|
self.cast = pychromecast.Chromecast(host)
|
|
|
|
self.youtube = youtube.YouTubeController()
|
|
|
|
self.cast.register_handler(self.youtube)
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-05-30 19:35:35 +00:00
|
|
|
self.cast.socket_client.receiver_controller.register_status_listener(
|
|
|
|
self)
|
|
|
|
self.cast.socket_client.media_controller.register_status_listener(self)
|
|
|
|
|
|
|
|
self.cast_status = self.cast.status
|
|
|
|
self.media_status = self.cast.media_controller.status
|
|
|
|
|
2015-06-09 05:49:32 +00:00
|
|
|
# Entity properties and methods
|
2015-06-03 22:17:03 +00:00
|
|
|
|
2015-05-30 19:35:35 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
return False
|
|
|
|
|
2015-03-04 07:50:54 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the device. """
|
|
|
|
return self.cast.device.friendly_name
|
|
|
|
|
2015-06-09 05:49:32 +00:00
|
|
|
# MediaPlayerDevice properties and methods
|
2015-05-30 05:36:40 +00:00
|
|
|
|
|
|
|
@property
|
2015-06-03 22:17:03 +00:00
|
|
|
def state(self):
|
|
|
|
""" State of the player. """
|
2015-06-09 05:49:32 +00:00
|
|
|
if self.media_status is None:
|
|
|
|
return STATE_UNKNOWN
|
|
|
|
elif self.media_status.player_is_playing:
|
2015-06-03 22:17:03 +00:00
|
|
|
return STATE_PLAYING
|
2015-06-09 05:49:32 +00:00
|
|
|
elif self.media_status.player_is_paused:
|
2015-06-03 22:17:03 +00:00
|
|
|
return STATE_PAUSED
|
2015-06-09 05:49:32 +00:00
|
|
|
elif self.media_status.player_is_idle:
|
2015-06-03 22:17:03 +00:00
|
|
|
return STATE_IDLE
|
|
|
|
elif self.cast.is_idle:
|
|
|
|
return STATE_OFF
|
2015-03-04 07:50:54 +00:00
|
|
|
else:
|
2015-06-03 22:17:03 +00:00
|
|
|
return STATE_UNKNOWN
|
2015-03-04 07:50:54 +00:00
|
|
|
|
|
|
|
@property
|
2015-06-03 22:17:03 +00:00
|
|
|
def volume_level(self):
|
|
|
|
""" Volume level of the media player (0..1). """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.cast_status.volume_level if self.cast_status else None
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
""" Boolean if volume is currently muted. """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.cast_status.volume_muted if self.cast_status else None
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_content_id(self):
|
|
|
|
""" Content ID of current playing media. """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.content_id if self.media_status else None
|
2015-05-29 20:19:42 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
|
|
|
""" Content type of current playing media. """
|
2015-06-09 05:49:32 +00:00
|
|
|
if self.media_status is None:
|
|
|
|
return None
|
|
|
|
elif self.media_status.media_is_tvshow:
|
|
|
|
return MEDIA_TYPE_TVSHOW
|
|
|
|
elif self.media_status.media_is_movie:
|
|
|
|
return MEDIA_TYPE_VIDEO
|
|
|
|
elif self.media_status.media_is_musictrack:
|
|
|
|
return MEDIA_TYPE_MUSIC
|
2015-06-03 22:17:03 +00:00
|
|
|
return None
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_duration(self):
|
|
|
|
""" Duration of current playing media in seconds. """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.duration if self.media_status else None
|
2015-05-31 20:27:59 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_image_url(self):
|
|
|
|
""" Image url of current playing media. """
|
2015-06-09 05:49:32 +00:00
|
|
|
if self.media_status is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
images = self.media_status.images
|
|
|
|
|
|
|
|
return images[0].url if images else None
|
2015-05-30 07:52:33 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_title(self):
|
|
|
|
""" Title of current playing media. """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.title if self.media_status else None
|
2015-05-30 07:52:33 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_artist(self):
|
|
|
|
""" Artist of current playing media. (Music track only) """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.artist if self.media_status else None
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_album(self):
|
|
|
|
""" Album of current playing media. (Music track only) """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.album_name if self.media_status else None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_album_artist(self):
|
|
|
|
""" Album arist of current playing media. (Music track only) """
|
|
|
|
return self.media_status.album_artist if self.media_status else None
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
@property
|
|
|
|
def media_track(self):
|
|
|
|
""" Track number of current playing media. (Music track only) """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.track if self.media_status else None
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_series_title(self):
|
|
|
|
""" Series title of current playing media. (TV Show only)"""
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.series_title if self.media_status else None
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_season(self):
|
|
|
|
""" Season of current playing media. (TV Show only) """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.season if self.media_status else None
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_episode(self):
|
|
|
|
""" Episode of current playing media. (TV Show only) """
|
2015-06-09 05:49:32 +00:00
|
|
|
return self.media_status.episode if self.media_status else None
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def app_id(self):
|
|
|
|
""" ID of the current running app. """
|
|
|
|
return self.cast.app_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def app_name(self):
|
|
|
|
""" Name of the current running app. """
|
|
|
|
return self.cast.app_display_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_media_commands(self):
|
|
|
|
""" Flags of media commands that are supported. """
|
2015-06-09 05:49:32 +00:00
|
|
|
return SUPPORT_CAST
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-05-31 07:38:14 +00:00
|
|
|
def turn_on(self):
|
|
|
|
""" Turns on the ChromeCast. """
|
|
|
|
# The only way we can turn the Chromecast is on is by launching an app
|
2015-05-31 08:29:28 +00:00
|
|
|
if not self.cast.status or not self.cast.status.is_active_input:
|
|
|
|
if self.cast.app_id:
|
|
|
|
self.cast.quit_app()
|
|
|
|
|
2015-05-31 07:38:14 +00:00
|
|
|
self.cast.play_media(
|
|
|
|
CAST_SPLASH, pychromecast.STREAM_TYPE_BUFFERED)
|
|
|
|
|
2015-03-04 07:50:54 +00:00
|
|
|
def turn_off(self):
|
2015-06-09 05:49:32 +00:00
|
|
|
""" Turns Chromecast off. """
|
2015-03-04 07:50:54 +00:00
|
|
|
self.cast.quit_app()
|
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
def mute_volume(self, mute):
|
|
|
|
""" mute the volume. """
|
2015-05-31 20:27:59 +00:00
|
|
|
self.cast.set_volume_muted(mute)
|
2015-05-31 11:36:28 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
def set_volume_level(self, volume):
|
|
|
|
""" set volume level, range 0..1. """
|
2015-05-31 11:36:28 +00:00
|
|
|
self.cast.set_volume(volume)
|
|
|
|
|
2015-03-04 07:50:54 +00:00
|
|
|
def media_play(self):
|
2015-06-03 22:17:03 +00:00
|
|
|
""" Send play commmand. """
|
|
|
|
self.cast.media_controller.play()
|
2015-03-04 07:50:54 +00:00
|
|
|
|
|
|
|
def media_pause(self):
|
2015-06-03 22:17:03 +00:00
|
|
|
""" Send pause command. """
|
|
|
|
self.cast.media_controller.pause()
|
2015-03-04 07:50:54 +00:00
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
def media_previous_track(self):
|
|
|
|
""" Send previous track command. """
|
2015-05-31 07:38:14 +00:00
|
|
|
self.cast.media_controller.rewind()
|
|
|
|
|
|
|
|
def media_next_track(self):
|
2015-06-03 22:17:03 +00:00
|
|
|
""" Send next track command. """
|
2015-05-31 07:38:14 +00:00
|
|
|
self.cast.media_controller.skip()
|
|
|
|
|
2015-06-03 22:17:03 +00:00
|
|
|
def media_seek(self, position):
|
|
|
|
""" Seek the media to a specific location. """
|
2015-06-09 05:49:32 +00:00
|
|
|
self.cast.media_controller.seek(position)
|
2015-06-03 22:17:03 +00:00
|
|
|
|
|
|
|
def play_youtube(self, media_id):
|
|
|
|
""" Plays a YouTube media. """
|
|
|
|
self.youtube.play_video(media_id)
|
|
|
|
|
2015-06-09 05:49:32 +00:00
|
|
|
# implementation of chromecast status_listener methods
|
2015-05-30 19:35:35 +00:00
|
|
|
|
|
|
|
def new_cast_status(self, status):
|
|
|
|
""" Called when a new cast status is received. """
|
|
|
|
self.cast_status = status
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def new_media_status(self, status):
|
|
|
|
""" Called when a new media status is received. """
|
|
|
|
self.media_status = status
|
2015-06-09 05:49:32 +00:00
|
|
|
self.update_ha_state()
|