diff --git a/blog/2025-04-17-search-media-play.md b/blog/2025-04-17-search-media-play.md new file mode 100644 index 00000000..ac055246 --- /dev/null +++ b/blog/2025-04-17-search-media-play.md @@ -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) diff --git a/docs/core/entity/media-player.md b/docs/core/entity/media-player.md index fc3504ea..465e1581 100644 --- a/docs/core/entity/media-player.md +++ b/docs/core/entity/media-player.md @@ -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.