core/homeassistant/components/media_player/mpd.py

269 lines
8.2 KiB
Python
Raw Normal View History

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
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,
SUPPORT_VOLUME_SET, SUPPORT_PLAY_MEDIA, SUPPORT_PLAY, MEDIA_TYPE_PLAYLIST,
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,
CONF_HOST, CONF_NAME)
2016-09-05 15:51:18 +00:00
import homeassistant.helpers.config_validation as cv
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__)
DEFAULT_NAME = 'MPD'
2016-09-05 15:51:18 +00:00
DEFAULT_PORT = 6600
PLAYLIST_UPDATE_INTERVAL = timedelta(seconds=120)
2015-06-09 06:06:41 +00:00
SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
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,
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)
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
# 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)
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")
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
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
# pylint: disable=no-member
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
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
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()
self._update_playlists()
except (mpd.ConnectionError, OSError, BrokenPipeError, ValueError):
# 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."""
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."""
name = self.currentsong.get('name', None)
title = self.currentsong.get('title', None)
if name is None and title is None:
2015-12-30 12:26:42 +00:00
return "None"
elif name is None:
return title
elif title is None:
return name
else:
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
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
@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
def turn_on(self):
2016-03-08 09:34:33 +00:00
"""Service to send the MPD the command to start playing."""
self.client.play()
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-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()
def play_media(self, media_type, media_id, **kwargs):
"""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:
if media_id in self.playlists:
self.currentplaylist = media_id
else:
self.currentplaylist = None
_LOGGER.warning(str.format("Unknown playlist name %s.",
media_id))
self.client.clear()
self.client.load(media_id)
self.client.play()
else:
self.client.clear()
self.client.add(media_id)
self.client.play()