Add media source support to openhome (#67566)

This commit is contained in:
Paulus Schoutsen 2022-03-14 12:14:21 -07:00 committed by GitHub
parent 314175135f
commit a109889f13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,9 +10,14 @@ from async_upnp_client.client import UpnpError
from openhomedevice.device import Device
import voluptuous as vol
from homeassistant.components import media_source
from homeassistant.components.media_player import MediaPlayerEntity
from homeassistant.components.media_player.browse_media import (
async_process_play_media_url,
)
from homeassistant.components.media_player.const import (
MEDIA_TYPE_MUSIC,
SUPPORT_BROWSE_MEDIA,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PLAY,
@ -149,7 +154,10 @@ class OpenhomeDevice(MediaPlayerEntity):
if self._source["type"] == "Radio":
self._supported_features |= (
SUPPORT_STOP | SUPPORT_PLAY | SUPPORT_PLAY_MEDIA
SUPPORT_STOP
| SUPPORT_PLAY
| SUPPORT_PLAY_MEDIA
| SUPPORT_BROWSE_MEDIA
)
if self._source["type"] in ("Playlist", "Spotify"):
self._supported_features |= (
@ -158,6 +166,7 @@ class OpenhomeDevice(MediaPlayerEntity):
| SUPPORT_PAUSE
| SUPPORT_PLAY
| SUPPORT_PLAY_MEDIA
| SUPPORT_BROWSE_MEDIA
)
if self._in_standby:
@ -189,6 +198,11 @@ class OpenhomeDevice(MediaPlayerEntity):
@catch_request_errors()
async def async_play_media(self, media_type, media_id, **kwargs):
"""Send the play_media command to the media player."""
if media_source.is_media_source_id(media_id):
media_type = MEDIA_TYPE_MUSIC
play_item = await media_source.async_resolve_media(self.hass, media_id)
media_id = play_item.url
if media_type != MEDIA_TYPE_MUSIC:
_LOGGER.error(
"Invalid media type %s. Only %s is supported",
@ -196,6 +210,9 @@ class OpenhomeDevice(MediaPlayerEntity):
MEDIA_TYPE_MUSIC,
)
return
media_id = async_process_play_media_url(self.hass, media_id)
track_details = {"title": "Home Assistant", "uri": media_id}
await self._device.play_media(track_details)
@ -320,3 +337,11 @@ class OpenhomeDevice(MediaPlayerEntity):
async def async_mute_volume(self, mute):
"""Mute (true) or unmute (false) media player."""
await self._device.set_mute(mute)
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/"),
)