mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Add media source support to Yamaha MusicCast (#67572)
This commit is contained in:
parent
c9ac0b49f6
commit
46984afbb8
@ -1,17 +1,22 @@
|
||||
"""Implementation of the musiccast media player."""
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import logging
|
||||
|
||||
from aiomusiccast import MusicCastGroupException, MusicCastMediaContent
|
||||
from aiomusiccast.features import ZoneFeature
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import media_source
|
||||
from homeassistant.components.media_player import (
|
||||
PLATFORM_SCHEMA,
|
||||
BrowseMedia,
|
||||
MediaPlayerEntity,
|
||||
)
|
||||
from homeassistant.components.media_player.browse_media import (
|
||||
async_process_play_media_url,
|
||||
)
|
||||
from homeassistant.components.media_player.const import (
|
||||
MEDIA_CLASS_DIRECTORY,
|
||||
MEDIA_CLASS_TRACK,
|
||||
@ -333,6 +338,10 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
|
||||
|
||||
async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> None:
|
||||
"""Play media."""
|
||||
if media_source.is_media_source_id(media_id):
|
||||
play_item = await media_source.async_resolve_media(self.hass, media_id)
|
||||
media_id = play_item.url
|
||||
|
||||
if self.state == STATE_OFF:
|
||||
await self.async_turn_on()
|
||||
|
||||
@ -353,7 +362,9 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
|
||||
)
|
||||
return
|
||||
|
||||
if parts[0] == "http":
|
||||
if parts[0] in ("http", "https") or media_id.startswith("/"):
|
||||
media_id = async_process_play_media_url(self.hass, media_id)
|
||||
|
||||
await self.coordinator.musiccast.play_url_media(
|
||||
self._zone_id, media_id, "HomeAssistant"
|
||||
)
|
||||
@ -365,6 +376,15 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
|
||||
|
||||
async def async_browse_media(self, media_content_type=None, media_content_id=None):
|
||||
"""Implement the websocket media browsing helper."""
|
||||
if media_content_id and media_source.is_media_source_id(media_content_id):
|
||||
return await media_source.async_browse_media(
|
||||
self.hass,
|
||||
media_content_id,
|
||||
content_filter=lambda item: item.media_content_type.startswith(
|
||||
"audio/"
|
||||
),
|
||||
)
|
||||
|
||||
if self.state == STATE_OFF:
|
||||
raise HomeAssistantError(
|
||||
"The device has to be turned on to be able to browse media."
|
||||
@ -375,11 +395,13 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
|
||||
media_content_provider = await MusicCastMediaContent.browse_media(
|
||||
self.coordinator.musiccast, self._zone_id, media_content_path, 24
|
||||
)
|
||||
add_media_source = False
|
||||
|
||||
else:
|
||||
media_content_provider = MusicCastMediaContent.categories(
|
||||
self.coordinator.musiccast, self._zone_id
|
||||
)
|
||||
add_media_source = True
|
||||
|
||||
def get_content_type(item):
|
||||
if item.can_play:
|
||||
@ -399,6 +421,21 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
|
||||
for child in media_content_provider.children
|
||||
]
|
||||
|
||||
if add_media_source:
|
||||
with contextlib.suppress(media_source.BrowseError):
|
||||
item = await media_source.async_browse_media(
|
||||
self.hass,
|
||||
None,
|
||||
content_filter=lambda item: item.media_content_type.startswith(
|
||||
"audio/"
|
||||
),
|
||||
)
|
||||
# If domain is None, it's overview of available sources
|
||||
if item.domain is None:
|
||||
children.extend(item.children)
|
||||
else:
|
||||
children.append(item)
|
||||
|
||||
overview = BrowseMedia(
|
||||
title=media_content_provider.title,
|
||||
media_class=MEDIA_CLASS_MAPPING.get(media_content_provider.content_type),
|
||||
|
Loading…
x
Reference in New Issue
Block a user