mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Merge remote-tracking branch 'origin/master' into dev
This commit is contained in:
commit
ef0eb8be02
@ -41,6 +41,7 @@ ATTR_MEDIA_DURATION = 'media_duration'
|
|||||||
|
|
||||||
MEDIA_STATE_UNKNOWN = 'unknown'
|
MEDIA_STATE_UNKNOWN = 'unknown'
|
||||||
MEDIA_STATE_PLAYING = 'playing'
|
MEDIA_STATE_PLAYING = 'playing'
|
||||||
|
MEDIA_STATE_PAUSED = 'paused'
|
||||||
MEDIA_STATE_STOPPED = 'stopped'
|
MEDIA_STATE_STOPPED = 'stopped'
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,15 +10,18 @@ import logging
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import pychromecast
|
import pychromecast
|
||||||
|
import pychromecast.controllers.youtube as youtube
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# We will throw error later
|
# We will throw error later
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# ATTR_MEDIA_ALBUM, ATTR_MEDIA_IMAGE_URL,
|
||||||
|
# ATTR_MEDIA_TITLE, ATTR_MEDIA_ARTIST,
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
MediaPlayerDevice, STATE_NO_APP, ATTR_MEDIA_STATE,
|
MediaPlayerDevice, STATE_NO_APP, ATTR_MEDIA_STATE,
|
||||||
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_TITLE, ATTR_MEDIA_ARTIST,
|
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_DURATION, ATTR_MEDIA_VOLUME,
|
||||||
ATTR_MEDIA_ALBUM, ATTR_MEDIA_IMAGE_URL, ATTR_MEDIA_DURATION,
|
MEDIA_STATE_PLAYING, MEDIA_STATE_PAUSED, MEDIA_STATE_STOPPED,
|
||||||
ATTR_MEDIA_VOLUME, MEDIA_STATE_PLAYING, MEDIA_STATE_STOPPED)
|
MEDIA_STATE_UNKNOWN)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
@ -57,7 +60,9 @@ class CastDevice(MediaPlayerDevice):
|
|||||||
""" Represents a Cast device on the network. """
|
""" Represents a Cast device on the network. """
|
||||||
|
|
||||||
def __init__(self, host):
|
def __init__(self, host):
|
||||||
self.cast = pychromecast.PyChromecast(host)
|
self.cast = pychromecast.Chromecast(host)
|
||||||
|
self.youtube = youtube.YouTubeController()
|
||||||
|
self.cast.register_handler(self.youtube)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -67,45 +72,45 @@ class CastDevice(MediaPlayerDevice):
|
|||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
""" Returns the state of the device. """
|
""" Returns the state of the device. """
|
||||||
status = self.cast.app
|
if self.cast.is_idle:
|
||||||
|
|
||||||
if status is None or status.app_id == pychromecast.APP_ID['HOME']:
|
|
||||||
return STATE_NO_APP
|
return STATE_NO_APP
|
||||||
else:
|
else:
|
||||||
return status.description
|
return self.cast.app_display_name
|
||||||
|
|
||||||
|
@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:
|
||||||
|
return MEDIA_STATE_UNKNOWN
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
""" Returns the state attributes. """
|
""" Returns the state attributes. """
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
cast_status = self.cast.status
|
||||||
|
media_status = self.cast.media_status
|
||||||
|
|
||||||
if ramp and ramp.state != pychromecast.RAMP_STATE_UNKNOWN:
|
state_attr = {
|
||||||
state_attr = {}
|
ATTR_MEDIA_STATE: self.media_state,
|
||||||
|
'application_id': self.cast.app_id,
|
||||||
|
}
|
||||||
|
|
||||||
if ramp.state == pychromecast.RAMP_STATE_PLAYING:
|
if cast_status:
|
||||||
state_attr[ATTR_MEDIA_STATE] = MEDIA_STATE_PLAYING
|
state_attr[ATTR_MEDIA_VOLUME] = cast_status.volume_level,
|
||||||
else:
|
|
||||||
state_attr[ATTR_MEDIA_STATE] = MEDIA_STATE_STOPPED
|
|
||||||
|
|
||||||
if ramp.content_id:
|
if media_status:
|
||||||
state_attr[ATTR_MEDIA_CONTENT_ID] = ramp.content_id
|
if media_status.content_id:
|
||||||
|
state_attr[ATTR_MEDIA_CONTENT_ID] = media_status.content_id
|
||||||
|
|
||||||
if ramp.title:
|
if media_status.duration:
|
||||||
state_attr[ATTR_MEDIA_TITLE] = ramp.title
|
state_attr[ATTR_MEDIA_DURATION] = media_status.duration
|
||||||
|
|
||||||
if ramp.artist:
|
|
||||||
state_attr[ATTR_MEDIA_ARTIST] = ramp.artist
|
|
||||||
|
|
||||||
if ramp.album:
|
|
||||||
state_attr[ATTR_MEDIA_ALBUM] = ramp.album
|
|
||||||
|
|
||||||
if ramp.image_url:
|
|
||||||
state_attr[ATTR_MEDIA_IMAGE_URL] = ramp.image_url
|
|
||||||
|
|
||||||
if ramp.duration:
|
|
||||||
state_attr[ATTR_MEDIA_DURATION] = ramp.duration
|
|
||||||
|
|
||||||
state_attr[ATTR_MEDIA_VOLUME] = ramp.volume
|
|
||||||
|
|
||||||
return state_attr
|
return state_attr
|
||||||
|
|
||||||
@ -117,46 +122,31 @@ class CastDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
def volume_up(self):
|
def volume_up(self):
|
||||||
""" Service to send the chromecast the command for volume up. """
|
""" Service to send the chromecast the command for volume up. """
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
self.cast.volume_up()
|
||||||
|
|
||||||
if ramp:
|
|
||||||
ramp.volume_up()
|
|
||||||
|
|
||||||
def volume_down(self):
|
def volume_down(self):
|
||||||
""" Service to send the chromecast the command for volume down. """
|
""" Service to send the chromecast the command for volume down. """
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
self.cast.volume_down()
|
||||||
|
|
||||||
if ramp:
|
|
||||||
ramp.volume_down()
|
|
||||||
|
|
||||||
def media_play_pause(self):
|
def media_play_pause(self):
|
||||||
""" Service to send the chromecast the command for play/pause. """
|
""" Service to send the chromecast the command for play/pause. """
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
media_state = self.media_state
|
||||||
|
|
||||||
if ramp:
|
if media_state in (MEDIA_STATE_STOPPED, MEDIA_STATE_PAUSED):
|
||||||
ramp.playpause()
|
self.cast.media_controller.play()
|
||||||
|
elif media_state == MEDIA_STATE_PLAYING:
|
||||||
|
self.cast.media_controller.pause()
|
||||||
|
|
||||||
def media_play(self):
|
def media_play(self):
|
||||||
""" Service to send the chromecast the command for play/pause. """
|
""" Service to send the chromecast the command for play/pause. """
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
if self.media_state in (MEDIA_STATE_STOPPED, MEDIA_STATE_PAUSED):
|
||||||
|
self.cast.media_controller.play()
|
||||||
if ramp and ramp.state == pychromecast.RAMP_STATE_STOPPED:
|
|
||||||
ramp.playpause()
|
|
||||||
|
|
||||||
def media_pause(self):
|
def media_pause(self):
|
||||||
""" Service to send the chromecast the command for play/pause. """
|
""" Service to send the chromecast the command for play/pause. """
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
if self.media_state == MEDIA_STATE_PLAYING:
|
||||||
|
self.cast.media_controller.pause()
|
||||||
if ramp and ramp.state == pychromecast.RAMP_STATE_PLAYING:
|
|
||||||
ramp.playpause()
|
|
||||||
|
|
||||||
def media_next_track(self):
|
|
||||||
""" Service to send the chromecast the command for next track. """
|
|
||||||
ramp = self.cast.get_protocol(pychromecast.PROTOCOL_RAMP)
|
|
||||||
|
|
||||||
if ramp:
|
|
||||||
ramp.next()
|
|
||||||
|
|
||||||
def play_youtube_video(self, video_id):
|
def play_youtube_video(self, video_id):
|
||||||
""" Plays specified video_id on the Chromecast's YouTube channel. """
|
""" Plays specified video_id on the Chromecast's YouTube channel. """
|
||||||
pychromecast.play_youtube_video(video_id, self.cast.host)
|
self.youtube.play_video(video_id)
|
||||||
|
@ -18,7 +18,7 @@ phue>=0.8
|
|||||||
ledcontroller>=1.0.7
|
ledcontroller>=1.0.7
|
||||||
|
|
||||||
# media_player.cast
|
# media_player.cast
|
||||||
pychromecast>=0.5
|
pychromecast>=0.6
|
||||||
|
|
||||||
# keyboard
|
# keyboard
|
||||||
pyuserinput>=0.1.9
|
pyuserinput>=0.1.9
|
||||||
|
Loading…
x
Reference in New Issue
Block a user