2020-06-03 12:01:56 +00:00
|
|
|
"""Dune HD implementation of the media player."""
|
2021-05-24 19:09:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, Final
|
|
|
|
|
|
|
|
from pdunehd import DuneHDPlayer
|
2016-12-03 19:46:04 +00:00
|
|
|
|
2024-02-16 14:20:09 +00:00
|
|
|
from homeassistant.components import media_source
|
2021-05-24 19:09:57 +00:00
|
|
|
from homeassistant.components.media_player import (
|
2024-02-16 14:20:09 +00:00
|
|
|
BrowseMedia,
|
2021-05-24 19:09:57 +00:00
|
|
|
MediaPlayerEntity,
|
2022-04-05 22:00:37 +00:00
|
|
|
MediaPlayerEntityFeature,
|
2022-09-08 09:03:10 +00:00
|
|
|
MediaPlayerState,
|
2024-02-16 14:20:09 +00:00
|
|
|
MediaType,
|
|
|
|
async_process_play_media_url,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-05-12 07:39:49 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-05-24 19:09:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2021-05-24 19:09:57 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2020-06-03 12:01:56 +00:00
|
|
|
from .const import ATTR_MANUFACTURER, DEFAULT_NAME, DOMAIN
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
CONF_SOURCES: Final = "sources"
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2022-11-17 12:58:34 +00:00
|
|
|
DUNEHD_PLAYER_SUPPORT: Final[MediaPlayerEntityFeature] = (
|
2022-04-05 22:00:37 +00:00
|
|
|
MediaPlayerEntityFeature.PAUSE
|
|
|
|
| MediaPlayerEntityFeature.TURN_ON
|
|
|
|
| MediaPlayerEntityFeature.TURN_OFF
|
|
|
|
| MediaPlayerEntityFeature.PREVIOUS_TRACK
|
|
|
|
| MediaPlayerEntityFeature.NEXT_TRACK
|
|
|
|
| MediaPlayerEntityFeature.PLAY
|
2024-02-16 14:20:09 +00:00
|
|
|
| MediaPlayerEntityFeature.PLAY_MEDIA
|
|
|
|
| MediaPlayerEntityFeature.BROWSE_MEDIA
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-06-03 12:01:56 +00:00
|
|
|
"""Add Dune HD entities from a config_entry."""
|
2021-05-24 19:09:57 +00:00
|
|
|
unique_id = entry.entry_id
|
2020-06-03 12:01:56 +00:00
|
|
|
|
2023-07-16 20:37:12 +00:00
|
|
|
player: DuneHDPlayer = hass.data[DOMAIN][entry.entry_id]
|
2020-06-03 12:01:56 +00:00
|
|
|
|
|
|
|
async_add_entities([DuneHDPlayerEntity(player, DEFAULT_NAME, unique_id)], True)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
class DuneHDPlayerEntity(MediaPlayerEntity):
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Implementation of the Dune HD player."""
|
|
|
|
|
2023-07-16 20:37:12 +00:00
|
|
|
_attr_has_entity_name = True
|
|
|
|
_attr_name = None
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def __init__(self, player: DuneHDPlayer, name: str, unique_id: str) -> None:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize entity to control Dune HD."""
|
2016-11-28 07:42:57 +00:00
|
|
|
self._player = player
|
2021-05-24 19:09:57 +00:00
|
|
|
self._media_title: str | None = None
|
|
|
|
self._state: dict[str, Any] = {}
|
2023-08-29 11:08:24 +00:00
|
|
|
self._attr_unique_id = unique_id
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, unique_id)},
|
|
|
|
manufacturer=ATTR_MANUFACTURER,
|
|
|
|
name=name,
|
|
|
|
)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2022-02-24 17:25:38 +00:00
|
|
|
def update(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Update internal status of the entity."""
|
|
|
|
self._state = self._player.update_state()
|
|
|
|
self.__update_title()
|
|
|
|
|
|
|
|
@property
|
2022-09-08 09:03:10 +00:00
|
|
|
def state(self) -> MediaPlayerState:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Return player state."""
|
2022-09-08 09:03:10 +00:00
|
|
|
state = MediaPlayerState.OFF
|
2019-07-31 19:25:30 +00:00
|
|
|
if "playback_position" in self._state:
|
2022-09-08 09:03:10 +00:00
|
|
|
state = MediaPlayerState.PLAYING
|
2020-06-03 12:01:56 +00:00
|
|
|
if self._state.get("player_state") in ("playing", "buffering", "photo_viewer"):
|
2022-09-08 09:03:10 +00:00
|
|
|
state = MediaPlayerState.PLAYING
|
2019-07-31 19:25:30 +00:00
|
|
|
if int(self._state.get("playback_speed", 1234)) == 0:
|
2022-09-08 09:03:10 +00:00
|
|
|
state = MediaPlayerState.PAUSED
|
2020-05-28 10:27:15 +00:00
|
|
|
if self._state.get("player_state") == "navigator":
|
2022-09-08 09:03:10 +00:00
|
|
|
state = MediaPlayerState.ON
|
2016-11-28 07:42:57 +00:00
|
|
|
return state
|
|
|
|
|
2020-05-28 10:27:15 +00:00
|
|
|
@property
|
2021-05-24 19:09:57 +00:00
|
|
|
def available(self) -> bool:
|
2020-05-28 10:27:15 +00:00
|
|
|
"""Return True if entity is available."""
|
2021-05-24 19:09:57 +00:00
|
|
|
return len(self._state) > 0
|
2020-05-28 10:27:15 +00:00
|
|
|
|
2016-11-28 07:42:57 +00:00
|
|
|
@property
|
2021-05-24 19:09:57 +00:00
|
|
|
def volume_level(self) -> float:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the volume level of the media player (0..1)."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return int(self._state.get("playback_volume", 0)) / 100
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-24 19:09:57 +00:00
|
|
|
def is_volume_muted(self) -> bool:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return a boolean if volume is currently muted."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return int(self._state.get("playback_mute", 0)) == 1
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
@property
|
2022-11-17 12:58:34 +00:00
|
|
|
def supported_features(self) -> MediaPlayerEntityFeature:
|
2017-02-08 04:42:45 +00:00
|
|
|
"""Flag media player features that are supported."""
|
2016-11-28 07:42:57 +00:00
|
|
|
return DUNEHD_PLAYER_SUPPORT
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def volume_up(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Volume up media player."""
|
|
|
|
self._state = self._player.volume_up()
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def volume_down(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Volume down media player."""
|
|
|
|
self._state = self._player.volume_down()
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def mute_volume(self, mute: bool) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Mute/unmute player volume."""
|
|
|
|
self._state = self._player.mute(mute)
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def turn_off(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Turn off media player."""
|
|
|
|
self._media_title = None
|
|
|
|
self._state = self._player.turn_off()
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def turn_on(self) -> None:
|
2024-02-16 14:20:09 +00:00
|
|
|
"""Turn on media player."""
|
2016-11-28 07:42:57 +00:00
|
|
|
self._state = self._player.turn_on()
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def media_play(self) -> None:
|
2017-10-05 19:55:09 +00:00
|
|
|
"""Play media player."""
|
2016-11-28 07:42:57 +00:00
|
|
|
self._state = self._player.play()
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def media_pause(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Pause media player."""
|
|
|
|
self._state = self._player.pause()
|
|
|
|
|
2024-02-16 14:20:09 +00:00
|
|
|
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)
|
|
|
|
|
2016-11-28 07:42:57 +00:00
|
|
|
@property
|
2021-05-24 19:09:57 +00:00
|
|
|
def media_title(self) -> str | None:
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the current media source."""
|
2016-11-28 07:42:57 +00:00
|
|
|
self.__update_title()
|
|
|
|
if self._media_title:
|
|
|
|
return self._media_title
|
2021-05-24 19:09:57 +00:00
|
|
|
return None
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def __update_title(self) -> None:
|
2020-05-28 10:27:15 +00:00
|
|
|
if self._state.get("player_state") == "bluray_playback":
|
2019-07-31 19:25:30 +00:00
|
|
|
self._media_title = "Blu-Ray"
|
2020-06-03 12:01:56 +00:00
|
|
|
elif self._state.get("player_state") == "photo_viewer":
|
|
|
|
self._media_title = "Photo Viewer"
|
|
|
|
elif self._state.get("playback_url"):
|
|
|
|
self._media_title = self._state["playback_url"].split("/")[-1]
|
|
|
|
else:
|
|
|
|
self._media_title = None
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def media_previous_track(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Send previous track command."""
|
|
|
|
self._state = self._player.previous_track()
|
|
|
|
|
2021-05-24 19:09:57 +00:00
|
|
|
def media_next_track(self) -> None:
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Send next track command."""
|
|
|
|
self._state = self._player.next_track()
|