Discover/Recognize Sonos media player added while HA is running?

I have a number of Sonos speakers around the house as well as one in the garden. Those in the house are always powered on and therefore always seen by HA. The Sonos in the garden is normally not powered on unless we’re going to be outside. Power for the garden speaker is controlled by an ESPHome-flashed ESP8266 power outlet which is in turn controlled through the front end or Google Assistant. See the switch at the bottom of this grab:

If power to the garden speaker is already switched on when HA restarts, it recognizes that speaker along with the others. However, if HA is already running when I switch on the speaker, HA never recognizes, and I can’t control it through HA. For example, adding the speaker to the group with mini-media-player as shown above does nothing. The States group looks like this:

The garden speaker shows up and is controllable through the Sonos app:

Is there a way to force HA to discover/recognize a Sonos speaker if it is turned on while HA is running?

Thanks.

I’m afraid I don’t have the definitive answer to your question but I think it is ‘no’ (but if it isn’t I’d be very interested too).

However, I have to ask, what is your Lovelace config for that card, especially the ‘Title Bar’ across the top?

Define your garden Sonos speaker in your config so you are not reliant on discovery. See the second and third examples here: https://www.home-assistant.io/compon

I’d do it for all three so you are not reliant on discovery.

You will need to give the speakers static IP addresses (or DHCP reservations at least).

Like I said… :wink:

2 Likes

Ah, of course! Thanks. I actually used to have them defined in the config but went to discovery when I started moving them around.

Do you know if I can define only the garden speaker in the config and rely on discovery for the rest?

Defining one should not prevent discovery finding the other two.

Unfortunately, setting up the garden speaker in configuration.yaml is not working as intended from either perspective.

  1. If the garden speaker is not turned on when HA starts, it is not added as an entity even though it is defined by IP in the config. (If it is turned on when HA starts, then it is added as an entity as expected.)
  2. Including a sonos: section in the config results in the rest of the speakers not being discovered.

Any other ideas? If there’s a way to add an entity after a restart (service call maybe?), there are a couple of different triggers I could use when power to the speaker is turned on.

Cheers.

I had a look but could not find a service to start discovery.

Have you checked the troubleshooting tips at the bottom of this document

It is not currently possible but I am actually working on continuous Sonos discovery right now.

I expect that feature to land in 0.93 (though it might be 0.94 if some problem turns up).

2 Likes

Wow, @amelchio . Excellent timing. Thanks.

Let me know if you need any help testing!

I now created PR #23484 for this.

To test that, either create a custom component from the files in that branch – or wait for the 0.93 beta :slight_smile:

Happy to try as a custom component. I’m still a bit confused by the great migration, though. In order not to override the standard component, should I just rename your media_player.py to something else?

Frankly, I am also a bit confused. I think it should work to just copy those 5 files to <config>/custom_components/sonos/

Thanks. Copying the files to <config>/custom_components/sonos/ worked.

In the limited testing I’ve done so far, continuous discovery appears to work just as expected. Big thanks for the PR!

In my set up:

  1. Restarting HA while the garden speaker power is turned off results in no media_player.garden entity existing as before.
  2. Turning on power to the garden speaker results in media_player.garden being discovered after the speaker has had time to power on and join the network. It appears in HA very shortly after it appears in the Sonos native app.
  3. Turning off power to the garden speaker results in the media_player.garden state changing to “unavailable” after a period of time. The entity does not disappear. It appears to take about two minutes for the state to change to “unavailable”.
  4. On the next HA restart with power to the speaker turned off, the entity does not exist as in point 1 above.

@amelchio Does the component poll for new Sonos devices on a schedule, or does it work another way? Also, what is the trigger for the component to notice that a speaker is now unavailable? Thx

For completeness, I plan to create automations that trigger when media_player.garden comes on or goes offline. I can know when it gets discovered and is available initially after a restart by watching for

{{ states("media_player.garden") == 'unknown' }}

to change from True to False, and then watch again for the state to change to unavailable. Or… any better methods?

That is exactly how it works now. It polls for new devices every minute and missing devices are marked unavailable after two minutes.

I am no automation expert so I will keep out of that part.

1 Like