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
|
|
|
"""
|
|
|
|
import logging
|
2015-05-31 18:52:28 +00:00
|
|
|
import socket
|
2017-01-17 07:01:57 +00:00
|
|
|
from datetime import timedelta
|
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 (
|
2016-09-05 15:51:18 +00:00
|
|
|
MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, PLATFORM_SCHEMA,
|
2016-02-19 05:27:50 +00:00
|
|
|
SUPPORT_PREVIOUS_TRACK, SUPPORT_TURN_OFF, SUPPORT_TURN_ON,
|
2017-01-09 00:09:30 +00:00
|
|
|
SUPPORT_VOLUME_SET, SUPPORT_PLAY_MEDIA, SUPPORT_PLAY, MEDIA_TYPE_PLAYLIST,
|
2017-01-17 07:01:57 +00:00
|
|
|
SUPPORT_SELECT_SOURCE, MediaPlayerDevice)
|
2016-09-05 15:51:18 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_OFF, STATE_PAUSED, STATE_PLAYING, CONF_PORT, CONF_PASSWORD,
|
2017-03-14 19:10:35 +00:00
|
|
|
CONF_HOST, CONF_NAME)
|
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)
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
|
2016-04-17 19:34:33 +00:00
|
|
|
SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
|
2017-01-17 07:01:57 +00:00
|
|
|
SUPPORT_PLAY_MEDIA | SUPPORT_PLAY | SUPPORT_SELECT_SOURCE
|
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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Setup the MPD platform."""
|
2016-09-05 15:51:18 +00:00
|
|
|
daemon = config.get(CONF_HOST)
|
|
|
|
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)
|
2016-01-29 05:37:08 +00:00
|
|
|
import mpd
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-01 11:25:55 +00:00
|
|
|
# pylint: disable=no-member
|
2015-05-31 18:52:28 +00:00
|
|
|
try:
|
2015-06-11 06:59:37 +00:00
|
|
|
mpd_client = mpd.MPDClient()
|
2015-05-31 18:52:28 +00:00
|
|
|
mpd_client.connect(daemon, port)
|
2015-08-30 13:53:40 +00:00
|
|
|
|
|
|
|
if password is not None:
|
|
|
|
mpd_client.password(password)
|
|
|
|
|
2015-06-01 11:25:55 +00:00
|
|
|
mpd_client.close()
|
|
|
|
mpd_client.disconnect()
|
2015-05-31 18:52:28 +00:00
|
|
|
except socket.error:
|
2016-09-05 15:51:18 +00:00
|
|
|
_LOGGER.error("Unable to connect to MPD")
|
2015-06-01 11:25:55 +00:00
|
|
|
return False
|
2015-08-30 13:53:40 +00:00
|
|
|
except mpd.CommandError as error:
|
2015-05-31 18:52:28 +00:00
|
|
|
|
2015-08-30 13:53:40 +00:00
|
|
|
if "incorrect password" in str(error):
|
2016-09-05 15:51:18 +00:00
|
|
|
_LOGGER.error("MPD reported incorrect password")
|
2015-08-30 13:53:40 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2017-03-14 19:10:35 +00:00
|
|
|
add_devices([MpdDevice(daemon, port, password, name)])
|
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-06-11 06:51:38 +00:00
|
|
|
self.status = None
|
|
|
|
self.currentsong = None
|
2017-01-17 07:01:57 +00:00
|
|
|
self.playlists = []
|
|
|
|
self.currentplaylist = None
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-11 06:59:37 +00:00
|
|
|
self.client = mpd.MPDClient()
|
2015-05-31 10:15:05 +00:00
|
|
|
self.client.timeout = 10
|
|
|
|
self.client.idletimeout = None
|
2015-06-11 06:51:38 +00:00
|
|
|
self.update()
|
|
|
|
|
|
|
|
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
|
2015-06-11 06:51:38 +00:00
|
|
|
try:
|
|
|
|
self.status = self.client.status()
|
|
|
|
self.currentsong = self.client.currentsong()
|
2017-01-17 07:01:57 +00:00
|
|
|
self._update_playlists()
|
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
|
|
|
|
try:
|
|
|
|
self.client.disconnect()
|
|
|
|
except mpd.ConnectionError:
|
|
|
|
pass
|
|
|
|
|
2015-06-11 06:51:38 +00:00
|
|
|
self.client.connect(self.server, self.port)
|
2015-08-30 13:53:40 +00:00
|
|
|
|
|
|
|
if self.password is not None:
|
|
|
|
self.client.password(self.password)
|
|
|
|
|
2015-06-11 06:51:38 +00:00
|
|
|
self.status = self.client.status()
|
|
|
|
self.currentsong = self.client.currentsong()
|
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."""
|
2015-06-11 06:51:38 +00:00
|
|
|
if self.status['state'] == 'play':
|
2015-06-09 06:06:41 +00:00
|
|
|
return STATE_PLAYING
|
2015-06-11 06:51:38 +00:00
|
|
|
elif self.status['state'] == 'pause':
|
2015-06-09 06:06:41 +00:00
|
|
|
return STATE_PAUSED
|
2015-05-31 10:15:05 +00:00
|
|
|
else:
|
2015-06-09 06:06:41 +00:00
|
|
|
return STATE_OFF
|
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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Content ID of current playing media."""
|
2016-12-01 20:20:42 +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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Duration of current playing media in seconds."""
|
2015-06-11 06:51:38 +00:00
|
|
|
# Time does not exist for streams
|
|
|
|
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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Title of current playing media."""
|
2015-12-13 18:54:10 +00:00
|
|
|
name = self.currentsong.get('name', None)
|
2015-12-29 16:52:05 +00:00
|
|
|
title = self.currentsong.get('title', 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:
|
2015-12-30 12:26:42 +00:00
|
|
|
return "None"
|
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
|
2015-12-13 18:54:10 +00:00
|
|
|
else:
|
2015-12-29 21:10:09 +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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Artist of current playing media (Music track only)."""
|
2015-06-11 06:51:38 +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):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Album of current playing media (Music track only)."""
|
2015-06-11 06:51:38 +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."""
|
2015-06-11 06:51:38 +00:00
|
|
|
return int(self.status['volume'])/100
|
|
|
|
|
|
|
|
@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."""
|
|
|
|
return self.currentplaylist
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return self.playlists
|
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Choose a different available playlist and play it."""
|
|
|
|
self.play_media(MEDIA_TYPE_PLAYLIST, source)
|
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
def turn_off(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command to stop playing."""
|
2015-05-31 18:52:28 +00:00
|
|
|
self.client.stop()
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-12-12 15:25:56 +00:00
|
|
|
def turn_on(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Service to send the MPD the command to start playing."""
|
2015-12-12 15:25:56 +00:00
|
|
|
self.client.play()
|
2017-01-17 07:01:57 +00:00
|
|
|
self._update_playlists(no_throttle=True)
|
|
|
|
|
|
|
|
@Throttle(PLAYLIST_UPDATE_INTERVAL)
|
|
|
|
def _update_playlists(self, **kwargs):
|
|
|
|
"""Update available MPD playlists."""
|
|
|
|
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."""
|
2015-06-09 06:06:41 +00:00
|
|
|
self.client.setvol(int(volume * 100))
|
|
|
|
|
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."""
|
2015-06-11 06:51:38 +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:
|
|
|
|
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."""
|
2015-06-11 06:51:38 +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:
|
|
|
|
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."""
|
2015-08-30 14:11:44 +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."""
|
2015-08-30 14:11:44 +00:00
|
|
|
self.client.pause(1)
|
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."""
|
2015-05-31 10:15:05 +00:00
|
|
|
self.client.next()
|
|
|
|
|
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."""
|
2015-05-31 10:15:05 +00:00
|
|
|
self.client.previous()
|
2016-04-17 19:34:33 +00:00
|
|
|
|
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."""
|
|
|
|
_LOGGER.info(str.format("Playing playlist: {0}", media_id))
|
|
|
|
if media_type == MEDIA_TYPE_PLAYLIST:
|
2017-01-17 07:01:57 +00:00
|
|
|
if media_id in self.playlists:
|
|
|
|
self.currentplaylist = media_id
|
|
|
|
else:
|
|
|
|
self.currentplaylist = None
|
|
|
|
_LOGGER.warning(str.format("Unknown playlist name %s.",
|
|
|
|
media_id))
|
2016-04-17 19:34:33 +00:00
|
|
|
self.client.clear()
|
|
|
|
self.client.load(media_id)
|
|
|
|
self.client.play()
|
|
|
|
else:
|
2017-02-11 09:07:31 +00:00
|
|
|
self.client.clear()
|
|
|
|
self.client.add(media_id)
|
|
|
|
self.client.play()
|