2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Clementine Music Player as media player."""
|
2017-02-16 14:34:34 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.media_player import MediaPlayerDevice, PLATFORM_SCHEMA
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
SUPPORT_NEXT_TRACK,
|
|
|
|
SUPPORT_PAUSE,
|
|
|
|
SUPPORT_PLAY,
|
|
|
|
SUPPORT_PREVIOUS_TRACK,
|
|
|
|
SUPPORT_SELECT_SOURCE,
|
|
|
|
SUPPORT_VOLUME_SET,
|
|
|
|
SUPPORT_VOLUME_STEP,
|
|
|
|
)
|
2017-02-16 14:34:34 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ACCESS_TOKEN,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PORT,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_PAUSED,
|
|
|
|
STATE_PLAYING,
|
|
|
|
)
|
2018-09-09 12:26:06 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Clementine Remote"
|
2017-05-02 16:18:47 +00:00
|
|
|
DEFAULT_PORT = 5500
|
2017-02-16 14:34:34 +00:00
|
|
|
|
2018-09-09 12:26:06 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=5)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_CLEMENTINE = (
|
|
|
|
SUPPORT_PAUSE
|
|
|
|
| SUPPORT_VOLUME_STEP
|
|
|
|
| SUPPORT_PREVIOUS_TRACK
|
|
|
|
| SUPPORT_VOLUME_SET
|
|
|
|
| SUPPORT_NEXT_TRACK
|
|
|
|
| SUPPORT_SELECT_SOURCE
|
|
|
|
| SUPPORT_PLAY
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_ACCESS_TOKEN): cv.positive_int,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
}
|
|
|
|
)
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Clementine platform."""
|
2017-02-16 14:34:34 +00:00
|
|
|
from clementineremote import ClementineRemote
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
port = config.get(CONF_PORT)
|
|
|
|
token = config.get(CONF_ACCESS_TOKEN)
|
|
|
|
|
|
|
|
client = ClementineRemote(host, port, token, reconnect=True)
|
2017-02-16 14:34:34 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([ClementineDevice(client, config[CONF_NAME])])
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClementineDevice(MediaPlayerDevice):
|
|
|
|
"""Representation of Clementine Player."""
|
|
|
|
|
|
|
|
def __init__(self, client, name):
|
|
|
|
"""Initialize the Clementine device."""
|
|
|
|
self._client = client
|
|
|
|
self._name = name
|
|
|
|
self._muted = False
|
|
|
|
self._volume = 0.0
|
|
|
|
self._track_id = 0
|
|
|
|
self._last_track_id = 0
|
2019-07-31 19:25:30 +00:00
|
|
|
self._track_name = ""
|
|
|
|
self._track_artist = ""
|
|
|
|
self._track_album_name = ""
|
2018-09-09 12:26:06 +00:00
|
|
|
self._state = None
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Retrieve the latest data from the Clementine Player."""
|
|
|
|
try:
|
|
|
|
client = self._client
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if client.state == "Playing":
|
2017-02-16 14:34:34 +00:00
|
|
|
self._state = STATE_PLAYING
|
2019-07-31 19:25:30 +00:00
|
|
|
elif client.state == "Paused":
|
2017-02-16 14:34:34 +00:00
|
|
|
self._state = STATE_PAUSED
|
2019-07-31 19:25:30 +00:00
|
|
|
elif client.state == "Disconnected":
|
2017-02-16 14:34:34 +00:00
|
|
|
self._state = STATE_OFF
|
|
|
|
else:
|
|
|
|
self._state = STATE_PAUSED
|
|
|
|
|
|
|
|
if client.last_update and (time.time() - client.last_update > 40):
|
|
|
|
self._state = STATE_OFF
|
|
|
|
|
|
|
|
self._volume = float(client.volume) if client.volume else 0.0
|
|
|
|
|
|
|
|
if client.current_track:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._track_id = client.current_track["track_id"]
|
|
|
|
self._track_name = client.current_track["title"]
|
|
|
|
self._track_artist = client.current_track["track_artist"]
|
|
|
|
self._track_album_name = client.current_track["track_album"]
|
2017-02-16 14:34:34 +00:00
|
|
|
|
2018-01-29 08:23:53 +00:00
|
|
|
except Exception:
|
2017-02-16 14:34:34 +00:00
|
|
|
self._state = STATE_OFF
|
|
|
|
raise
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Volume level of the media player (0..1)."""
|
|
|
|
return self._volume / 100.0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return current source name."""
|
|
|
|
source_name = "Unknown"
|
|
|
|
client = self._client
|
|
|
|
if client.active_playlist_id in client.playlists:
|
2019-07-31 19:25:30 +00:00
|
|
|
source_name = client.playlists[client.active_playlist_id]["name"]
|
2017-02-16 14:34:34 +00:00
|
|
|
return source_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
source_names = [s["name"] for s in self._client.playlists.values()]
|
|
|
|
return source_names
|
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Select input source."""
|
|
|
|
client = self._client
|
2019-07-31 19:25:30 +00:00
|
|
|
sources = [s for s in client.playlists.values() if s["name"] == source]
|
2017-02-16 14:34:34 +00:00
|
|
|
if len(sources) == 1:
|
2019-07-31 19:25:30 +00:00
|
|
|
client.change_song(sources[0]["id"], 0)
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
|
|
|
"""Content type of current playing media."""
|
|
|
|
return MEDIA_TYPE_MUSIC
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_title(self):
|
|
|
|
"""Title of current playing media."""
|
|
|
|
return self._track_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_artist(self):
|
|
|
|
"""Artist of current playing media, music track only."""
|
|
|
|
return self._track_artist
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_album_name(self):
|
|
|
|
"""Album name of current playing media, music track only."""
|
|
|
|
return self._track_album_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
|
|
|
return SUPPORT_CLEMENTINE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_image_hash(self):
|
|
|
|
"""Hash value for media image."""
|
|
|
|
if self._client.current_track:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._client.current_track["track_id"]
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_get_media_image(self):
|
2017-02-16 14:34:34 +00:00
|
|
|
"""Fetch media image of current playing image."""
|
|
|
|
if self._client.current_track:
|
2019-07-31 19:25:30 +00:00
|
|
|
image = bytes(self._client.current_track["art"])
|
|
|
|
return (image, "image/png")
|
2017-02-16 14:34:34 +00:00
|
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
def volume_up(self):
|
|
|
|
"""Volume up the media player."""
|
|
|
|
newvolume = min(self._client.volume + 4, 100)
|
|
|
|
self._client.set_volume(newvolume)
|
|
|
|
|
|
|
|
def volume_down(self):
|
|
|
|
"""Volume down media player."""
|
|
|
|
newvolume = max(self._client.volume - 4, 0)
|
|
|
|
self._client.set_volume(newvolume)
|
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Send mute command."""
|
|
|
|
self._client.set_volume(0)
|
|
|
|
|
|
|
|
def set_volume_level(self, volume):
|
|
|
|
"""Set volume level."""
|
|
|
|
self._client.set_volume(int(100 * volume))
|
|
|
|
|
|
|
|
def media_play_pause(self):
|
|
|
|
"""Simulate play pause media player."""
|
|
|
|
if self._state == STATE_PLAYING:
|
|
|
|
self.media_pause()
|
|
|
|
else:
|
|
|
|
self.media_play()
|
|
|
|
|
|
|
|
def media_play(self):
|
|
|
|
"""Send play command."""
|
|
|
|
self._state = STATE_PLAYING
|
|
|
|
self._client.play()
|
|
|
|
|
|
|
|
def media_pause(self):
|
|
|
|
"""Send media pause command to media player."""
|
|
|
|
self._state = STATE_PAUSED
|
|
|
|
self._client.pause()
|
|
|
|
|
|
|
|
def media_next_track(self):
|
|
|
|
"""Send next track command."""
|
|
|
|
self._client.next()
|
|
|
|
|
|
|
|
def media_previous_track(self):
|
|
|
|
"""Send the previous track command."""
|
|
|
|
self._client.previous()
|