support for MPD password authorization

This commit is contained in:
René Kliment 2015-08-30 15:53:40 +02:00
parent 9ecac9e934
commit 41011f0c95

View file

@ -14,6 +14,7 @@ media_player:
server: 127.0.0.1
port: 6600
location: bedroom
password: superSecretPassword123
Variables:
@ -28,6 +29,10 @@ Port of the Music Player Daemon, defaults to 6600. Example: 6600
location
*Optional
Location of your Music Player Daemon.
password
*Optional
Password for your Music Player Daemon.
"""
import logging
import socket
@ -61,6 +66,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
daemon = config.get('server', None)
port = config.get('port', 6600)
location = config.get('location', 'MPD')
password = config.get('password', None)
global mpd # pylint: disable=invalid-name
if mpd is None:
@ -71,6 +77,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try:
mpd_client = mpd.MPDClient()
mpd_client.connect(daemon, port)
if password is not None:
mpd_client.password(password)
mpd_client.close()
mpd_client.disconnect()
except socket.error:
@ -79,8 +89,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"Please check your settings")
return False
except mpd.CommandError as error:
add_devices([MpdDevice(daemon, port, location)])
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)])
class MpdDevice(MediaPlayerDevice):
@ -89,10 +109,11 @@ class MpdDevice(MediaPlayerDevice):
# MPD confuses pylint
# pylint: disable=no-member, abstract-method
def __init__(self, server, port, location):
def __init__(self, server, port, location, password):
self.server = server
self.port = port
self._name = location
self.password = password
self.status = None
self.currentsong = None
@ -107,6 +128,10 @@ class MpdDevice(MediaPlayerDevice):
self.currentsong = self.client.currentsong()
except mpd.ConnectionError:
self.client.connect(self.server, self.port)
if self.password is not None:
self.client.password(self.password)
self.status = self.client.status()
self.currentsong = self.client.currentsong()