Add search capabilities to media player (#2625)

* Add search to media_player

* add blog post

* fix typos

* Add blog post on searching in media players

* Update blog/2025-04-17-search-media-play.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update 2025-04-17-search-media-play.md

* Update media-player.md

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Josef Zweck 2025-04-16 23:19:03 +02:00 committed by GitHub
parent 87162ada68
commit 9c863753db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,8 @@
---
author: Josef Zweck
authorURL: https://github.com/zweckj
authorImageURL: https://avatars.githubusercontent.com/u/24647999?v=4
title: "Searching in media players"
---
Media players can now allow users to search through the media, by adding `MediaEntityFeature.SEARCH_MEDIA` and implementing `async_search_media`. The users can filter the search result by a search query and a list of `MediaClasses` that the returned results should have. For more info, see [the updated documentation.](/docs/core/entity/media-player#search-media)

View File

@ -67,6 +67,7 @@ and are combined using the bitwise or (`|`) operator.
| `PLAY_MEDIA` | Entity allows playing media sources. |
| `PREVIOUS_TRACK` | Entity allows returning back to a previous media track. |
| `REPEAT_SET` | Entity allows setting repeat. |
| `SEARCH_MEDIA` | Entity allows searching for media. |
| `SEEK` | Entity allows seeking position during playback of media. |
| `SELECT_SOUND_MODE` | Entity allows selecting a sound mode. |
| `SELECT_SOURCE` | Entity allows selecting a source/input. |
@ -192,6 +193,32 @@ class MyMediaPlayer(MediaPlayerEntity):
await self._media_player.play_url(media_id)
```
### Search media
If the media player supports searching media, it should implement the following method:
```python
class MyMediaPlayer(MediaPlayerEntity):
async def async_search_media(
self,
query: SearchMediaQuery,
) -> SearchMedia:
"""Search the media player."""
# search for the requested media on your library client.
result = await my_client.search(query=query.search_query)
return SearchMedia(result=result)
```
The SearchMediaQuery is a dataclass with the following properties:
| Attribute | Type | Default | Description |
|-----------------------|---------------------------------------|-------------|------------------------------------|
| `search_query` | `str` | *required* | The search string or query. |
| `media_content_type` | `MediaType \| str \| None` | `None` | The content type to search inside. |
| `media_content_id` | `str \| None` | `None` | The content ID to search inside. |
| `media_filter_classes`| `list[MediaClass] \| None` | `None` | List of media classes to filter. |
### Select sound mode
Optional. Switch the sound mode of the media player.