# Stability issues: stale speaker IP, Sonos favorites from music services not pl…ayable, event subscription leak
Hi Vincent, thanks for this integration! Running it with a Sonos One (S2, Ethernet) on Gladys 5.3, I hit a few problems. I went through the code (v2.0.0, `033637d`) to find where they come from. The findings below come from reading the code, not from a debugger on real hardware, so line numbers point to `033637d`.
## 1. A speaker whose IP changes stays unreachable until a manual rescan
**Observed:** from time to time every command, the poll and the queue fail. Editing the "Known IPs" setting fixes it.
**Cause:**
- `SonosClient.discover()` stores the `SonosDevice` instances, with their IP, in `devicesByUuid`, and every command reuses that IP ([SonosClient.js#L276-L311](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L276-L311)).
- Discovery only runs on `connected`, on a scan request, on the `scan_now` action and on `onConfigUpdated` ([index.js](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/index.js)). That is why saving the config "fixes" it: it triggers a rediscovery.
- When the speaker gets a new DHCP lease, nothing notices. `pollDevice` swallows the error at debug level ([devices.js#L143-L148](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/devices.js#L143-L148)), and every command times out after 8 s.
**Made worse by:** `this.devicesByUuid = foundByUuid` ([SonosClient.js#L305](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L305)) runs even when the discovery found nothing, for example after a transient SSDP miss on a reconnection. A single empty scan wipes the working cache, and every command then answers "This Sonos speaker is currently unreachable" until the next scan.
**Suggested fix:**
- On a command or poll failure (timeout, `EHOSTUNREACH`, `ECONNREFUSED`), trigger a debounced rediscovery and retry once.
- Merge discovery results into the cache instead of replacing it (or at least never replace it with an empty map).
- Optionally, persist the last known IPs (in `/data`) and use them as extra topology seeds.
## 2. Sonos favorites from a music service (Spotify…) cannot be played
**Cause:**
- `browse()` uses `ContentDirectoryService.BrowseParsed` ([SonosClient.js#L603-L619](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L603-L619)). `MetadataHelper.ParseDIDLTrack` in `@svrooij/sonos` drops the favorite's `r:resMD`, which carries the service account token (`SA_RINCON…_X_#Svc…-0-Token`).
- The favorite's MediaItem id is its `res` URI, for example `x-rincon-cpcontainer:1006206cspotify%3aplaylist%3a…?sid=12&flags=8300&sn=1`. `playMedia` → `playUri` → `AddUriToQueue` then lets `@svrooij/sonos` guess the metadata from that URI.
- `MetadataHelper.GuessTrack` has no rule for `x-rincon-cpcontainer:…spotify…`. It only handles `spotify:<kind>:<id>` URIs and a few Amazon / SoundCloud / Deezer / Apple container prefixes. So it returns `undefined` and the speaker is sent empty metadata, which Sonos refuses for a cloud container.
- The same applies to stream favorites without a guessing rule (e.g. `x-sonosapi-hls:` radios).
**Suggested fix:**
- Browse `FV:2` with the raw `Browse` and keep `res` + `r:resMD` for each favorite (e.g. map the favorite's ObjectID `FV:2/n` as the MediaItem id).
- At play time, read them back with `BrowseMetadata` and send them verbatim: `AVTransportService.AddURIToQueue({ EnqueuedURI: res, EnqueuedURIMetaData: resMD, … })` for containers and tracks, or `SetAVTransportURI({ CurrentURI: res, CurrentURIMetaData: resMD })` for streams. This is how SoCo / Home Assistant play favorites.
- Side note: favorites have the class `object.itemobject.item.sonos-favorite`, so `upnpClassToMediaType` reports every favorite as a `track`. `r:description` / the class inside `r:resMD` give the real type.
## 3. No catalog or search for music services (missing feature, documentation at least)
The catalog root is only Sonos favorites / Sonos playlists / `R:0/0` ([constants.js](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/constants.js)). `search()` only filters the titles of `FV:2` and `SQ:` ([SonosClient.js#L633-L667](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L633-L667)).
As a user, I expected to find Spotify in "Search" and could not start my music. Browsing a service through the speaker would need SMAPI credentials that a local controller cannot get, so this is probably out of scope. Still, it would help to:
- say it in the docs and in the empty state ("add your Spotify playlists to your Sonos favorites in the Sonos app to find them here");
- point to a Spotify Connect integration for full Spotify search.
Point 2 has to be fixed first, otherwise those favorites still will not play.
## 4. Event subscriptions are leaked on every discovery
Each `tryManager` call creates a new `SonosManager` ([SonosClient.js#L280-L291](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L280-L291)). `Initialize()` → `SubscribeForGroupEvents()` attaches a listener to `ZoneGroupTopologyService.Events`, and that listener makes `@svrooij/sonos`:
- start its event listener;
- send a `SUBSCRIBE` with a callback URL on the container's bridge IP, which the speakers cannot reach;
- set a 10-minute renewal `setInterval`.
These managers are never cancelled (`CancelSubscription()` is never called). So every discovery (startup, each reconnection, each scan, each config save) adds one subscription and one timer per seed. After an IP change, these renewals fail and retry forever.
Since the code comments already state that UPnP eventing cannot reach the container, the simplest fix is `ENV SONOS_DISABLE_EVENTS=1` in the Dockerfile, plus `manager.CancelSubscription()` once the devices have been read.
## 5. `InitializeWithDiscovery(10)` cannot succeed and costs 8 s per discovery
[SonosClient.js#L295](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L295) runs an in-container SSDP search. In the bridge network, multicast never reaches the LAN, so it always ends in the `SONOS_INIT_TIMEOUT_MS` (8 s) timeout, which is also shorter than the 10 s requested. The mediated `gladys.scanNetwork('ssdp')` already covers this case. Dropping that step would make every discovery about 8 s faster.
## 6. Queue panel: errors, and a wrong queue while using Spotify Connect
- **Errors:** the "Queue" panel often shows an error. As far as I can tell, these are the consequences of point 1 (stale IP → 8 s timeout, or wiped cache → "unreachable").
- **Spotify Connect:** when playback was started from the Spotify app, the transport source is `x-sonos-vli:…` and the Sonos queue `Q:0` is not used. `getQueue` ([SonosClient.js#L677-L700](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/SonosClient.js#L677-L700)) still returns the old Sonos queue, with `currentIndex = Track - 1` pointing at an unrelated item. Checking `GetMediaInfo().CurrentURI` (or `GetPositionInfo().TrackURI`) and returning an empty queue with `currentIndex: null` when the source is not `x-rincon-queue:` would avoid showing a misleading queue.
## Minor
The catalog root folders always use `label.en` ([devices.js#L214-L224](https://github.com/vincentBesseau/gladys-sonos/blob/033637d1d08783c9417e9d84df83060307fb9879/src/devices.js#L214-L224)), so a French user sees "Sonos favorites" / "Radio stations".
---
_Generated by [Claude Code](https://claude.ai/code)_