Skip to content

Commit

Permalink
[ 1.0.48 ] * Updated service player_media_play_track_favorites to t…
Browse files Browse the repository at this point in the history
…ransfer the Spotify users track favorites (200 max) to the Sonos device and play them from a local queue. See the [Sonos Limitations](https://github.com/thlucas1/homeassistantcomponent_spotifyplus/wiki/Spotify-Connect-Brand-Notes#sonos) wiki documentation for further details about Sonos-related issues.
  • Loading branch information
thlucas1 committed Aug 10, 2024
1 parent 557bf8c commit b234491
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Change are listed in reverse chronological order (newest to oldest).

<span class="changelog">

###### [ 1.0.48 ] - 2024/08/10

* Updated service `player_media_play_track_favorites` to transfer the Spotify users track favorites (200 max) to the Sonos device and play them from a local queue. See the [Sonos Limitations](https://github.com/thlucas1/homeassistantcomponent_spotifyplus/wiki/Spotify-Connect-Brand-Notes#sonos) wiki documentation for further details about Sonos-related issues.

###### [ 1.0.47 ] - 2024/08/05

* Added configuration option to hide specified device names from the HA media player UI source list.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/spotifyplus/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"urllib3>=1.21.1,<1.27",
"zeroconf>=0.132.2"
],
"version": "1.0.47",
"version": "1.0.48",
"zeroconf": [ "_spotify-connect._tcp.local." ]
}
70 changes: 65 additions & 5 deletions custom_components/spotifyplus/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
SpotifyConnectDevice,
SpotifyConnectDevices,
Track,
TrackSaved,
TrackPage,
TrackPageSaved,
UserProfile
Expand Down Expand Up @@ -860,6 +861,8 @@ def set_shuffle(self, shuffle: bool) -> None:
sonos_repeat = False
elif self._playerState.RepeatState == 'context':
sonos_repeat = True
else: # assume off if nothing else.
sonos_repeat = False
self._sonosDevice.play_mode = SONOS_PLAY_MODE_BY_MEANING[(shuffle, sonos_repeat)]

# give SoCo api time to process the change.
Expand Down Expand Up @@ -3588,13 +3591,66 @@ def service_spotify_player_media_play_track_favorites(self,
apiMethodParms.AppendKeyValue("shuffle", shuffle)
apiMethodParms.AppendKeyValue("delay", delay)
_logsi.LogMethodParmList(SILevel.Verbose, "Spotify Player Media Play Favorite Tracks Service", apiMethodParms)

# see if default device option was specified (e.g. deviceId="*").
deviceId = self._GetDefaultDeviceOption(deviceId)

# validations.
delay = validateDelay(delay, 0.50, 10)
if deviceId == '':
deviceId = None
if deviceId is None or deviceId == "*":
deviceId = PlayerDevice.GetIdFromSelectItem(self.data.OptionDeviceDefault)

# get selected device reference from cached list of Spotify Connect devices.
scDevices:SpotifyConnectDevices = self.data.spotifyClient.GetSpotifyConnectDevices(refresh=False)
scDevice:SpotifyConnectDevice = scDevices.GetDeviceByName(deviceId)
if scDevice is None:
scDevice = scDevices.GetDeviceById(deviceId)

# start playing track favorites on the specified Spotify Connect device.
_logsi.LogVerbose("Playing Media Favorite Tracks on device")
self.data.spotifyClient.PlayerMediaPlayTrackFavorites(deviceId, shuffle, delay)
if (scDevice is not None) and (scDevice.DeviceInfo.IsBrandSonos):

# get current users favorite tracks.
tracks:TrackPageSaved = self.data.spotifyClient.GetTrackFavorites(limitTotal=200)
if (tracks.ItemsCount == 0):
_logsi.LogVerbose("Current user has no favorite tracks; nothing to do")
return

offsetPosition:int = 0

# build a list of all item uri's.
arrUris:list[str] = []
trackSaved:TrackSaved
for trackSaved in tracks.Items:
arrUris.append(trackSaved.Track.Uri)

# for Sonos, use the SoCo API command.
sonosDevice = SoCo(scDevice.DiscoveryResult.HostIpAddress)
_logsi.LogVerbose("'%s': Issuing command to Sonos device '%s' ('%s'): CLEAR_QUEUE" % (self.name, sonosDevice.ip_address, sonosDevice.player_name))
sonosDevice.clear_queue()
sharelink = ShareLinkPlugin(sonosDevice)
for uri in arrUris:
_logsi.LogVerbose("'%s': Issuing command to Sonos device '%s' ('%s'): ADD_SHARE_LINK_TO_QUEUE (uri=%s)" % (self.name, sonosDevice.ip_address, sonosDevice.player_name, uri))
sharelink.add_share_link_to_queue(uri)

# set desired shuffle mode.
self.service_spotify_player_set_shuffle_mode(state=shuffle, deviceId=deviceId, delay=delay)

# skip first track if shuffle is enabled.
if (len(arrUris) > 1) and (shuffle is True):
offsetPosition = 1

# play the queue.
_logsi.LogVerbose("'%s': Issuing command to Sonos device '%s' ('%s'): PLAY_FROM_QUEUE (index=%s)" % (self.name, sonosDevice.ip_address, sonosDevice.player_name, offsetPosition))
sonosDevice.play_from_queue(index=offsetPosition)

# give SoCo api time to process the change.
if delay > 0:
_logsi.LogVerbose(TRACE_MSG_DELAY_DEVICE_SONOS % delay)
time.sleep(delay)

else:

# for everything else, just use the Spotify Web API command.
self.data.spotifyClient.PlayerMediaPlayTrackFavorites(deviceId, shuffle, delay)

# update ha state.
self.schedule_update_ha_state(force_refresh=False)
Expand Down Expand Up @@ -3956,6 +4012,8 @@ def service_spotify_player_set_shuffle_mode(
deviceId = None
if deviceId is None or deviceId == "*":
deviceId = PlayerDevice.GetIdFromSelectItem(self.data.OptionDeviceDefault)
if state is None:
state = False

# get selected device reference from cached list of Spotify Connect devices.
scDevices:SpotifyConnectDevices = self.data.spotifyClient.GetSpotifyConnectDevices(refresh=False)
Expand All @@ -3979,6 +4037,8 @@ def service_spotify_player_set_shuffle_mode(
sonos_repeat = False
elif playerState.RepeatState == 'context':
sonos_repeat = True
else: # assume off if nothing else.
sonos_repeat = False
playMode:str = SONOS_PLAY_MODE_BY_MEANING[(state, sonos_repeat)]

# for Sonos, use the SoCo API command.
Expand Down
2 changes: 2 additions & 0 deletions spotifyplus.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<Compile Include="custom_components\spotifyplus\__init__.py" />
<Compile Include="test\testVS_Sonos_AvTransport.py" />
<Compile Include="test\testVS_Sonos_Base.py" />
<Compile Include="test\testVS_Sonos_MusicServices.py" />
<Compile Include="test\testVS_Sonos_MusicService_Spotify.py" />
</ItemGroup>
<ItemGroup>
<Folder Include=".github\" />
Expand Down

0 comments on commit b234491

Please sign in to comment.