How to access the media source list in templates?

That what you see here is only available in the frontend, but not for programming/templating purposes.

Can you give an example use?

Not sure if your reference is the same, but the following works.

service: media_player.play_media
data:
  media_content_id: "x-sonosapi-hls-static:ALkSOiGOqT4NOsTCYpKs5I5vEoOF85V3OqKmhZY5op7biKrQHlysYSU792MKCxopJDlkP6ZKvWP6seoJW-hIaQ?sid=284&flags=0&sn=13"
  media_content_type: music
target:
  entity_id: media_player.living_room_2
1 Like

If that is shown as an attribute of the media player you most definitely should be able to access the list with:

{{state_attr('media_player.myradio', 'source_list') }}

Or if you want the third item from the list:

{{ state_attr('media_player.myradio', 'source_list')[2] }}

The following works as well.

service: media_player.select_source
data:
  source: 1. T'Hits YouTube
target:
  entity_id: media_player.living_room_2
1 Like

yeah, would be. but as mentioned it is not available as an attribute

I can see the source list is an attribute in Coolie’s image.

Share the full code you are using so we can see why it is not working.

that’s correct. that works for a single song/radio station/playlist.
But I need to access the whole list, e.g. for text2speech purposes to tell, what choice you have.

But the whole list is available.

Yes. But again, it is missing in my lsit
grafik

Does it show in Sonos App like below?
image

What does the template show?
{{ state_attr('media_player.living_room', 'source_list') }}

The app shows it similar to yours, the state_attr call fails as “source_list” does not exist.
I currently use the latest homeassistant version 2022.4.1. Which version do you use?

2022.3.7

When did it last work for you?

Try deleting and re-adding the integration?, else it’s probably a bug.

FYI. I tend to stay away from updates unless absolutely necessary, or a feature I need has been added, or after the bugs have been worked out, usually around release 2022.4.3+

Thanks. I updated yesterday evening, because I needed the new timer functions. So I create a bug report

This is why it is still working for you.

The attribute was removed in 2022.4 to reduce database activity.

1 Like

Had no idea, my apologies @gpgmailencrypt

But is there still access to the data?

I’ve been reading the release notes and new documentation and it does not mention it. So if there is a way it is undocumented.

If I were you I would open an issue here:

https://github.com/home-assistant/core/issues?q=is%3Aissue+label%3A"integration%3A+sonos"+is%3Aopen

Best case: it needs just needs documenting and someone will explain how you can do it and close the issue.
Worst case: it needs reverting to recover the functionality in which case the issue will be closed and you will be told to open a feature request here on the forum.

It’s the question I asked in response to the release notes. It would be too pity if there is no option to list these favorites and it would even be worse if they decide to remove the media_player.select_source without a replacement too…

The media_player.select_source service call will continue to accept favorite names. This compatibility feature may be removed in a future release, but there are no current plans to do so.

For now a workaround could be in creating a select list that contains the favorites and then do a media_player.select_source on select. It does however result in a double administration, which is not at all ideal.

The source_list attribute used to be stored in the state machine (like any entity’s state and its attributes). As of 2022.4.0, the attribute was eliminated and source_list is now stored in the hidden .storage/core.entity_registry file as part of a (Sonos) media_player’s capabilities.

Screenshot from 2022-04-12 17-44-17

I don’t know of any template function able to get an entity’s data stored in core.entity_registry.

In contrast, for a device, the device_attr function allows you to get data from the core.device_registry file.

If there was an equivalent entity_attr function, then one could get an entity’s data, like capabilities and platform, stored in core.entity_registry. Perhaps this merits a Feature Request.


EDIT

I was wrong. The author of the Sonos integration (jjlawren) explained the source_list in storage/core.entity_registry is created at the moment the Sonos media_player entity is created and then never updated again. There’s no point in attempting to access it because it’s a static list.

Add Sonos favorites sensor by jjlawren · Pull Request #70235 · home-assistant/core · GitHub will be in 2022.5 which adds a new sensor.sonos_favorites entity that contains the original list, plus each media_content_id for use in media_player.play_media. See the linked PR for template examples.

1 Like