2015-05-31 10:15:05 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.media_player.mpd
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Provides functionality to interact with a Music Player Daemon.
|
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
To use MPD you will need to add something like the following to your
|
|
|
|
config/configuration.yaml
|
|
|
|
|
|
|
|
media_player:
|
|
|
|
platform: mpd
|
|
|
|
server: 127.0.0.1
|
|
|
|
port: 6600
|
2015-05-31 20:21:11 +00:00
|
|
|
location: bedroom
|
|
|
|
|
|
|
|
Variables:
|
|
|
|
|
|
|
|
server
|
|
|
|
*Required
|
|
|
|
IP address of the Music Player Daemon. Example: 192.168.1.32
|
|
|
|
|
|
|
|
port
|
|
|
|
*Optional
|
|
|
|
Port of the Music Player Daemon, defaults to 6600. Example: 6600
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-05-31 20:21:11 +00:00
|
|
|
location
|
|
|
|
*Optional
|
|
|
|
Location of your Music Player Daemon.
|
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-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,
|
|
|
|
SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK,
|
|
|
|
MEDIA_TYPE_MUSIC)
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
SUPPORT_MPD = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_TURN_OFF | \
|
|
|
|
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK
|
|
|
|
|
|
|
|
|
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-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
try:
|
2015-05-31 18:52:28 +00:00
|
|
|
from mpd import MPDClient
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
_LOGGER.exception(
|
|
|
|
"Unable to import mpd2. "
|
|
|
|
"Did you maybe not install the 'python-mpd2' package?")
|
|
|
|
|
2015-06-01 11:25:55 +00:00
|
|
|
return False
|
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:
|
|
|
|
mpd_client = MPDClient()
|
|
|
|
mpd_client.connect(daemon, port)
|
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-05-31 18:52:28 +00:00
|
|
|
|
2015-05-31 10:15:05 +00:00
|
|
|
mpd = []
|
2015-06-01 11:25:55 +00:00
|
|
|
mpd.append(MpdDevice(daemon, port, location))
|
2015-05-31 10:15:05 +00:00
|
|
|
add_devices(mpd)
|
|
|
|
|
|
|
|
|
|
|
|
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-05-31 18:52:28 +00:00
|
|
|
def __init__(self, server, port, location):
|
2015-05-31 10:15:05 +00:00
|
|
|
from mpd import MPDClient
|
|
|
|
|
|
|
|
self.server = server
|
|
|
|
self.port = port
|
2015-05-31 18:52:28 +00:00
|
|
|
self._name = location
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
self.client = MPDClient()
|
|
|
|
self.client.timeout = 10
|
|
|
|
self.client.idletimeout = None
|
|
|
|
self.client.connect(self.server, self.port)
|
|
|
|
|
|
|
|
@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-05-31 10:15:05 +00:00
|
|
|
status = self.client.status()
|
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
if status['state'] == 'play':
|
|
|
|
return STATE_PLAYING
|
|
|
|
elif status['state'] == 'pause':
|
|
|
|
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-05-31 10:15:05 +00:00
|
|
|
current_song = self.client.currentsong()
|
2015-06-09 06:06:41 +00:00
|
|
|
return current_song['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. """
|
|
|
|
current_song = self.client.currentsong()
|
|
|
|
return current_song['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. """
|
|
|
|
current_song = self.client.currentsong()
|
|
|
|
return current_song['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) """
|
|
|
|
current_song = self.client.currentsong()
|
|
|
|
return current_song['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) """
|
|
|
|
current_song = self.client.currentsong()
|
|
|
|
return current_song['album']
|
2015-05-31 10:15:05 +00:00
|
|
|
|
2015-06-09 06:06:41 +00:00
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
status = self.client.status()
|
|
|
|
return int(status['volume'])/100
|
2015-05-31 10:15:05 +00:00
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
""" Service to exit the running MPD. """
|
2015-05-31 18:52:28 +00:00
|
|
|
self.client.stop()
|
2015-05-31 10:15:05 +00:00
|
|
|
|
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. """
|
|
|
|
current_volume = self.client.status()['volume']
|
|
|
|
|
2015-05-31 18:52:28 +00:00
|
|
|
if int(current_volume) <= 100:
|
2015-05-31 10:15:05 +00:00
|
|
|
self.client.setvol(int(current_volume) + 5)
|
|
|
|
|
|
|
|
def volume_down(self):
|
|
|
|
""" Service to send the MPD the command for volume down. """
|
|
|
|
current_volume = self.client.status()['volume']
|
|
|
|
|
2015-05-31 18:52:28 +00:00
|
|
|
if int(current_volume) >= 0:
|
2015-05-31 10:15:05 +00:00
|
|
|
self.client.setvol(int(current_volume) - 5)
|
|
|
|
|
|
|
|
def media_play(self):
|
|
|
|
""" Service to send the MPD the command for play/pause. """
|
|
|
|
self.client.start()
|
|
|
|
|
|
|
|
def media_pause(self):
|
|
|
|
""" Service to send the MPD the command for play/pause. """
|
|
|
|
self.client.pause()
|
|
|
|
|
|
|
|
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()
|