core/homeassistant/components/media_player/cast.py

161 lines
4.9 KiB
Python
Raw Normal View History

"""
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
except ImportError:
# We will throw error later
pass
from homeassistant.const import ATTR_ENTITY_PICTURE
2015-05-30 05:36:40 +00:00
# ATTR_MEDIA_ALBUM, ATTR_MEDIA_IMAGE_URL,
# ATTR_MEDIA_ARTIST,
from homeassistant.components.media_player import (
MediaPlayerDevice, STATE_NO_APP, ATTR_MEDIA_STATE, ATTR_MEDIA_TITLE,
2015-05-30 05:36:40 +00:00
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_DURATION, ATTR_MEDIA_VOLUME,
MEDIA_STATE_PLAYING, MEDIA_STATE_PAUSED, MEDIA_STATE_STOPPED,
MEDIA_STATE_UNKNOWN)
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the cast platform. """
logger = logging.getLogger(__name__)
try:
# pylint: disable=redefined-outer-name
import pychromecast
except ImportError:
logger.exception(("Failed to import pychromecast. "
"Did you maybe not install the 'pychromecast' "
"dependency?"))
return
if discovery_info:
hosts = [discovery_info[0]]
else:
hosts = pychromecast.discover_chromecasts()
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. """
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)
@property
def name(self):
""" Returns the name of the device. """
return self.cast.device.friendly_name
@property
def state(self):
""" Returns the state of the device. """
2015-05-30 05:36:40 +00:00
if self.cast.is_idle:
return STATE_NO_APP
else:
2015-05-29 20:19:42 +00:00
return self.cast.app_display_name
2015-05-30 05:36:40 +00:00
@property
def media_state(self):
""" Returns the media state. """
media_controller = self.cast.media_controller
if media_controller.is_playing:
return MEDIA_STATE_PLAYING
elif media_controller.is_paused:
return MEDIA_STATE_PAUSED
elif media_controller.is_idle:
return MEDIA_STATE_STOPPED
else:
2015-05-30 05:36:40 +00:00
return MEDIA_STATE_UNKNOWN
@property
def state_attributes(self):
""" Returns the state attributes. """
2015-05-30 05:36:40 +00:00
cast_status = self.cast.status
media_controller = self.cast.media_controller
media_status = media_controller.status
2015-05-29 20:19:42 +00:00
2015-05-30 05:36:40 +00:00
state_attr = {
ATTR_MEDIA_STATE: self.media_state,
'application_id': self.cast.app_id,
}
2015-05-30 05:36:40 +00:00
if cast_status:
state_attr[ATTR_MEDIA_VOLUME] = cast_status.volume_level,
if media_status.content_id:
state_attr[ATTR_MEDIA_CONTENT_ID] = media_status.content_id
if media_status.duration:
state_attr[ATTR_MEDIA_DURATION] = media_status.duration
if media_controller.title:
state_attr[ATTR_MEDIA_TITLE] = media_controller.title
if media_controller.thumbnail:
state_attr[ATTR_ENTITY_PICTURE] = media_controller.thumbnail
2015-05-30 05:36:40 +00:00
return state_attr
def turn_off(self):
""" Service to exit any running app on the specimedia player ChromeCast and
shows idle screen. Will quit all ChromeCasts if nothing specified.
"""
self.cast.quit_app()
def volume_up(self):
""" Service to send the chromecast the command for volume up. """
2015-05-29 20:19:42 +00:00
self.cast.volume_up()
def volume_down(self):
""" Service to send the chromecast the command for volume down. """
2015-05-29 20:19:42 +00:00
self.cast.volume_down()
def media_play_pause(self):
""" Service to send the chromecast the command for play/pause. """
2015-05-30 05:36:40 +00:00
media_state = self.media_state
if media_state in (MEDIA_STATE_STOPPED, MEDIA_STATE_PAUSED):
2015-05-29 20:31:52 +00:00
self.cast.media_controller.play()
2015-05-29 20:19:42 +00:00
elif media_state == MEDIA_STATE_PLAYING:
2015-05-29 20:31:52 +00:00
self.cast.media_controller.pause()
def media_play(self):
""" Service to send the chromecast the command for play/pause. """
2015-05-30 05:36:40 +00:00
if self.media_state in (MEDIA_STATE_STOPPED, MEDIA_STATE_PAUSED):
self.cast.media_controller.play()
def media_pause(self):
""" Service to send the chromecast the command for play/pause. """
2015-05-30 05:36:40 +00:00
if self.media_state == MEDIA_STATE_PLAYING:
self.cast.media_controller.pause()
def play_youtube_video(self, video_id):
""" Plays specified video_id on the Chromecast's YouTube channel. """
2015-05-30 05:36:40 +00:00
self.youtube.play_video(video_id)