Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

### Fixed
- `sonos open`, `play-spotify`, and SMAPI open no longer report success when the speaker omits the enqueued track number. Playback is not started on an unknown queue position.

## [0.3.5] - 2026-10-23

**Highlights:** Fix the reachable crypto/x509 vulnerability GO-2026-6809 with Go 1.27.3, explain SMAPI API-client identity rejections, and correctly label initial watch events.
Expand Down
8 changes: 8 additions & 0 deletions internal/sonos/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (c *Client) PlayFromQueueTrack(ctx context.Context, oneBasedTrackNumber int
return c.playFromQueueTrack(ctx, oneBasedTrackNumber)
}

func (c *Client) startEnqueuedPlayback(ctx context.Context, firstTrack int) error {
if firstTrack <= 0 {
// The queue was not cleared, so track 1 can be an older item.
return errors.New("playback was not started because the speaker did not report the new queue position; the track is already on the queue. Run sonos queue list, then sonos queue play <pos> for this speaker. Do not enqueue the track again")
}
return c.playFromQueueTrack(ctx, firstTrack)
}

func (c *Client) playFromQueueTrack(ctx context.Context, oneBasedTrackNumber int) error {
dd, err := c.GetDeviceDescription(ctx)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/sonos/smapi_enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ func (c *Client) EnqueueSMAPIItem(ctx context.Context, svc MusicServiceDescripto
if err != nil {
return 0, err
}
if opts.PlayNow && first > 0 {
if err := c.playFromQueueTrack(ctx, first); err != nil {
if opts.PlayNow {
if err := c.startEnqueuedPlayback(ctx, first); err != nil {
return first, err
}
} else if opts.PlayNow {
_ = c.Play(ctx)
}
return first, nil
}
Expand Down
35 changes: 35 additions & 0 deletions internal/sonos/smapi_enqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,41 @@ func TestEnqueueSMAPIItem(t *testing.T) {
}
}

func TestEnqueueSMAPIItem_PlayNowMissingTrackNumber(t *testing.T) {
t.Parallel()

var actions []string
c := &Client{
IP: "192.0.2.1",
HTTP: &http.Client{Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
action := r.Header.Get("SOAPACTION")
actions = append(actions, action)
if strings.Contains(action, "AddURIToQueue") {
return httpResponse(200, `<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:AddURIToQueueResponse xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"></u:AddURIToQueueResponse></s:Body></s:Envelope>`), nil
}
if strings.Contains(action, "Play") {
return httpResponse(200, `<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:PlayResponse xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"></u:PlayResponse></s:Body></s:Envelope>`), nil
}
t.Fatalf("unexpected action %s", action)
return nil, nil
})},
}

_, err := c.EnqueueSMAPIItem(context.Background(),
MusicServiceDescriptor{ID: "2311", Name: "Spotify"},
SMAPIItem{ID: "track-1", Title: "Song"},
EnqueueOptions{PlayNow: true},
)
if err == nil || !enqueueMissingPositionMessage(err) {
t.Fatalf("error = %v", err)
}
for _, action := range actions {
if strings.Contains(action, "Play") {
t.Fatalf("started playback without a track number: %v", actions)
}
}
}

func TestEnqueueSMAPIItemValidation(t *testing.T) {
t.Parallel()

Expand Down
7 changes: 2 additions & 5 deletions internal/sonos/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ func (c *Client) EnqueueSpotify(ctx context.Context, input string, opts EnqueueO
lastErr = err
continue
}
if opts.PlayNow && first > 0 {
if err := c.playFromQueueTrack(ctx, first); err != nil {
if opts.PlayNow {
if err := c.startEnqueuedPlayback(ctx, first); err != nil {
return first, err
}
} else if opts.PlayNow {
// If the speaker doesn't report a first track number, just call Play.
_ = c.Play(ctx)
}
return first, nil
}
Expand Down
62 changes: 62 additions & 0 deletions internal/sonos/spotify_enqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,65 @@ func TestClientEnqueueSpotify_TrackFallbacksToLegacyURI(t *testing.T) {
t.Fatalf("attempted URIs mismatch: got %v want %v", attemptedURIs, expected)
}
}

func TestClientEnqueueSpotify_PlayNowMissingTrackNumber(t *testing.T) {
t.Parallel()

var actions []string
c := &Client{
IP: "192.0.2.1",
HTTP: &http.Client{Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
if r.Method == http.MethodGet {
return httpResponse(200, `<?xml version="1.0"?><root xmlns="urn:schemas-upnp-org:device-1-0"><device><UDN>uuid:RINCON_ABC1400</UDN></device></root>`), nil
}
action := r.Header.Get("SOAPACTION")
actions = append(actions, action)
if strings.Contains(action, "#AddURIToQueue") {
return httpResponse(200, `<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:AddURIToQueueResponse xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"></u:AddURIToQueueResponse></s:Body></s:Envelope>`), nil
}
if strings.Contains(action, "#Play") {
return httpResponse(200, `<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:PlayResponse xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"></u:PlayResponse></s:Body></s:Envelope>`), nil
}
t.Fatalf("unexpected SOAPACTION %q", action)
return nil, nil
})},
}

_, err := c.EnqueueSpotify(context.Background(), "spotify:track:abc123", EnqueueOptions{PlayNow: true})
if err == nil || !enqueueMissingPositionMessage(err) {
t.Fatalf("error = %v", err)
}
for _, action := range actions {
if strings.Contains(action, "#Play") || strings.Contains(action, "#Seek") {
t.Fatalf("started playback without a track number: %v", actions)
}
}
}

func TestStartEnqueuedPlaybackMissingPosition(t *testing.T) {
t.Parallel()

err := (&Client{}).startEnqueuedPlayback(context.Background(), 0)
if !enqueueMissingPositionMessage(err) {
t.Fatalf("error = %v", err)
}
}

func enqueueMissingPositionMessage(err error) bool {
if err == nil {
return false
}
msg := err.Error()
for _, part := range []string{
"playback was not started",
"already on the queue",
"sonos queue list",
"sonos queue play <pos>",
"Do not enqueue the track again",
} {
if !strings.Contains(msg, part) {
return false
}
}
return true
}