Add media source support (#306)

This commit is contained in:
Steven Rollason 2022-04-16 22:06:28 +01:00 committed by GitHub
parent d4dc57d683
commit 12179c7fde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
from homeassistant.components import media_source
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
SUPPORT_PLAY, SUPPORT_PLAY,
SUPPORT_PLAY_MEDIA, SUPPORT_PLAY_MEDIA,
@ -8,6 +9,14 @@ from homeassistant.components.media_player import (
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_MUTE,
MediaPlayerEntity, MediaPlayerEntity,
) )
from homeassistant.components.media_player.browse_media import (
async_process_play_media_url,
)
from homeassistant.components.media_player.const import (
MEDIA_TYPE_MUSIC,
MEDIA_TYPE_URL,
SUPPORT_BROWSE_MEDIA,
)
from homeassistant.const import ( from homeassistant.const import (
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
STATE_PAUSED, STATE_PAUSED,
@ -68,6 +77,7 @@ class BrowserModPlayer(MediaPlayerEntity, BrowserModEntity):
| SUPPORT_STOP | SUPPORT_STOP
| SUPPORT_VOLUME_SET | SUPPORT_VOLUME_SET
| SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_MUTE
| SUPPORT_BROWSE_MEDIA
) )
@property @property
@ -88,9 +98,23 @@ class BrowserModPlayer(MediaPlayerEntity, BrowserModEntity):
def mute_volume(self, mute): def mute_volume(self, mute):
self.connection.send("mute", mute=mute) self.connection.send("mute", mute=mute)
def play_media(self, media_type, media_id, **kwargs): async def async_play_media(self, media_type, media_id, **kwargs):
if media_source.is_media_source_id(media_id):
media_type = MEDIA_TYPE_URL
play_item = await media_source.async_resolve_media(self.hass, media_id)
media_id = play_item.url
if media_type in (MEDIA_TYPE_URL, MEDIA_TYPE_MUSIC):
media_id = async_process_play_media_url(self.hass, media_id)
self.connection.send("play", media_content_id=media_id) self.connection.send("play", media_content_id=media_id)
async def async_browse_media(self, media_content_type=None, media_content_id=None):
"""Implement the websocket media browsing helper."""
return await media_source.async_browse_media(
self.hass,
media_content_id,
content_filter=lambda item: item.media_content_type.startswith("audio/"),
)
def media_play(self): def media_play(self):
self.connection.send("play") self.connection.send("play")

View File

@ -1,4 +1,4 @@
{ {
"name": "browser_mod", "name": "browser_mod",
"homeassistant": "2021.5.0" "homeassistant": "2022.3.0"
} }