Add media file playback to DuneHD (#105796)

* Add media player play file option

* Add media playback from HA

* Remove volume capability

* Add missing imports

* Apply suggestions from code review

* Update homeassistant/components/dunehd/media_player.py

* Apply suggestions from code review

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
pull/109505/head
Imre Liessens 2024-02-16 15:20:09 +01:00 committed by GitHub
parent f9dc92a9a0
commit 085f75ef1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 1 deletions

View File

@ -5,10 +5,14 @@ from typing import Any, Final
from pdunehd import DuneHDPlayer
from homeassistant.components import media_source
from homeassistant.components.media_player import (
BrowseMedia,
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
MediaType,
async_process_play_media_url,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -26,6 +30,8 @@ DUNEHD_PLAYER_SUPPORT: Final[MediaPlayerEntityFeature] = (
| MediaPlayerEntityFeature.PREVIOUS_TRACK
| MediaPlayerEntityFeature.NEXT_TRACK
| MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.BROWSE_MEDIA
)
@ -115,7 +121,7 @@ class DuneHDPlayerEntity(MediaPlayerEntity):
self._state = self._player.turn_off()
def turn_on(self) -> None:
"""Turn off media player."""
"""Turn on media player."""
self._state = self._player.turn_on()
def media_play(self) -> None:
@ -126,6 +132,32 @@ class DuneHDPlayerEntity(MediaPlayerEntity):
"""Pause media player."""
self._state = self._player.pause()
async def async_play_media(
self, media_type: MediaType | str, media_id: str, **kwargs: Any
) -> None:
"""Play media from a URL or file."""
# Handle media_source
if media_source.is_media_source_id(media_id):
sourced_media = await media_source.async_resolve_media(
self.hass, media_id, self.entity_id
)
media_id = sourced_media.url
# If media ID is a relative URL, we serve it from HA.
media_id = async_process_play_media_url(self.hass, media_id)
self._state = await self.hass.async_add_executor_job(
self._player.launch_media_url, media_id
)
async def async_browse_media(
self,
media_content_type: MediaType | str | None = None,
media_content_id: str | None = None,
) -> BrowseMedia:
"""Implement the websocket media browsing helper."""
return await media_source.async_browse_media(self.hass, media_content_id)
@property
def media_title(self) -> str | None:
"""Return the current media source."""