2015-05-31 10:15:05 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.media_player.mpd
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Provides functionality to interact with a Music Player Daemon.
|
|
|
|
|
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
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-11 06:51:38 +00:00
|
|
|
try:
|
2015-06-11 06:59:37 +00:00
|
|
|
import mpd
|
2015-06-11 06:51:38 +00:00
|
|
|
except ImportError:
|
2015-06-11 06:59:37 +00:00
|
|
|
mpd = None
|
2015-06-11 06:51:38 +00:00
|
|
|
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
STATE_PLAYING, STATE_PAUSED, STATE_OFF)
|
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
from homeassistant.components.media_player import (
|
2015-06-09 06:06:41 +00:00
|
|
|
MediaPlayerDevice,
|
|
|
|
SUPPORT_PAUSE, SUPPORT_VOLUME_SET, SUPPORT_TURN_OFF,
|
2015-12-12 15:25:56 +00:00
|
|
|
SUPPORT_TURN_ON, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK,
|
2015-06-09 06:06:41 +00:00
|
|
|
MEDIA_TYPE_MUSIC)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-08-30 01:39:50 +00:00
|
|
|
REQUIREMENTS = ['python-mpd2==0.5.4']
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
|
2015-12-12 15:25:56 +00:00
|
|
|
SUPPORT_TURN_ON | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK
|
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):
|
|
|
|
""" Sets up the MPD platform. """
|
|
|
|
|
|
|
|
daemon = config.get('server', None)
|
|
|
|
port = config.get('port', 6600)
|
2015-05-31 18:52:28 +00:00
|
|
|
location = config.get('location', 'MPD')
|
2015-08-30 13:53:40 +00:00
|
|
|
password = config.get('password', None)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-07-07 07:01:46 +00:00
|
|
|
global mpd # pylint: disable=invalid-name
|
2015-06-11 06:59:37 +00:00
|
|
|
if mpd is None:
|
2015-07-07 07:01:46 +00:00
|
|
|
import mpd as mpd_
|
|
|
|
mpd = 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:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to connect to MPD. "
|
|
|
|
"Please check your settings")
|
|
|
|
|
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):
|
|
|
|
_LOGGER.error(
|
|
|
|
"MPD reported incorrect password. "
|
|
|
|
"Please check your password.")
|
|
|
|
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
add_devices([MpdDevice(daemon, port, location, password)])
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MpdDevice(MediaPlayerDevice):
|
|
|
|
""" Represents a MPD server. """
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
# MPD confuses pylint
|
|
|
|
# pylint: disable=no-member, abstract-method
|
|
|
|
|
2015-08-30 13:53:40 +00:00
|
|
|
def __init__(self, server, port, location, password):
|
2015-05-31 10:15:05 +00:00
|
|
|
self.server = server
|
|
|
|
self.port = port
|
2015-05-31 18:52:28 +00:00
|
|
|
self._name = location
|
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
|
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):
|
|
|
|
try:
|
|
|
|
self.status = self.client.status()
|
|
|
|
self.currentsong = self.client.currentsong()
|
2015-06-11 06:59:37 +00:00
|
|
|
except mpd.ConnectionError:
|
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):
|
|
|
|
""" Returns the name of the device. """
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2015-06-09 06:06:41 +00:00
|
|
|
""" Returns 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):
|
|
|
|
""" Content ID of current playing media. """
|
2015-06-11 06:51:38 +00:00
|
|
|
return self.currentsong['id']
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
|
|
|
""" Content type of current playing media. """
|
|
|
|
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):
|
|
|
|
""" 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):
|
|
|
|
""" Title of current playing media. """
|
2015-12-13 18:54:10 +00:00
|
|
|
name = self.currentsong.get('name', None)
|
2015-12-13 17:42:53 +00:00
|
|
|
title = self.currentsong['title']
|
|
|
|
|
2015-12-13 18:54:10 +00:00
|
|
|
if name is None:
|
2015-12-13 17:42:53 +00:00
|
|
|
return title
|
2015-12-13 18:54:10 +00:00
|
|
|
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):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
2015-06-11 06:51:38 +00:00
|
|
|
return int(self.status['volume'])/100
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_media_commands(self):
|
|
|
|
""" Flags of media commands that are supported. """
|
|
|
|
return SUPPORT_MPD
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
def turn_off(self):
|
2015-12-12 15:25:56 +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):
|
|
|
|
""" Service to send the MPD the command to start playing. """
|
|
|
|
self.client.play()
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
def set_volume_level(self, volume):
|
|
|
|
""" Sets volume """
|
|
|
|
self.client.setvol(int(volume * 100))
|
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
def volume_up(self):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
|
|
|
""" 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):
|
|
|
|
""" Service to send the MPD the command for next track. """
|
|
|
|
self.client.next()
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
def media_previous_track(self):
|
2015-05-31 10:15:05 +00:00
|
|
|
""" Service to send the MPD the command for previous track. """
|
|
|
|
self.client.previous()
|