Skip to content
Merged
Changes from 4 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
21 changes: 16 additions & 5 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, {}): {cv.string: cv.string}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vol.Optional(CONF_ALIASES, default={}) or similar per @bieniu

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@happyleavesaoc With your change the component starts with HA and in log there is information about new device. But when I add this device as an alias in configuration and turn off all Spotify players in log every minute appears this error:

2017-06-01 10:53:17 ERROR (MainThread) [homeassistant.helpers.entity] Update for media_player.spotify fails Traceback (most recent call last):
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/entity.py", line 225, in async_update_ha_state None, self.update)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__ yield self # This tells Task to wait for completion. File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup value = future.result() File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run result = self.fn(*self.args, **self.kwargs) File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/media_player/spotify.py", line 161, in update devices = self._player.devices().get('devices') AttributeError: 'NoneType' object has no attribute 'get'

And after HA restart there is no Spotify component at all but only error:

2017-06-01 10:39:29 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved Traceback (most recent call last):
File "/usr/lib/python3.4/asyncio/tasks.py", line 233, in _step result = coro.throw(exc) File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 361, in async_process_entity new_entity, self, update_before_add=update_before_add
File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/helpers/entity_component.py", line 191, in async_add_entity yield from self.hass.loop.run_in_executor(None, entity.update)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__ yield self # This tells Task to wait for completion. File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup value = future.result() File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run result = self.fn(*self.args, **self.kwargs) File "/srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/media_player/spotify.py", line 161, in update devices = self._player.devices().get('devices') AttributeError: 'NoneType' object has no attribute 'get'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a None check and the default try the most recent commit.

})

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 @@ -156,8 +160,15 @@ def update(self):
# Available devices
devices = self._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}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'old_devices'

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