Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions homeassistant/components/media_player/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ICON = 'mdi:spotify'
DEFAULT_NAME = 'Spotify'
DOMAIN = 'spotify'
CONF_ALIASES = 'aliases'
CONF_CLIENT_ID = 'client_id'
CONF_CLIENT_SECRET = 'client_secret'
CONF_CACHE_PATH = 'cache_path'
Expand All @@ -52,7 +53,8 @@
vol.Required(CONF_CLIENT_ID): cv.string,
vol.Required(CONF_CLIENT_SECRET): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_CACHE_PATH): cv.string
vol.Optional(CONF_CACHE_PATH): cv.string,
vol.Optional(CONF_ALIASES, default={}): {cv.string: cv.string}
})

SCAN_INTERVAL = timedelta(seconds=30)
Expand Down Expand Up @@ -89,7 +91,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
configurator = get_component('configurator')
configurator.request_done(hass.data.get(DOMAIN))
del hass.data[DOMAIN]
player = SpotifyMediaPlayer(oauth, config.get(CONF_NAME, DEFAULT_NAME))
player = SpotifyMediaPlayer(oauth, config.get(CONF_NAME, DEFAULT_NAME),
config[CONF_ALIASES])
add_devices([player], True)


Expand Down Expand Up @@ -117,7 +120,7 @@ def get(self, request):
class SpotifyMediaPlayer(MediaPlayerDevice):
"""Representation of a Spotify controller."""

def __init__(self, oauth, name):
def __init__(self, oauth, name, aliases):
"""Initialize."""
self._name = name
self._oauth = oauth
Expand All @@ -128,10 +131,11 @@ def __init__(self, oauth, name):
self._image_url = None
self._state = STATE_UNKNOWN
self._current_device = None
self._devices = None
self._devices = {}
self._volume = None
self._shuffle = False
self._player = None
self._aliases = aliases
self._token_info = self._oauth.get_cached_token()

def refresh_spotify_instance(self):
Expand All @@ -154,10 +158,19 @@ def update(self):
"""Update state and attributes."""
self.refresh_spotify_instance()
# Available devices
devices = self._player.devices().get('devices')
player_devices = self._player.devices()
if player_devices is not None:
devices = player_devices.get('devices')
if devices is not None:
self._devices = {device.get('name'): device.get('id')
old_devices = self._devices
self._devices = {self._aliases.get(device.get('id'),
device.get('name')):
device.get('id')
for device in devices}
device_diff = {name: id for name, id in self._devices.items()
if old_devices.get(name, None) is None}
if len(device_diff) > 0:
_LOGGER.info("New Devices: %s", str(device_diff))
# Current playback state
current = self._player.current_playback()
if current is None:
Expand Down