Add Media Browser support to VLC (#72052)

This commit is contained in:
Paulus Schoutsen 2022-05-18 00:15:17 -07:00 committed by GitHub
parent 14361f9587
commit 12020ffac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,11 +6,16 @@ import logging
import vlc
import voluptuous as vol
from homeassistant.components import media_source
from homeassistant.components.media_player import (
PLATFORM_SCHEMA,
BrowseMedia,
MediaPlayerEntity,
MediaPlayerEntityFeature,
)
from homeassistant.components.media_player.browse_media import (
async_process_play_media_url,
)
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
from homeassistant.const import CONF_NAME, STATE_IDLE, STATE_PAUSED, STATE_PLAYING
from homeassistant.core import HomeAssistant
@ -54,6 +59,7 @@ class VlcDevice(MediaPlayerEntity):
| MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.STOP
| MediaPlayerEntityFeature.BROWSE_MEDIA
)
def __init__(self, name, arguments):
@ -158,15 +164,36 @@ class VlcDevice(MediaPlayerEntity):
self._vlc.stop()
self._state = STATE_IDLE
def play_media(self, media_type, media_id, **kwargs):
async def async_play_media(self, media_type, media_id, **kwargs):
"""Play media from a URL or file."""
if media_type != MEDIA_TYPE_MUSIC:
# Handle media_source
if media_source.is_media_source_id(media_id):
sourced_media = await media_source.async_resolve_media(self.hass, media_id)
media_id = sourced_media.url
elif media_type != MEDIA_TYPE_MUSIC:
_LOGGER.error(
"Invalid media type %s. Only %s is supported",
media_type,
MEDIA_TYPE_MUSIC,
)
return
self._vlc.set_media(self._instance.media_new(media_id))
self._vlc.play()
media_id = async_process_play_media_url(self.hass, media_id)
def play():
self._vlc.set_media(self._instance.media_new(media_id))
self._vlc.play()
await self.hass.async_add_executor_job(play)
self._state = STATE_PLAYING
async def async_browse_media(
self, media_content_type=None, media_content_id=None
) -> BrowseMedia:
"""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/"),
)