Merge pull request #294 from renekliment/mpd-password-authorization
support for MPD password authorization
This commit is contained in:
commit
b4fea395de
1 changed files with 27 additions and 2 deletions
|
@ -14,6 +14,7 @@ media_player:
|
||||||
server: 127.0.0.1
|
server: 127.0.0.1
|
||||||
port: 6600
|
port: 6600
|
||||||
location: bedroom
|
location: bedroom
|
||||||
|
password: superSecretPassword123
|
||||||
|
|
||||||
Variables:
|
Variables:
|
||||||
|
|
||||||
|
@ -28,6 +29,10 @@ Port of the Music Player Daemon, defaults to 6600. Example: 6600
|
||||||
location
|
location
|
||||||
*Optional
|
*Optional
|
||||||
Location of your Music Player Daemon.
|
Location of your Music Player Daemon.
|
||||||
|
|
||||||
|
password
|
||||||
|
*Optional
|
||||||
|
Password for your Music Player Daemon.
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
@ -61,6 +66,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
daemon = config.get('server', None)
|
daemon = config.get('server', None)
|
||||||
port = config.get('port', 6600)
|
port = config.get('port', 6600)
|
||||||
location = config.get('location', 'MPD')
|
location = config.get('location', 'MPD')
|
||||||
|
password = config.get('password', None)
|
||||||
|
|
||||||
global mpd # pylint: disable=invalid-name
|
global mpd # pylint: disable=invalid-name
|
||||||
if mpd is None:
|
if mpd is None:
|
||||||
|
@ -71,6 +77,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
try:
|
try:
|
||||||
mpd_client = mpd.MPDClient()
|
mpd_client = mpd.MPDClient()
|
||||||
mpd_client.connect(daemon, port)
|
mpd_client.connect(daemon, port)
|
||||||
|
|
||||||
|
if password is not None:
|
||||||
|
mpd_client.password(password)
|
||||||
|
|
||||||
mpd_client.close()
|
mpd_client.close()
|
||||||
mpd_client.disconnect()
|
mpd_client.disconnect()
|
||||||
except socket.error:
|
except socket.error:
|
||||||
|
@ -79,8 +89,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"Please check your settings")
|
"Please check your settings")
|
||||||
|
|
||||||
return False
|
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):
|
class MpdDevice(MediaPlayerDevice):
|
||||||
|
@ -89,10 +109,11 @@ class MpdDevice(MediaPlayerDevice):
|
||||||
# MPD confuses pylint
|
# MPD confuses pylint
|
||||||
# pylint: disable=no-member, abstract-method
|
# pylint: disable=no-member, abstract-method
|
||||||
|
|
||||||
def __init__(self, server, port, location):
|
def __init__(self, server, port, location, password):
|
||||||
self.server = server
|
self.server = server
|
||||||
self.port = port
|
self.port = port
|
||||||
self._name = location
|
self._name = location
|
||||||
|
self.password = password
|
||||||
self.status = None
|
self.status = None
|
||||||
self.currentsong = None
|
self.currentsong = None
|
||||||
|
|
||||||
|
@ -107,6 +128,10 @@ class MpdDevice(MediaPlayerDevice):
|
||||||
self.currentsong = self.client.currentsong()
|
self.currentsong = self.client.currentsong()
|
||||||
except mpd.ConnectionError:
|
except mpd.ConnectionError:
|
||||||
self.client.connect(self.server, self.port)
|
self.client.connect(self.server, self.port)
|
||||||
|
|
||||||
|
if self.password is not None:
|
||||||
|
self.client.password(self.password)
|
||||||
|
|
||||||
self.status = self.client.status()
|
self.status = self.client.status()
|
||||||
self.currentsong = self.client.currentsong()
|
self.currentsong = self.client.currentsong()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue