core/homeassistant/components/media_player/squeezebox.py

318 lines
9.8 KiB
Python
Raw Normal View History

2015-08-04 15:22:56 +00:00
"""
homeassistant.components.media_player.squeezebox
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Provides an interface to the Logitech SqueezeBox API
Configuration:
To use SqueezeBox add something like this to your configuration:
media_player:
platform: squeezebox
2015-08-05 11:49:45 +00:00
host: 192.168.1.21
2015-08-04 15:22:56 +00:00
port: 9090
2015-08-05 11:49:45 +00:00
username: user
2015-08-04 15:22:56 +00:00
password: password
Variables:
2015-08-05 11:49:45 +00:00
host
2015-08-04 15:22:56 +00:00
*Required
2015-08-05 11:49:45 +00:00
The host name or address of the Logitech Media Server
2015-08-04 15:22:56 +00:00
port
*Optional
Telnet port to Logitech Media Server, default 9090
2015-08-05 11:49:45 +00:00
usermame
2015-08-04 15:22:56 +00:00
*Optional
2015-08-05 11:49:45 +00:00
Username, if password protection is enabled
2015-08-04 15:22:56 +00:00
password
*Optional
Password, if password protection is enabled
"""
import logging
import telnetlib
import urllib.parse
from homeassistant.components.media_player import (
MediaPlayerDevice, SUPPORT_PAUSE, SUPPORT_SEEK, SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_MUTE, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK,
2015-08-05 11:49:45 +00:00
MEDIA_TYPE_MUSIC, DOMAIN)
2015-08-04 15:22:56 +00:00
from homeassistant.const import (
2015-08-05 11:49:45 +00:00
CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
2015-08-04 15:22:56 +00:00
STATE_IDLE, STATE_PLAYING, STATE_PAUSED, STATE_OFF, STATE_UNKNOWN)
_LOGGER = logging.getLogger(__name__)
SUPPORT_SQUEEZEBOX = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |\
SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_SEEK
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the squeezebox platform. """
2015-08-05 11:49:45 +00:00
if not config.get(CONF_HOST):
_LOGGER.error(
2015-08-05 18:02:39 +00:00
"Missing required configuration items in %s: %s",
2015-08-05 18:09:20 +00:00
DOMAIN,
CONF_HOST)
2015-08-05 11:49:45 +00:00
return False
2015-08-04 15:22:56 +00:00
2015-08-05 11:49:45 +00:00
lms = LogitechMediaServer(
config.get(CONF_HOST),
config.get('port', '9090'),
config.get(CONF_USERNAME),
config.get(CONF_PASSWORD))
2015-08-04 15:22:56 +00:00
2015-08-05 11:49:45 +00:00
if not lms.init_success:
return False
2015-08-04 15:22:56 +00:00
2015-08-05 11:49:45 +00:00
add_devices(lms.create_players())
2015-08-04 15:22:56 +00:00
2015-08-05 11:49:45 +00:00
return True
2015-08-04 15:22:56 +00:00
2015-08-05 11:49:45 +00:00
class LogitechMediaServer(object):
2015-08-05 18:02:39 +00:00
""" Represents a Logitech media server. """
2015-08-05 18:09:20 +00:00
2015-08-05 11:49:45 +00:00
def __init__(self, host, port, username, password):
self.host = host
self.port = port
self._username = username
self._password = password
self.http_port = self._get_http_port()
self.init_success = True if self.http_port else False
def _get_http_port(self):
""" Get http port from media server, it is used to get cover art """
http_port = None
try:
http_port = self.query('pref', 'httpport', '?')
if not http_port:
_LOGGER.error(
2015-08-05 18:02:39 +00:00
"Unable to read data from server %s:%s",
2015-08-05 18:09:20 +00:00
self.host,
self.port)
2015-08-05 11:49:45 +00:00
return
return http_port
except ConnectionError as ex:
_LOGGER.error(
2015-08-05 18:02:39 +00:00
"Failed to connect to server %s:%s - %s",
2015-08-05 18:09:20 +00:00
self.host,
self.port,
ex)
2015-08-05 11:49:45 +00:00
return
def create_players(self):
""" Create a list of SqueezeBoxDevices connected to the LMS """
players = []
count = self.query('player', 'count', '?')
for index in range(0, int(count)):
player_id = self.query('player', 'id', str(index), '?')
player = SqueezeBoxDevice(self, player_id)
players.append(player)
return players
def query(self, *parameters):
2015-08-04 15:22:56 +00:00
""" Send request and await response from server """
2015-08-05 11:49:45 +00:00
telnet = telnetlib.Telnet(self.host, self.port)
if self._username and self._password:
telnet.write('login {username} {password}\n'.format(
username=self._username,
2015-08-04 18:08:48 +00:00
password=self._password).encode('UTF-8'))
telnet.read_until(b'\n', timeout=3)
2015-08-04 15:22:56 +00:00
message = '{}\n'.format(' '.join(parameters))
telnet.write(message.encode('UTF-8'))
response = telnet.read_until(b'\n', timeout=3)\
.decode('UTF-8')\
.split(' ')[-1]\
.strip()
telnet.write(b'exit\n')
return urllib.parse.unquote(response)
2015-08-05 11:49:45 +00:00
def get_player_status(self, player):
""" Get ithe status of a player """
2015-08-04 15:22:56 +00:00
# (title) : Song title
# Requested Information
# a (artist): Artist name 'artist'
# d (duration): Song duration in seconds 'duration'
# K (artwork_url): URL to remote artwork
tags = 'adK'
new_status = {}
2015-08-05 11:49:45 +00:00
telnet = telnetlib.Telnet(self.host, self.port)
2015-08-04 15:22:56 +00:00
telnet.write('{player} status - 1 tags:{tags}\n'.format(
2015-08-05 11:49:45 +00:00
player=player,
2015-08-04 15:22:56 +00:00
tags=tags
).encode('UTF-8'))
response = telnet.read_until(b'\n', timeout=3)\
.decode('UTF-8')\
.split(' ')
telnet.write(b'exit\n')
for item in response:
parts = urllib.parse.unquote(item).partition(':')
new_status[parts[0]] = parts[2]
2015-08-05 11:49:45 +00:00
return new_status
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-public-methods
class SqueezeBoxDevice(MediaPlayerDevice):
""" Represents a SqueezeBox device. """
# pylint: disable=too-many-arguments
def __init__(self, lms, player_id):
super(SqueezeBoxDevice, self).__init__()
self._lms = lms
self._id = player_id
self._name = self._lms.query(self._id, 'name', '?')
self._status = self._lms.get_player_status(self._id)
@property
def name(self):
""" Returns the name of the device. """
return self._name
@property
def state(self):
""" Returns the state of the device. """
if 'power' in self._status and self._status['power'] == '0':
return STATE_OFF
if 'mode' in self._status:
if self._status['mode'] == 'pause':
return STATE_PAUSED
if self._status['mode'] == 'play':
return STATE_PLAYING
if self._status['mode'] == 'stop':
return STATE_IDLE
return STATE_UNKNOWN
def update(self):
""" Retrieve latest state. """
self._status = self._lms.get_player_status(self._name)
2015-08-04 15:22:56 +00:00
@property
def volume_level(self):
""" Volume level of the media player (0..1). """
2015-08-04 17:35:53 +00:00
if 'mixer volume' in self._status:
2015-08-04 15:22:56 +00:00
return int(self._status['mixer volume']) / 100.0
@property
def is_volume_muted(self):
2015-08-04 17:35:53 +00:00
if 'mixer volume' in self._status:
2015-08-04 15:22:56 +00:00
return int(self._status['mixer volume']) < 0
@property
def media_content_id(self):
""" Content ID of current playing media. """
2015-08-04 17:35:53 +00:00
if 'current_title' in self._status:
2015-08-04 15:22:56 +00:00
return self._status['current_title']
@property
def media_content_type(self):
""" Content type of current playing media. """
return MEDIA_TYPE_MUSIC
@property
def media_duration(self):
""" Duration of current playing media in seconds. """
2015-08-04 17:35:53 +00:00
if 'duration' in self._status:
2015-08-04 15:22:56 +00:00
return int(float(self._status['duration']))
@property
def media_image_url(self):
""" Image url of current playing media. """
2015-08-04 17:35:53 +00:00
if 'artwork_url' in self._status:
2015-08-04 15:22:56 +00:00
return self._status['artwork_url']
return 'http://{server}:{port}/music/current/cover.jpg?player={player}'\
.format(
2015-08-05 11:49:45 +00:00
server=self._lms.host,
port=self._lms.http_port,
player=self._id)
2015-08-04 15:22:56 +00:00
@property
def media_title(self):
""" Title of current playing media. """
2015-08-04 17:35:53 +00:00
if 'artist' in self._status and 'title' in self._status:
2015-08-04 15:22:56 +00:00
return '{artist} - {title}'.format(
artist=self._status['artist'],
title=self._status['title']
)
2015-08-04 17:35:53 +00:00
if 'current_title' in self._status:
2015-08-04 15:22:56 +00:00
return self._status['current_title']
@property
def supported_media_commands(self):
""" Flags of media commands that are supported. """
return SUPPORT_SQUEEZEBOX
def turn_off(self):
""" turn_off media player. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'power', '0')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def volume_up(self):
""" volume_up media player. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'mixer', 'volume', '+5')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def volume_down(self):
""" volume_down media player. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'mixer', 'volume', '-5')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def set_volume_level(self, volume):
""" set volume level, range 0..1. """
volume_percent = str(int(volume*100))
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'mixer', 'volume', volume_percent)
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def mute_volume(self, mute):
""" mute (true) or unmute (false) media player. """
mute_numeric = '1' if mute else '0'
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'mixer', 'muting', mute_numeric)
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def media_play_pause(self):
""" media_play_pause media player. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'pause')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def media_play(self):
""" media_play media player. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'play')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def media_pause(self):
""" media_pause media player. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'pause', '0')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def media_next_track(self):
""" Send next track command. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'playlist', 'index', '+1')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def media_previous_track(self):
""" Send next track command. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'playlist', 'index', '-1')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def media_seek(self, position):
""" Send seek command. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'time', position)
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def turn_on(self):
""" turn the media player on. """
2015-08-05 11:49:45 +00:00
self._lms.query(self._id, 'power', '1')
2015-08-04 15:22:56 +00:00
self.update_ha_state()
def play_youtube(self, media_id):
""" Plays a YouTube media. """
raise NotImplementedError()