Spotify aliases (#7702)
* Alias support for spotify devices * Fix log * Formatting/Fixes * Remove default arg * Add default keyword * None check
This commit is contained in:
parent
78887c5d5c
commit
cefacf9ce4
1 changed files with 19 additions and 6 deletions
|
@ -40,6 +40,7 @@ AUTH_CALLBACK_NAME = 'api:spotify'
|
||||||
ICON = 'mdi:spotify'
|
ICON = 'mdi:spotify'
|
||||||
DEFAULT_NAME = 'Spotify'
|
DEFAULT_NAME = 'Spotify'
|
||||||
DOMAIN = 'spotify'
|
DOMAIN = 'spotify'
|
||||||
|
CONF_ALIASES = 'aliases'
|
||||||
CONF_CLIENT_ID = 'client_id'
|
CONF_CLIENT_ID = 'client_id'
|
||||||
CONF_CLIENT_SECRET = 'client_secret'
|
CONF_CLIENT_SECRET = 'client_secret'
|
||||||
CONF_CACHE_PATH = 'cache_path'
|
CONF_CACHE_PATH = 'cache_path'
|
||||||
|
@ -52,7 +53,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_CLIENT_ID): cv.string,
|
vol.Required(CONF_CLIENT_ID): cv.string,
|
||||||
vol.Required(CONF_CLIENT_SECRET): cv.string,
|
vol.Required(CONF_CLIENT_SECRET): cv.string,
|
||||||
vol.Optional(CONF_NAME): 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)
|
SCAN_INTERVAL = timedelta(seconds=30)
|
||||||
|
@ -89,7 +91,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
configurator = get_component('configurator')
|
configurator = get_component('configurator')
|
||||||
configurator.request_done(hass.data.get(DOMAIN))
|
configurator.request_done(hass.data.get(DOMAIN))
|
||||||
del hass.data[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)
|
add_devices([player], True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,7 +120,7 @@ class SpotifyAuthCallbackView(HomeAssistantView):
|
||||||
class SpotifyMediaPlayer(MediaPlayerDevice):
|
class SpotifyMediaPlayer(MediaPlayerDevice):
|
||||||
"""Representation of a Spotify controller."""
|
"""Representation of a Spotify controller."""
|
||||||
|
|
||||||
def __init__(self, oauth, name):
|
def __init__(self, oauth, name, aliases):
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._oauth = oauth
|
self._oauth = oauth
|
||||||
|
@ -128,10 +131,11 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
|
||||||
self._image_url = None
|
self._image_url = None
|
||||||
self._state = STATE_UNKNOWN
|
self._state = STATE_UNKNOWN
|
||||||
self._current_device = None
|
self._current_device = None
|
||||||
self._devices = None
|
self._devices = {}
|
||||||
self._volume = None
|
self._volume = None
|
||||||
self._shuffle = False
|
self._shuffle = False
|
||||||
self._player = None
|
self._player = None
|
||||||
|
self._aliases = aliases
|
||||||
self._token_info = self._oauth.get_cached_token()
|
self._token_info = self._oauth.get_cached_token()
|
||||||
|
|
||||||
def refresh_spotify_instance(self):
|
def refresh_spotify_instance(self):
|
||||||
|
@ -154,10 +158,19 @@ class SpotifyMediaPlayer(MediaPlayerDevice):
|
||||||
"""Update state and attributes."""
|
"""Update state and attributes."""
|
||||||
self.refresh_spotify_instance()
|
self.refresh_spotify_instance()
|
||||||
# Available devices
|
# 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:
|
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}
|
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 playback state
|
||||||
current = self._player.current_playback()
|
current = self._player.current_playback()
|
||||||
if current is None:
|
if current is None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue