mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
MPD platform fixes
This commit is contained in:
parent
801eabe598
commit
ae847994fc
@ -32,6 +32,12 @@ Location of your Music Player Daemon.
|
|||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
try:
|
||||||
|
from mpd import MPDClient, ConnectionError
|
||||||
|
except ImportError:
|
||||||
|
MPDClient = None
|
||||||
|
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_PLAYING, STATE_PAUSED, STATE_OFF)
|
STATE_PLAYING, STATE_PAUSED, STATE_OFF)
|
||||||
|
|
||||||
@ -56,10 +62,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
port = config.get('port', 6600)
|
port = config.get('port', 6600)
|
||||||
location = config.get('location', 'MPD')
|
location = config.get('location', 'MPD')
|
||||||
|
|
||||||
try:
|
if MPDClient is None:
|
||||||
from mpd import MPDClient
|
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
_LOGGER.exception(
|
_LOGGER.exception(
|
||||||
"Unable to import mpd2. "
|
"Unable to import mpd2. "
|
||||||
"Did you maybe not install the 'python-mpd2' package?")
|
"Did you maybe not install the 'python-mpd2' package?")
|
||||||
@ -91,16 +94,25 @@ class MpdDevice(MediaPlayerDevice):
|
|||||||
# pylint: disable=no-member, abstract-method
|
# pylint: disable=no-member, abstract-method
|
||||||
|
|
||||||
def __init__(self, server, port, location):
|
def __init__(self, server, port, location):
|
||||||
from mpd import MPDClient
|
|
||||||
|
|
||||||
self.server = server
|
self.server = server
|
||||||
self.port = port
|
self.port = port
|
||||||
self._name = location
|
self._name = location
|
||||||
|
self.status = None
|
||||||
|
self.currentsong = None
|
||||||
|
|
||||||
self.client = MPDClient()
|
self.client = MPDClient()
|
||||||
self.client.timeout = 10
|
self.client.timeout = 10
|
||||||
self.client.idletimeout = None
|
self.client.idletimeout = None
|
||||||
self.client.connect(self.server, self.port)
|
self.update()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
try:
|
||||||
|
self.status = self.client.status()
|
||||||
|
self.currentsong = self.client.currentsong()
|
||||||
|
except ConnectionError:
|
||||||
|
self.client.connect(self.server, self.port)
|
||||||
|
self.status = self.client.status()
|
||||||
|
self.currentsong = self.client.currentsong()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -110,11 +122,9 @@ class MpdDevice(MediaPlayerDevice):
|
|||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
""" Returns the media state. """
|
""" Returns the media state. """
|
||||||
status = self.client.status()
|
if self.status['state'] == 'play':
|
||||||
|
|
||||||
if status['state'] == 'play':
|
|
||||||
return STATE_PLAYING
|
return STATE_PLAYING
|
||||||
elif status['state'] == 'pause':
|
elif self.status['state'] == 'pause':
|
||||||
return STATE_PAUSED
|
return STATE_PAUSED
|
||||||
else:
|
else:
|
||||||
return STATE_OFF
|
return STATE_OFF
|
||||||
@ -122,8 +132,7 @@ class MpdDevice(MediaPlayerDevice):
|
|||||||
@property
|
@property
|
||||||
def media_content_id(self):
|
def media_content_id(self):
|
||||||
""" Content ID of current playing media. """
|
""" Content ID of current playing media. """
|
||||||
current_song = self.client.currentsong()
|
return self.currentsong['id']
|
||||||
return current_song['id']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_content_type(self):
|
def media_content_type(self):
|
||||||
@ -133,31 +142,32 @@ class MpdDevice(MediaPlayerDevice):
|
|||||||
@property
|
@property
|
||||||
def media_duration(self):
|
def media_duration(self):
|
||||||
""" Duration of current playing media in seconds. """
|
""" Duration of current playing media in seconds. """
|
||||||
current_song = self.client.currentsong()
|
# Time does not exist for streams
|
||||||
return current_song['time']
|
return self.currentsong.get('time')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_title(self):
|
def media_title(self):
|
||||||
""" Title of current playing media. """
|
""" Title of current playing media. """
|
||||||
current_song = self.client.currentsong()
|
return self.currentsong['title']
|
||||||
return current_song['title']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_artist(self):
|
def media_artist(self):
|
||||||
""" Artist of current playing media. (Music track only) """
|
""" Artist of current playing media. (Music track only) """
|
||||||
current_song = self.client.currentsong()
|
return self.currentsong.get('artist')
|
||||||
return current_song['artist']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_album_name(self):
|
def media_album_name(self):
|
||||||
""" Album of current playing media. (Music track only) """
|
""" Album of current playing media. (Music track only) """
|
||||||
current_song = self.client.currentsong()
|
return self.currentsong.get('album')
|
||||||
return current_song['album']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_level(self):
|
def volume_level(self):
|
||||||
status = self.client.status()
|
return int(self.status['volume'])/100
|
||||||
return int(status['volume'])/100
|
|
||||||
|
@property
|
||||||
|
def supported_media_commands(self):
|
||||||
|
""" Flags of media commands that are supported. """
|
||||||
|
return SUPPORT_MPD
|
||||||
|
|
||||||
def turn_off(self):
|
def turn_off(self):
|
||||||
""" Service to exit the running MPD. """
|
""" Service to exit the running MPD. """
|
||||||
@ -169,17 +179,17 @@ class MpdDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
def volume_up(self):
|
def volume_up(self):
|
||||||
""" Service to send the MPD the command for volume up. """
|
""" Service to send the MPD the command for volume up. """
|
||||||
current_volume = self.client.status()['volume']
|
current_volume = int(self.status['volume'])
|
||||||
|
|
||||||
if int(current_volume) <= 100:
|
if current_volume <= 100:
|
||||||
self.client.setvol(int(current_volume) + 5)
|
self.client.setvol(current_volume + 5)
|
||||||
|
|
||||||
def volume_down(self):
|
def volume_down(self):
|
||||||
""" Service to send the MPD the command for volume down. """
|
""" Service to send the MPD the command for volume down. """
|
||||||
current_volume = self.client.status()['volume']
|
current_volume = int(self.status['volume'])
|
||||||
|
|
||||||
if int(current_volume) >= 0:
|
if current_volume >= 0:
|
||||||
self.client.setvol(int(current_volume) - 5)
|
self.client.setvol(current_volume - 5)
|
||||||
|
|
||||||
def media_play(self):
|
def media_play(self):
|
||||||
""" Service to send the MPD the command for play/pause. """
|
""" Service to send the MPD the command for play/pause. """
|
||||||
|
Loading…
x
Reference in New Issue
Block a user