Home Assistant Unofficial Reference 2024.12.1
media_player.py
Go to the documentation of this file.
1 """Kaleidescape Media Player."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import TYPE_CHECKING
7 
8 from kaleidescape import const as kaleidescape_const
9 
11  MediaPlayerEntity,
12  MediaPlayerEntityFeature,
13  MediaPlayerState,
14 )
15 from homeassistant.util.dt import utcnow
16 
17 from .const import DOMAIN as KALEIDESCAPE_DOMAIN
18 from .entity import KaleidescapeEntity
19 
20 if TYPE_CHECKING:
21  from datetime import datetime
22 
23  from homeassistant.config_entries import ConfigEntry
24  from homeassistant.core import HomeAssistant
25  from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 
27 
28 KALEIDESCAPE_PLAYING_STATES = [
29  kaleidescape_const.PLAY_STATUS_PLAYING,
30  kaleidescape_const.PLAY_STATUS_FORWARD,
31  kaleidescape_const.PLAY_STATUS_REVERSE,
32 ]
33 
34 KALEIDESCAPE_PAUSED_STATES = [kaleidescape_const.PLAY_STATUS_PAUSED]
35 
36 
37 _LOGGER = logging.getLogger(__name__)
38 
39 
41  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
42 ) -> None:
43  """Set up the platform from a config entry."""
44  entities = [KaleidescapeMediaPlayer(hass.data[KALEIDESCAPE_DOMAIN][entry.entry_id])]
45  async_add_entities(entities)
46 
47 
49  """Representation of a Kaleidescape device."""
50 
51  _attr_supported_features = (
52  MediaPlayerEntityFeature.TURN_ON
53  | MediaPlayerEntityFeature.TURN_OFF
54  | MediaPlayerEntityFeature.PLAY
55  | MediaPlayerEntityFeature.PAUSE
56  | MediaPlayerEntityFeature.STOP
57  | MediaPlayerEntityFeature.NEXT_TRACK
58  | MediaPlayerEntityFeature.PREVIOUS_TRACK
59  )
60  _attr_name = None
61 
62  async def async_turn_on(self) -> None:
63  """Send leave standby command."""
64  await self._device_device.leave_standby()
65 
66  async def async_turn_off(self) -> None:
67  """Send enter standby command."""
68  await self._device_device.enter_standby()
69 
70  async def async_media_pause(self) -> None:
71  """Send pause command."""
72  await self._device_device.pause()
73 
74  async def async_media_play(self) -> None:
75  """Send play command."""
76  await self._device_device.play()
77 
78  async def async_media_stop(self) -> None:
79  """Send stop command."""
80  await self._device_device.stop()
81 
82  async def async_media_next_track(self) -> None:
83  """Send track next command."""
84  await self._device_device.next()
85 
86  async def async_media_previous_track(self) -> None:
87  """Send track previous command."""
88  await self._device_device.previous()
89 
90  @property
91  def state(self) -> MediaPlayerState:
92  """State of device."""
93  if self._device_device.power.state == kaleidescape_const.DEVICE_POWER_STATE_STANDBY:
94  return MediaPlayerState.OFF
95  if self._device_device.movie.play_status in KALEIDESCAPE_PLAYING_STATES:
96  return MediaPlayerState.PLAYING
97  if self._device_device.movie.play_status in KALEIDESCAPE_PAUSED_STATES:
98  return MediaPlayerState.PAUSED
99  return MediaPlayerState.IDLE
100 
101  @property
102  def available(self) -> bool:
103  """Return if device is available."""
104  return self._device_device.is_connected
105 
106  @property
107  def media_content_id(self) -> str | None:
108  """Content ID of current playing media."""
109  if self._device_device.movie.handle:
110  return self._device_device.movie.handle
111  return None
112 
113  @property
114  def media_content_type(self) -> str | None:
115  """Content type of current playing media."""
116  return self._device_device.movie.media_type
117 
118  @property
119  def media_duration(self) -> int | None:
120  """Duration of current playing media in seconds."""
121  if self._device_device.movie.title_length:
122  return self._device_device.movie.title_length
123  return None
124 
125  @property
126  def media_position(self) -> int | None:
127  """Position of current playing media in seconds."""
128  if self._device_device.movie.title_location:
129  return self._device_device.movie.title_location
130  return None
131 
132  @property
133  def media_position_updated_at(self) -> datetime | None:
134  """When was the position of the current playing media valid."""
135  if self._device_device.movie.play_status in KALEIDESCAPE_PLAYING_STATES:
136  return utcnow()
137  return None
138 
139  @property
140  def media_image_url(self) -> str:
141  """Image url of current playing media."""
142  return self._device_device.movie.cover
143 
144  @property
145  def media_title(self) -> str:
146  """Title of current playing media."""
147  return self._device_device.movie.title
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: media_player.py:42