Improve support for Yamaha receiver

* Playback (play, pause, stop, next, previous)

* Media title, artist and album
This commit is contained in:
Pierre Ståhl 2016-10-22 09:11:50 +02:00
parent 0dfcf40d37
commit 297a6f6f03
2 changed files with 79 additions and 18 deletions

View file

@ -10,13 +10,15 @@ import voluptuous as vol
from homeassistant.components.media_player import (
SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,
SUPPORT_SELECT_SOURCE, SUPPORT_PLAY_MEDIA,
SUPPORT_SELECT_SOURCE, SUPPORT_PLAY_MEDIA, SUPPORT_PAUSE, SUPPORT_STOP,
SUPPORT_NEXT_TRACK, SUPPORT_PREVIOUS_TRACK,
MEDIA_TYPE_MUSIC,
MediaPlayerDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, CONF_HOST, STATE_OFF, STATE_ON)
from homeassistant.const import (CONF_NAME, CONF_HOST, STATE_OFF, STATE_ON,
STATE_PLAYING, STATE_IDLE)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['rxv==0.2.0']
REQUIREMENTS = ['rxv==0.3.0']
_LOGGER = logging.getLogger(__name__)
@ -24,6 +26,10 @@ SUPPORT_YAMAHA = SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE | \
SUPPORT_PLAY_MEDIA
# Only supported by some sources
SUPPORT_PLAYBACK = SUPPORT_PLAY_MEDIA | SUPPORT_PAUSE | SUPPORT_STOP | \
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK
CONF_SOURCE_NAMES = 'source_names'
CONF_SOURCE_IGNORE = 'source_ignore'
@ -76,16 +82,25 @@ class YamahaDevice(MediaPlayerDevice):
self._source_ignore = source_ignore
self._source_names = source_names
self._reverse_mapping = None
self._is_playback_supported = False
self._play_status = None
self.update()
self._name = name
self._zone = receiver.zone
def update(self):
"""Get the latest details from the device."""
self._play_status = self._receiver.play_status()
if self._receiver.on:
self._pwstate = STATE_ON
if self._play_status is None:
self._pwstate = STATE_ON
elif self._play_status.playing:
self._pwstate = STATE_PLAYING
else:
self._pwstate = STATE_IDLE
else:
self._pwstate = STATE_OFF
self._muted = self._receiver.mute
self._volume = (self._receiver.volume / 100) + 1
@ -95,6 +110,8 @@ class YamahaDevice(MediaPlayerDevice):
current_source = self._receiver.input
self._current_source = self._source_names.get(
current_source, current_source)
self._is_playback_supported = self._receiver.is_playback_supported(
self._current_source)
def build_source_list(self):
"""Build the source list."""
@ -143,7 +160,10 @@ class YamahaDevice(MediaPlayerDevice):
@property
def supported_media_commands(self):
"""Flag of media commands that are supported."""
return SUPPORT_YAMAHA
supported_commands = SUPPORT_YAMAHA
if self._is_playback_supported:
supported_commands |= SUPPORT_PLAYBACK
return supported_commands
def turn_off(self):
"""Turn off media player."""
@ -164,6 +184,34 @@ class YamahaDevice(MediaPlayerDevice):
self._receiver.on = True
self._volume = (self._receiver.volume / 100) + 1
def media_play(self):
"""Send play commmand."""
self._call_playback_function(self._receiver.play, "play")
def media_pause(self):
"""Send pause command."""
self._call_playback_function(self._receiver.pause, "pause")
def media_stop(self):
"""Send stop command."""
self._call_playback_function(self._receiver.stop, "stop")
def media_previous_track(self):
"""Send previous track command."""
self._call_playback_function(self._receiver.previous, "previous track")
def media_next_track(self):
"""Send next track command."""
self._call_playback_function(self._receiver.next, "next track")
def _call_playback_function(self, function, function_text):
import rxv
try:
function()
except rxv.exceptions.ResponseException:
_LOGGER.warning(
'Failed to execute %s on %s', function_text, self._name)
def select_source(self, source):
"""Select input source."""
self._receiver.input = self._reverse_mapping.get(source, source)
@ -179,23 +227,36 @@ class YamahaDevice(MediaPlayerDevice):
if media_type == "NET RADIO":
self._receiver.net_radio(media_id)
@property
def media_artist(self):
"""Artist of current playing media."""
if self._play_status is not None:
return self._play_status.artist
@property
def media_album_name(self):
"""Album of current playing media."""
if self._play_status is not None:
return self._play_status.album
@property
def media_content_type(self):
"""Return the media content type."""
if self.source == "NET RADIO":
"""Content type of current playing media."""
# Loose assumption that if playback is supported, we are playing music
if self._is_playback_supported:
return MEDIA_TYPE_MUSIC
return None
@property
def media_title(self):
"""Return the media title.
"""Artist of current playing media."""
if self._play_status is not None:
song = self._play_status.song
station = self._play_status.station
This will vary by input source, as they provide different
information in metadata.
"""
if self.source == "NET RADIO":
info = self._receiver.play_status()
if info.song:
return "%s: %s" % (info.station, info.song)
# If both song and station is available, print both, otherwise
# just the one we have.
if song and station:
return '{}: {}'.format(station, song)
else:
return info.station
return song or station

View file

@ -447,7 +447,7 @@ radiotherm==1.2
# rpi-rf==0.9.5
# homeassistant.components.media_player.yamaha
rxv==0.2.0
rxv==0.3.0
# homeassistant.components.media_player.samsungtv
samsungctl==0.5.1