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:
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).
Unfortunately, setting up the garden speaker in configuration.yaml is not working as intended from either perspective.
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.)
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.
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?
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:
Restarting HA while the garden speaker power is turned off results in no media_player.garden entity existing as before.
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.
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”.
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?