Home Assistant Unofficial Reference 2024.12.1
media_player.py
Go to the documentation of this file.
1 """Provide functionality to interact with vlc devices on the network."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import vlc
9 import voluptuous as vol
10 
11 from homeassistant.components import media_source
13  PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA,
14  BrowseMedia,
15  MediaPlayerEntity,
16  MediaPlayerEntityFeature,
17  MediaPlayerState,
18  MediaType,
19  async_process_play_media_url,
20 )
21 from homeassistant.const import CONF_NAME
22 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.entity_platform import AddEntitiesCallback
25 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
26 import homeassistant.util.dt as dt_util
27 
28 _LOGGER = logging.getLogger(__name__)
29 
30 CONF_ARGUMENTS = "arguments"
31 DEFAULT_NAME = "Vlc"
32 
33 PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
34  {
35  vol.Optional(CONF_ARGUMENTS, default=""): cv.string,
36  vol.Optional(CONF_NAME): cv.string,
37  }
38 )
39 
40 
42  hass: HomeAssistant,
43  config: ConfigType,
44  add_entities: AddEntitiesCallback,
45  discovery_info: DiscoveryInfoType | None = None,
46 ) -> None:
47  """Set up the vlc platform."""
49  [VlcDevice(config.get(CONF_NAME, DEFAULT_NAME), config.get(CONF_ARGUMENTS))]
50  )
51 
52 
54  """Representation of a vlc player."""
55 
56  _attr_media_content_type = MediaType.MUSIC
57  _attr_supported_features = (
58  MediaPlayerEntityFeature.PAUSE
59  | MediaPlayerEntityFeature.VOLUME_SET
60  | MediaPlayerEntityFeature.VOLUME_MUTE
61  | MediaPlayerEntityFeature.PLAY_MEDIA
62  | MediaPlayerEntityFeature.PLAY
63  | MediaPlayerEntityFeature.STOP
64  | MediaPlayerEntityFeature.BROWSE_MEDIA
65  )
66 
67  def __init__(self, name, arguments):
68  """Initialize the vlc device."""
69  self._instance_instance = vlc.Instance(arguments)
70  self._vlc_vlc = self._instance_instance.media_player_new()
71  self._attr_name_attr_name = name
72 
73  def update(self):
74  """Get the latest details from the device."""
75  status = self._vlc_vlc.get_state()
76  if status == vlc.State.Playing:
77  self._attr_state_attr_state = MediaPlayerState.PLAYING
78  elif status == vlc.State.Paused:
79  self._attr_state_attr_state = MediaPlayerState.PAUSED
80  else:
81  self._attr_state_attr_state = MediaPlayerState.IDLE
82  self._attr_media_duration_attr_media_duration = self._vlc_vlc.get_length() / 1000
83  position = self._vlc_vlc.get_position() * self._attr_media_duration_attr_media_duration
84  if position != self._attr_media_position_attr_media_position:
85  self._attr_media_position_updated_at_attr_media_position_updated_at = dt_util.utcnow()
86  self._attr_media_position_attr_media_position = position
87 
88  self._attr_volume_level_attr_volume_level = self._vlc_vlc.audio_get_volume() / 100
89  self._attr_is_volume_muted_attr_is_volume_muted = self._vlc_vlc.audio_get_mute() == 1
90 
91  return True
92 
93  def media_seek(self, position: float) -> None:
94  """Seek the media to a specific location."""
95  track_length = self._vlc_vlc.get_length() / 1000
96  self._vlc_vlc.set_position(position / track_length)
97 
98  def mute_volume(self, mute: bool) -> None:
99  """Mute the volume."""
100  self._vlc_vlc.audio_set_mute(mute)
101  self._attr_is_volume_muted_attr_is_volume_muted = mute
102 
103  def set_volume_level(self, volume: float) -> None:
104  """Set volume level, range 0..1."""
105  self._vlc_vlc.audio_set_volume(int(volume * 100))
106  self._attr_volume_level_attr_volume_level = volume
107 
108  def media_play(self) -> None:
109  """Send play command."""
110  self._vlc_vlc.play()
111  self._attr_state_attr_state = MediaPlayerState.PLAYING
112 
113  def media_pause(self) -> None:
114  """Send pause command."""
115  self._vlc_vlc.pause()
116  self._attr_state_attr_state = MediaPlayerState.PAUSED
117 
118  def media_stop(self) -> None:
119  """Send stop command."""
120  self._vlc_vlc.stop()
121  self._attr_state_attr_state = MediaPlayerState.IDLE
122 
123  async def async_play_media(
124  self, media_type: MediaType | str, media_id: str, **kwargs: Any
125  ) -> None:
126  """Play media from a URL or file."""
127  # Handle media_source
128  if media_source.is_media_source_id(media_id):
129  sourced_media = await media_source.async_resolve_media(
130  self.hasshass, media_id, self.entity_identity_id
131  )
132  media_id = sourced_media.url
133 
134  elif media_type != MediaType.MUSIC:
135  _LOGGER.error(
136  "Invalid media type %s. Only %s is supported",
137  media_type,
138  MediaType.MUSIC,
139  )
140  return
141 
142  media_id = async_process_play_media_url(self.hasshass, media_id)
143 
144  def play():
145  self._vlc_vlc.set_media(self._instance_instance.media_new(media_id))
146  self._vlc_vlc.play()
147 
148  await self.hasshass.async_add_executor_job(play)
149  self._attr_state_attr_state = MediaPlayerState.PLAYING
150 
152  self,
153  media_content_type: MediaType | str | None = None,
154  media_content_id: str | None = None,
155  ) -> BrowseMedia:
156  """Implement the websocket media browsing helper."""
157  return await media_source.async_browse_media(
158  self.hasshass,
159  media_content_id,
160  content_filter=lambda item: item.media_content_type.startswith("audio/"),
161  )
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)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
str|float get_state(dict[str, float] data, str key)
Definition: sensor.py:26
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 setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: media_player.py:46