2015-05-31 10:15:05 +00:00
|
|
|
"""
|
2016-03-08 09:34:33 +00:00
|
|
|
Support to interact with a Music Player Daemon.
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-10-23 16:13:45 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/media_player.mpd/
|
2015-05-31 10:15:05 +00:00
|
|
|
"""
|
2018-01-21 06:35:38 +00:00
|
|
|
from datetime import timedelta
|
2015-05-31 10:15:05 +00:00
|
|
|
import logging
|
2017-10-27 09:21:47 +00:00
|
|
|
import os
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2016-09-05 15:51:18 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
from homeassistant.components.media_player import (
|
2018-01-21 06:35:38 +00:00
|
|
|
MEDIA_TYPE_MUSIC, MEDIA_TYPE_PLAYLIST, PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_CLEAR_PLAYLIST, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PLAY,
|
|
|
|
SUPPORT_PLAY_MEDIA, SUPPORT_PREVIOUS_TRACK, SUPPORT_SEEK,
|
|
|
|
SUPPORT_SELECT_SOURCE, SUPPORT_SHUFFLE_SET, SUPPORT_STOP, SUPPORT_TURN_OFF,
|
|
|
|
SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,
|
|
|
|
SUPPORT_VOLUME_STEP, MediaPlayerDevice)
|
2016-09-05 15:51:18 +00:00
|
|
|
from homeassistant.const import (
|
2018-01-21 06:35:38 +00:00
|
|
|
CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_PORT, STATE_OFF, STATE_PAUSED,
|
|
|
|
STATE_PLAYING)
|
2016-09-05 15:51:18 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-01-17 07:01:57 +00:00
|
|
|
from homeassistant.util import Throttle
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2016-03-24 07:07:06 +00:00
|
|
|
REQUIREMENTS = ['python-mpd2==0.5.5']
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2016-09-05 15:51:18 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-03-14 19:10:35 +00:00
|
|
|
DEFAULT_NAME = 'MPD'
|
2016-09-05 15:51:18 +00:00
|
|
|
DEFAULT_PORT = 6600
|
|
|
|
|
2017-01-17 07:01:57 +00:00
|
|
|
PLAYLIST_UPDATE_INTERVAL = timedelta(seconds=120)
|
|
|
|
|
2017-09-14 06:38:07 +00:00
|
|
|
SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_STEP | \
|
|
|
|
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_MUTE | \
|
2017-06-18 16:31:45 +00:00
|
|
|
SUPPORT_PLAY_MEDIA | SUPPORT_PLAY | SUPPORT_SELECT_SOURCE | \
|
2017-08-01 06:18:26 +00:00
|
|
|
SUPPORT_CLEAR_PLAYLIST | SUPPORT_SHUFFLE_SET | SUPPORT_SEEK | \
|
2017-09-14 06:38:07 +00:00
|
|
|
SUPPORT_STOP | SUPPORT_TURN_OFF | SUPPORT_TURN_ON
|
2015-06-09 06:06:41 +00:00
|
|
|
|
2016-09-05 15:51:18 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
2017-03-14 19:10:35 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-09-05 15:51:18 +00:00
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
})
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the MPD platform."""
|
2017-08-01 06:18:26 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2016-09-05 15:51:18 +00:00
|
|
|
port = config.get(CONF_PORT)
|
2017-03-14 19:10:35 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-09-05 15:51:18 +00:00
|
|
|
password = config.get(CONF_PASSWORD)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2017-08-01 06:18:26 +00:00
|
|
|
device = MpdDevice(host, port, password, name)
|
|
|
|
add_devices([device], True)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MpdDevice(MediaPlayerDevice):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Representation of a MPD server."""
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2016-11-04 01:32:14 +00:00
|
|
|
# pylint: disable=no-member
|
2017-03-14 19:10:35 +00:00
|
|
|
def __init__(self, server, port, password, name):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Initialize the MPD device."""
|
2016-01-29 05:37:08 +00:00
|
|
|
import mpd
|
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
self.server = server
|
|
|
|
self.port = port
|
2017-03-14 19:10:35 +00:00
|
|
|
self._name = name
|
2015-08-30 13:53:40 +00:00
|
|
|
self.password = password
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2017-08-01 06:18:26 +00:00
|
|
|
self._status = None
|
|
|
|
self._currentsong = None
|
|
|
|
self._playlists = []
|
|
|
|
self._currentplaylist = None
|
|
|
|
self._is_connected = False
|
2017-09-14 06:38:07 +00:00
|
|
|
self._muted = False
|
|
|
|
self._muted_volume = 0
|
2017-08-01 06:18:26 +00:00
|
|
|
|
|
|
|
# set up MPD client
|
|
|
|
self._client = mpd.MPDClient()
|
|
|
|
self._client.timeout = 5
|
|
|
|
self._client.idletimeout = None
|
|
|
|
|
|
|
|
def _connect(self):
|
|
|
|
"""Connect to MPD."""
|
|
|
|
import mpd
|
|
|
|
try:
|
|
|
|
self._client.connect(self.server, self.port)
|
2017-08-19 08:58:42 +00:00
|
|
|
|
|
|
|
if self.password is not None:
|
|
|
|
self._client.password(self.password)
|
2017-08-01 06:18:26 +00:00
|
|
|
except mpd.ConnectionError:
|
|
|
|
return
|
|
|
|
|
|
|
|
self._is_connected = True
|
|
|
|
|
|
|
|
def _disconnect(self):
|
|
|
|
"""Disconnect from MPD."""
|
|
|
|
import mpd
|
|
|
|
try:
|
|
|
|
self._client.disconnect()
|
|
|
|
except mpd.ConnectionError:
|
|
|
|
pass
|
|
|
|
self._is_connected = False
|
|
|
|
self._status = None
|
|
|
|
|
|
|
|
def _fetch_status(self):
|
|
|
|
"""Fetch status from MPD."""
|
|
|
|
self._status = self._client.status()
|
|
|
|
self._currentsong = self._client.currentsong()
|
|
|
|
|
|
|
|
self._update_playlists()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return true if MPD is available and connected."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._is_connected
|
2015-06-11 06:51:38 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Get the latest data and update the state."""
|
2016-01-29 05:37:08 +00:00
|
|
|
import mpd
|
2017-08-01 06:18:26 +00:00
|
|
|
|
2015-06-11 06:51:38 +00:00
|
|
|
try:
|
2017-08-01 06:18:26 +00:00
|
|
|
if not self._is_connected:
|
|
|
|
self._connect()
|
|
|
|
|
|
|
|
self._fetch_status()
|
2016-12-01 20:28:31 +00:00
|
|
|
except (mpd.ConnectionError, OSError, BrokenPipeError, ValueError):
|
2016-05-12 04:53:56 +00:00
|
|
|
# Cleanly disconnect in case connection is not in valid state
|
2017-08-01 06:18:26 +00:00
|
|
|
self._disconnect()
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the name of the device."""
|
2015-05-31 10:15:05 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the media state."""
|
2017-08-01 06:18:26 +00:00
|
|
|
if self._status is None:
|
|
|
|
return STATE_OFF
|
|
|
|
elif self._status['state'] == 'play':
|
2015-06-09 06:06:41 +00:00
|
|
|
return STATE_PLAYING
|
2017-08-01 06:18:26 +00:00
|
|
|
elif self._status['state'] == 'pause':
|
2015-06-09 06:06:41 +00:00
|
|
|
return STATE_PAUSED
|
2017-09-14 06:38:07 +00:00
|
|
|
elif self._status['state'] == 'stop':
|
|
|
|
return STATE_OFF
|
|
|
|
|
|
|
|
return STATE_OFF
|
2017-07-06 06:30:01 +00:00
|
|
|
|
2017-09-14 06:38:07 +00:00
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Boolean if volume is currently muted."""
|
|
|
|
return self._muted
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-05-31 18:52:28 +00:00
|
|
|
@property
|
2015-06-09 06:06:41 +00:00
|
|
|
def media_content_id(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the content ID of current playing media."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._currentsong.get('file')
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the content type of current playing media."""
|
2015-06-09 06:06:41 +00:00
|
|
|
return MEDIA_TYPE_MUSIC
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def media_duration(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the duration of current playing media in seconds."""
|
2015-06-11 06:51:38 +00:00
|
|
|
# Time does not exist for streams
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._currentsong.get('time')
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def media_title(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the title of current playing media."""
|
2017-08-01 06:18:26 +00:00
|
|
|
name = self._currentsong.get('name', None)
|
|
|
|
title = self._currentsong.get('title', None)
|
2017-10-27 09:21:47 +00:00
|
|
|
file_name = self._currentsong.get('file', None)
|
2015-12-13 17:42:53 +00:00
|
|
|
|
2015-12-29 21:10:09 +00:00
|
|
|
if name is None and title is None:
|
2017-10-27 09:21:47 +00:00
|
|
|
if file_name is None:
|
|
|
|
return "None"
|
2018-02-11 17:20:28 +00:00
|
|
|
return os.path.basename(file_name)
|
2015-12-29 21:10:09 +00:00
|
|
|
elif name is None:
|
2015-12-13 17:42:53 +00:00
|
|
|
return title
|
2015-12-29 21:10:09 +00:00
|
|
|
elif title is None:
|
2015-12-30 12:00:34 +00:00
|
|
|
return name
|
2017-07-06 06:30:01 +00:00
|
|
|
|
|
|
|
return '{}: {}'.format(name, title)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def media_artist(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the artist of current playing media (Music track only)."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._currentsong.get('artist')
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def media_album_name(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the album of current playing media (Music track only)."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._currentsong.get('album')
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def volume_level(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the volume level."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return int(self._status['volume'])/100
|
2015-06-11 06:51:38 +00:00
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2015-06-11 06:51:38 +00:00
|
|
|
return SUPPORT_MPD
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2017-01-17 07:01:57 +00:00
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Name of the current input source."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._currentplaylist
|
2017-01-17 07:01:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the list of available input sources."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return self._playlists
|
2017-01-17 07:01:57 +00:00
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Choose a different available playlist and play it."""
|
|
|
|
self.play_media(MEDIA_TYPE_PLAYLIST, source)
|
|
|
|
|
|
|
|
@Throttle(PLAYLIST_UPDATE_INTERVAL)
|
|
|
|
def _update_playlists(self, **kwargs):
|
|
|
|
"""Update available MPD playlists."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._playlists = []
|
|
|
|
for playlist_data in self._client.listplaylists():
|
|
|
|
self._playlists.append(playlist_data['playlist'])
|
2015-12-12 15:25:56 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
def set_volume_level(self, volume):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Set volume of media player."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.setvol(int(volume * 100))
|
2015-06-09 06:06:41 +00:00
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
def volume_up(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command for volume up."""
|
2017-08-01 06:18:26 +00:00
|
|
|
current_volume = int(self._status['volume'])
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-11 06:51:38 +00:00
|
|
|
if current_volume <= 100:
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.setvol(current_volume + 5)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
def volume_down(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command for volume down."""
|
2017-08-01 06:18:26 +00:00
|
|
|
current_volume = int(self._status['volume'])
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-11 06:51:38 +00:00
|
|
|
if current_volume >= 0:
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.setvol(current_volume - 5)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
def media_play(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command for play/pause."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.pause(0)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
def media_pause(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command for play/pause."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.pause(1)
|
|
|
|
|
|
|
|
def media_stop(self):
|
|
|
|
"""Service to send the MPD the command for stop."""
|
|
|
|
self._client.stop()
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
def media_next_track(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command for next track."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.next()
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
def media_previous_track(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command for previous track."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.previous()
|
2016-04-17 19:34:33 +00:00
|
|
|
|
2017-09-14 06:38:07 +00:00
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Mute. Emulated with set_volume_level."""
|
|
|
|
if mute is True:
|
|
|
|
self._muted_volume = self.volume_level
|
|
|
|
self.set_volume_level(0)
|
|
|
|
elif mute is False:
|
|
|
|
self.set_volume_level(self._muted_volume)
|
|
|
|
self._muted = mute
|
|
|
|
|
2016-05-20 06:30:19 +00:00
|
|
|
def play_media(self, media_type, media_id, **kwargs):
|
2016-04-17 19:34:33 +00:00
|
|
|
"""Send the media player the command for playing a playlist."""
|
2017-08-01 06:18:26 +00:00
|
|
|
_LOGGER.debug(str.format("Playing playlist: {0}", media_id))
|
2016-04-17 19:34:33 +00:00
|
|
|
if media_type == MEDIA_TYPE_PLAYLIST:
|
2017-08-01 06:18:26 +00:00
|
|
|
if media_id in self._playlists:
|
|
|
|
self._currentplaylist = media_id
|
2017-01-17 07:01:57 +00:00
|
|
|
else:
|
2017-08-01 06:18:26 +00:00
|
|
|
self._currentplaylist = None
|
2017-01-17 07:01:57 +00:00
|
|
|
_LOGGER.warning(str.format("Unknown playlist name %s.",
|
|
|
|
media_id))
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.clear()
|
|
|
|
self._client.load(media_id)
|
|
|
|
self._client.play()
|
2016-04-17 19:34:33 +00:00
|
|
|
else:
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.clear()
|
|
|
|
self._client.add(media_id)
|
|
|
|
self._client.play()
|
2017-06-18 16:31:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def shuffle(self):
|
|
|
|
"""Boolean if shuffle is enabled."""
|
2017-08-01 06:18:26 +00:00
|
|
|
return bool(self._status['random'])
|
2017-06-18 16:31:45 +00:00
|
|
|
|
|
|
|
def set_shuffle(self, shuffle):
|
|
|
|
"""Enable/disable shuffle mode."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.random(int(shuffle))
|
2017-06-18 16:31:45 +00:00
|
|
|
|
2017-09-14 06:38:07 +00:00
|
|
|
def turn_off(self):
|
|
|
|
"""Service to send the MPD the command to stop playing."""
|
|
|
|
self._client.stop()
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Service to send the MPD the command to start playing."""
|
|
|
|
self._client.play()
|
|
|
|
self._update_playlists(no_throttle=True)
|
|
|
|
|
2017-06-18 16:31:45 +00:00
|
|
|
def clear_playlist(self):
|
|
|
|
"""Clear players playlist."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.clear()
|
2017-06-18 16:31:45 +00:00
|
|
|
|
|
|
|
def media_seek(self, position):
|
|
|
|
"""Send seek command."""
|
2017-08-01 06:18:26 +00:00
|
|
|
self._client.seekcur(position)
|