Home Assistant Unofficial Reference 2024.12.1
media_player.py
Go to the documentation of this file.
1 """Provide functionality to interact with the vlc telnet interface."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable, Coroutine
6 from functools import wraps
7 from typing import Any, Concatenate, Literal
8 
9 from aiovlc.client import Client
10 from aiovlc.exceptions import AuthError, CommandError, ConnectError
11 
12 from homeassistant.components import media_source
14  BrowseMedia,
15  MediaPlayerEntity,
16  MediaPlayerEntityFeature,
17  MediaPlayerState,
18  MediaType,
19  async_process_play_media_url,
20 )
21 from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry
22 from homeassistant.const import CONF_NAME
23 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 import homeassistant.util.dt as dt_util
27 
28 from . import VlcConfigEntry
29 from .const import DEFAULT_NAME, DOMAIN, LOGGER
30 
31 MAX_VOLUME = 500
32 
33 
34 def _get_str(data: dict, key: str) -> str | None:
35  """Get a value from a dictionary and cast it to a string or None."""
36  if value := data.get(key):
37  return str(value)
38  return None
39 
40 
42  hass: HomeAssistant, entry: VlcConfigEntry, async_add_entities: AddEntitiesCallback
43 ) -> None:
44  """Set up the vlc platform."""
45  # CONF_NAME is only present in imported YAML.
46  name = entry.data.get(CONF_NAME) or DEFAULT_NAME
47  vlc = entry.runtime_data.vlc
48  available = entry.runtime_data.available
49 
50  async_add_entities([VlcDevice(entry, vlc, name, available)], True)
51 
52 
53 def catch_vlc_errors[_VlcDeviceT: VlcDevice, **_P](
54  func: Callable[Concatenate[_VlcDeviceT, _P], Awaitable[None]],
55 ) -> Callable[Concatenate[_VlcDeviceT, _P], Coroutine[Any, Any, None]]:
56  """Catch VLC errors."""
57 
58  @wraps(func)
59  async def wrapper(self: _VlcDeviceT, *args: _P.args, **kwargs: _P.kwargs) -> None:
60  """Catch VLC errors and modify availability."""
61  try:
62  await func(self, *args, **kwargs)
63  except CommandError as err:
64  LOGGER.error("Command error: %s", err)
65  except ConnectError as err:
66  if self._attr_available:
67  LOGGER.error("Connection error: %s", err)
68  self._attr_available = False
69 
70  return wrapper
71 
72 
74  """Representation of a vlc player."""
75 
76  _attr_has_entity_name = True
77  _attr_name = None
78  _attr_media_content_type = MediaType.MUSIC
79  _attr_supported_features = (
80  MediaPlayerEntityFeature.CLEAR_PLAYLIST
81  | MediaPlayerEntityFeature.NEXT_TRACK
82  | MediaPlayerEntityFeature.PAUSE
83  | MediaPlayerEntityFeature.PLAY
84  | MediaPlayerEntityFeature.PLAY_MEDIA
85  | MediaPlayerEntityFeature.PREVIOUS_TRACK
86  | MediaPlayerEntityFeature.SEEK
87  | MediaPlayerEntityFeature.SHUFFLE_SET
88  | MediaPlayerEntityFeature.STOP
89  | MediaPlayerEntityFeature.VOLUME_MUTE
90  | MediaPlayerEntityFeature.VOLUME_SET
91  | MediaPlayerEntityFeature.BROWSE_MEDIA
92  )
93  _volume_bkp = 0.0
94  volume_level: int
95 
96  def __init__(
97  self, config_entry: ConfigEntry, vlc: Client, name: str, available: bool
98  ) -> None:
99  """Initialize the vlc device."""
100  self._config_entry_config_entry = config_entry
101  self._vlc_vlc = vlc
102  self._attr_available_attr_available = available
103  config_entry_id = config_entry.entry_id
104  self._attr_unique_id_attr_unique_id = config_entry_id
105  self._attr_device_info_attr_device_info = DeviceInfo(
106  entry_type=DeviceEntryType.SERVICE,
107  identifiers={(DOMAIN, config_entry_id)},
108  manufacturer="VideoLAN",
109  name=name,
110  )
111  self._using_addon_using_addon = config_entry.source == SOURCE_HASSIO
112 
113  @catch_vlc_errors
114  async def async_update(self) -> None:
115  """Get the latest details from the device."""
116  if not self.availableavailable:
117  try:
118  await self._vlc_vlc.connect()
119  except ConnectError as err:
120  LOGGER.debug("Connection error: %s", err)
121  return
122 
123  try:
124  await self._vlc_vlc.login()
125  except AuthError:
126  LOGGER.debug("Failed to login to VLC")
127  self.hasshass.async_create_task(
128  self.hasshass.config_entries.async_reload(self._config_entry_config_entry.entry_id)
129  )
130  return
131 
132  self._attr_state_attr_state = MediaPlayerState.IDLE
133  self._attr_available_attr_available = True
134  LOGGER.debug("Connected to vlc host: %s", self._vlc_vlc.host)
135 
136  status = await self._vlc_vlc.status()
137  LOGGER.debug("Status: %s", status)
138 
139  self._attr_volume_level_attr_volume_level = status.audio_volume / MAX_VOLUME
140  state = status.state
141  if state == "playing":
142  self._attr_state_attr_state = MediaPlayerState.PLAYING
143  elif state == "paused":
144  self._attr_state_attr_state = MediaPlayerState.PAUSED
145  else:
146  self._attr_state_attr_state = MediaPlayerState.IDLE
147 
148  if self._attr_state_attr_state != MediaPlayerState.IDLE:
149  self._attr_media_duration_attr_media_duration = (await self._vlc_vlc.get_length()).length
150  time_output = await self._vlc_vlc.get_time()
151  vlc_position = time_output.time
152 
153  # Check if current position is stale.
154  if vlc_position != self.media_positionmedia_position:
155  self._attr_media_position_updated_at_attr_media_position_updated_at = dt_util.utcnow()
156  self._attr_media_position_attr_media_position = vlc_position
157 
158  info = await self._vlc_vlc.info()
159  data = info.data
160  LOGGER.debug("Info data: %s", data)
161 
162  self._attr_media_album_name_attr_media_album_name = _get_str(data.get("data", {}), "album")
163  self._attr_media_artist_attr_media_artist = _get_str(data.get("data", {}), "artist")
164  self._attr_media_title_attr_media_title = _get_str(data.get("data", {}), "title")
165  now_playing = _get_str(data.get("data", {}), "now_playing")
166 
167  # Many radio streams put artist/title/album in now_playing and title is the station name.
168  if now_playing:
169  if not self.media_artistmedia_artist:
170  self._attr_media_artist_attr_media_artist = self._attr_media_title_attr_media_title
171  self._attr_media_title_attr_media_title = now_playing
172 
173  if self.media_titlemedia_title:
174  return
175 
176  # Fall back to filename.
177  if data_info := data.get("data"):
178  media_title = _get_str(data_info, "filename")
179 
180  # Strip out auth signatures if streaming local media
181  if media_title and (pos := media_title.find("?authSig=")) != -1:
182  self._attr_media_title_attr_media_title = media_title[:pos]
183  else:
184  self._attr_media_title_attr_media_title = media_title
185 
186  @catch_vlc_errors
187  async def async_media_seek(self, position: float) -> None:
188  """Seek the media to a specific location."""
189  await self._vlc_vlc.seek(round(position))
190 
191  @catch_vlc_errors
192  async def async_mute_volume(self, mute: bool) -> None:
193  """Mute the volume."""
194  assert self._attr_volume_level_attr_volume_level is not None
195  if mute:
196  self._volume_bkp_volume_bkp_volume_bkp = self._attr_volume_level_attr_volume_level
197  await self.async_set_volume_levelasync_set_volume_levelasync_set_volume_level(0)
198  else:
199  await self.async_set_volume_levelasync_set_volume_levelasync_set_volume_level(self._volume_bkp_volume_bkp_volume_bkp)
200 
201  self._attr_is_volume_muted_attr_is_volume_muted = mute
202 
203  @catch_vlc_errors
204  async def async_set_volume_level(self, volume: float) -> None:
205  """Set volume level, range 0..1."""
206  await self._vlc_vlc.set_volume(round(volume * MAX_VOLUME))
207  self._attr_volume_level_attr_volume_level = volume
208 
209  if self.is_volume_mutedis_volume_muted and self.volume_levelvolume_level > 0:
210  # This can happen if we were muted and then see a volume_up.
211  self._attr_is_volume_muted_attr_is_volume_muted = False
212 
213  @catch_vlc_errors
214  async def async_media_play(self) -> None:
215  """Send play command."""
216  status = await self._vlc_vlc.status()
217  if status.state == "paused":
218  # If already paused, play by toggling pause.
219  await self._vlc_vlc.pause()
220  else:
221  await self._vlc_vlc.play()
222  self._attr_state_attr_state = MediaPlayerState.PLAYING
223 
224  @catch_vlc_errors
225  async def async_media_pause(self) -> None:
226  """Send pause command."""
227  status = await self._vlc_vlc.status()
228  if status.state != "paused":
229  # Make sure we're not already paused as pausing again will unpause.
230  await self._vlc_vlc.pause()
231 
232  self._attr_state_attr_state = MediaPlayerState.PAUSED
233 
234  @catch_vlc_errors
235  async def async_media_stop(self) -> None:
236  """Send stop command."""
237  await self._vlc_vlc.stop()
238  self._attr_state_attr_state = MediaPlayerState.IDLE
239 
240  @catch_vlc_errors
241  async def async_play_media(
242  self, media_type: MediaType | str, media_id: str, **kwargs: Any
243  ) -> None:
244  """Play media from a URL or file."""
245  # Handle media_source
246  if media_source.is_media_source_id(media_id):
247  sourced_media = await media_source.async_resolve_media(
248  self.hasshass, media_id, self.entity_identity_id
249  )
250  media_id = sourced_media.url
251 
252  # If media ID is a relative URL, we serve it from HA.
253  media_id = async_process_play_media_url(
254  self.hasshass, media_id, for_supervisor_network=self._using_addon_using_addon
255  )
256 
257  await self._vlc_vlc.add(media_id)
258  self._attr_state_attr_state = MediaPlayerState.PLAYING
259 
260  @catch_vlc_errors
261  async def async_media_previous_track(self) -> None:
262  """Send previous track command."""
263  await self._vlc_vlc.prev()
264 
265  @catch_vlc_errors
266  async def async_media_next_track(self) -> None:
267  """Send next track command."""
268  await self._vlc_vlc.next()
269 
270  @catch_vlc_errors
271  async def async_clear_playlist(self) -> None:
272  """Clear players playlist."""
273  await self._vlc_vlc.clear()
274 
275  @catch_vlc_errors
276  async def async_set_shuffle(self, shuffle: bool) -> None:
277  """Enable/disable shuffle mode."""
278  shuffle_command: Literal["on", "off"] = "on" if shuffle else "off"
279  await self._vlc_vlc.random(shuffle_command)
280 
282  self,
283  media_content_type: MediaType | str | None = None,
284  media_content_id: str | None = None,
285  ) -> BrowseMedia:
286  """Implement the websocket media browsing helper."""
287  return await media_source.async_browse_media(self.hasshass, media_content_id)
None __init__(self, ConfigEntry config_entry, Client vlc, str name, bool available)
Definition: media_player.py:98
None async_play_media(self, MediaType|str media_type, str media_id, **Any kwargs)
BrowseMedia async_browse_media(self, MediaType|str|None media_content_type=None, str|None media_content_id=None)
bool add(self, _T matcher)
Definition: match.py:185
str async_process_play_media_url(HomeAssistant hass, str media_content_id, *bool allow_relative_url=False, bool for_supervisor_network=False)
Definition: browse_media.py:36
None async_setup_entry(HomeAssistant hass, VlcConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: media_player.py:43