2016-06-25 07:06:36 +00:00
|
|
|
"""
|
|
|
|
Support for interacting with and controlling the cmus music player.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2016-06-30 08:33:34 +00:00
|
|
|
https://home-assistant.io/components/media_player.cmus/
|
2016-06-25 07:06:36 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-09-04 02:18:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
|
2016-06-25 07:06:36 +00:00
|
|
|
from homeassistant.components.media_player import (
|
|
|
|
MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE,
|
2017-01-09 00:09:30 +00:00
|
|
|
SUPPORT_PREVIOUS_TRACK, SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_PLAY,
|
2016-09-04 02:18:11 +00:00
|
|
|
SUPPORT_VOLUME_SET, SUPPORT_PLAY_MEDIA, SUPPORT_SEEK, PLATFORM_SCHEMA,
|
2016-06-25 07:06:36 +00:00
|
|
|
MediaPlayerDevice)
|
2016-09-04 02:18:11 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_OFF, STATE_PAUSED, STATE_PLAYING, CONF_HOST, CONF_NAME, CONF_PORT,
|
|
|
|
CONF_PASSWORD)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-06-25 07:06:36 +00:00
|
|
|
|
2016-06-27 16:02:45 +00:00
|
|
|
REQUIREMENTS = ['pycmus==0.1.0']
|
2016-06-25 07:06:36 +00:00
|
|
|
|
2016-09-04 02:18:11 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'cmus'
|
|
|
|
DEFAULT_PORT = 3000
|
|
|
|
|
2016-06-25 07:06:36 +00:00
|
|
|
SUPPORT_CMUS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
|
|
|
|
SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
|
2017-01-09 00:09:30 +00:00
|
|
|
SUPPORT_PLAY_MEDIA | SUPPORT_SEEK | SUPPORT_PLAY
|
2016-06-25 07:06:36 +00:00
|
|
|
|
2016-09-04 02:18:11 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Inclusive(CONF_HOST, 'remote'): cv.string,
|
|
|
|
vol.Inclusive(CONF_PASSWORD, 'remote'): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2016-06-25 07:06:36 +00:00
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discover_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the CMUS platform."""
|
2016-06-25 07:06:36 +00:00
|
|
|
from pycmus import exceptions
|
|
|
|
|
2016-09-04 02:18:11 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
port = config.get(CONF_PORT)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
2016-06-25 07:06:36 +00:00
|
|
|
try:
|
|
|
|
cmus_remote = CmusDevice(host, password, port, name)
|
|
|
|
except exceptions.InvalidPassword:
|
|
|
|
_LOGGER.error("The provided password was rejected by cmus")
|
|
|
|
return False
|
2017-08-08 18:21:33 +00:00
|
|
|
add_devices([cmus_remote], True)
|
2016-06-25 07:06:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CmusDevice(MediaPlayerDevice):
|
2016-09-04 02:18:11 +00:00
|
|
|
"""Representation of a running cmus."""
|
2016-06-25 07:06:36 +00:00
|
|
|
|
2016-11-04 01:32:14 +00:00
|
|
|
# pylint: disable=no-member
|
2016-06-25 07:06:36 +00:00
|
|
|
def __init__(self, server, password, port, name):
|
|
|
|
"""Initialize the CMUS device."""
|
|
|
|
from pycmus import remote
|
|
|
|
|
|
|
|
if server:
|
2016-09-04 02:18:11 +00:00
|
|
|
self.cmus = remote.PyCmus(
|
|
|
|
server=server, password=password, port=port)
|
|
|
|
auto_name = 'cmus-{}'.format(server)
|
2016-06-25 07:06:36 +00:00
|
|
|
else:
|
|
|
|
self.cmus = remote.PyCmus()
|
2016-09-04 02:18:11 +00:00
|
|
|
auto_name = 'cmus-local'
|
2016-06-25 07:06:36 +00:00
|
|
|
self._name = name or auto_name
|
|
|
|
self.status = {}
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and update the state."""
|
|
|
|
status = self.cmus.get_status_dict()
|
|
|
|
if not status:
|
2018-01-27 19:58:27 +00:00
|
|
|
_LOGGER.warning("Received no status from cmus")
|
2016-06-25 07:06:36 +00:00
|
|
|
else:
|
|
|
|
self.status = status
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the media state."""
|
2017-02-16 03:13:25 +00:00
|
|
|
if self.status.get('status') == 'playing':
|
2016-06-25 07:06:36 +00:00
|
|
|
return STATE_PLAYING
|
2017-02-16 03:13:25 +00:00
|
|
|
elif self.status.get('status') == 'paused':
|
2016-06-25 07:06:36 +00:00
|
|
|
return STATE_PAUSED
|
2017-07-06 06:30:01 +00:00
|
|
|
return STATE_OFF
|
2016-06-25 07:06:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_content_id(self):
|
|
|
|
"""Content ID of current playing media."""
|
|
|
|
return self.status.get('file')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content_type(self):
|
|
|
|
"""Content type of the current playing media."""
|
|
|
|
return MEDIA_TYPE_MUSIC
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_duration(self):
|
|
|
|
"""Duration of current playing media in seconds."""
|
|
|
|
return self.status.get('duration')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_title(self):
|
|
|
|
"""Title of current playing media."""
|
|
|
|
return self.status['tag'].get('title')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_artist(self):
|
|
|
|
"""Artist of current playing media, music track only."""
|
|
|
|
return self.status['tag'].get('artist')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_track(self):
|
|
|
|
"""Track number of current playing media, music track only."""
|
|
|
|
return self.status['tag'].get('tracknumber')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_album_name(self):
|
|
|
|
"""Album name of current playing media, music track only."""
|
|
|
|
return self.status['tag'].get('album')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_album_artist(self):
|
|
|
|
"""Album artist of current playing media, music track only."""
|
|
|
|
return self.status['tag'].get('albumartist')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Return the volume level."""
|
|
|
|
left = self.status['set'].get('vol_left')[0]
|
|
|
|
right = self.status['set'].get('vol_right')[0]
|
|
|
|
if left != right:
|
|
|
|
volume = float(left + right) / 2
|
|
|
|
else:
|
|
|
|
volume = left
|
|
|
|
return int(volume)/100
|
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2016-06-25 07:06:36 +00:00
|
|
|
return SUPPORT_CMUS
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Service to send the CMUS the command to stop playing."""
|
|
|
|
self.cmus.player_stop()
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Service to send the CMUS the command to start playing."""
|
|
|
|
self.cmus.player_play()
|
|
|
|
|
|
|
|
def set_volume_level(self, volume):
|
|
|
|
"""Set volume level, range 0..1."""
|
|
|
|
self.cmus.set_volume(int(volume * 100))
|
|
|
|
|
|
|
|
def volume_up(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set the volume up."""
|
2016-06-25 07:06:36 +00:00
|
|
|
left = self.status['set'].get('vol_left')
|
|
|
|
right = self.status['set'].get('vol_right')
|
|
|
|
if left != right:
|
|
|
|
current_volume = float(left + right) / 2
|
|
|
|
else:
|
|
|
|
current_volume = left
|
|
|
|
|
|
|
|
if current_volume <= 100:
|
|
|
|
self.cmus.set_volume(int(current_volume) + 5)
|
|
|
|
|
|
|
|
def volume_down(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set the volume down."""
|
2016-06-25 07:06:36 +00:00
|
|
|
left = self.status['set'].get('vol_left')
|
|
|
|
right = self.status['set'].get('vol_right')
|
|
|
|
if left != right:
|
|
|
|
current_volume = float(left + right) / 2
|
|
|
|
else:
|
|
|
|
current_volume = left
|
|
|
|
|
|
|
|
if current_volume <= 100:
|
|
|
|
self.cmus.set_volume(int(current_volume) - 5)
|
|
|
|
|
|
|
|
def play_media(self, media_type, media_id, **kwargs):
|
|
|
|
"""Send the play command."""
|
|
|
|
if media_type in [MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST]:
|
|
|
|
self.cmus.player_play_file(media_id)
|
|
|
|
else:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Invalid media type %s. Only %s and %s are supported",
|
|
|
|
media_type, MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST)
|
|
|
|
|
|
|
|
def media_pause(self):
|
|
|
|
"""Send the pause command."""
|
|
|
|
self.cmus.player_pause()
|
|
|
|
|
|
|
|
def media_next_track(self):
|
|
|
|
"""Send next track command."""
|
|
|
|
self.cmus.player_next()
|
|
|
|
|
|
|
|
def media_previous_track(self):
|
|
|
|
"""Send next track command."""
|
|
|
|
self.cmus.player_prev()
|
|
|
|
|
|
|
|
def media_seek(self, position):
|
|
|
|
"""Send seek command."""
|
|
|
|
self.cmus.seek(position)
|
|
|
|
|
|
|
|
def media_play(self):
|
|
|
|
"""Send the play command."""
|
|
|
|
self.cmus.player_play()
|
|
|
|
|
|
|
|
def media_stop(self):
|
|
|
|
"""Send the stop command."""
|
|
|
|
self.cmus.stop()
|