2019-04-03 15:40:03 +00:00
|
|
|
"""Provide functionality to interact with vlc devices on the network."""
|
2016-12-11 22:59:12 +00:00
|
|
|
import logging
|
|
|
|
|
2019-10-10 07:19:46 +00:00
|
|
|
import vlc
|
2019-12-09 13:42:53 +00:00
|
|
|
import voluptuous as vol
|
2016-12-11 22:59:12 +00:00
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
SUPPORT_PAUSE,
|
|
|
|
SUPPORT_PLAY,
|
|
|
|
SUPPORT_PLAY_MEDIA,
|
|
|
|
SUPPORT_STOP,
|
|
|
|
SUPPORT_VOLUME_MUTE,
|
|
|
|
SUPPORT_VOLUME_SET,
|
|
|
|
)
|
|
|
|
from homeassistant.const import CONF_NAME, STATE_IDLE, STATE_PAUSED, STATE_PLAYING
|
2016-12-11 22:59:12 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ARGUMENTS = "arguments"
|
|
|
|
DEFAULT_NAME = "Vlc"
|
2016-12-11 22:59:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_VLC = (
|
|
|
|
SUPPORT_PAUSE
|
|
|
|
| SUPPORT_VOLUME_SET
|
|
|
|
| SUPPORT_VOLUME_MUTE
|
|
|
|
| SUPPORT_PLAY_MEDIA
|
|
|
|
| SUPPORT_PLAY
|
|
|
|
| SUPPORT_STOP
|
|
|
|
)
|
2016-12-11 22:59:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ARGUMENTS, default=""): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-12-11 22:59:12 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the vlc platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities(
|
|
|
|
[VlcDevice(config.get(CONF_NAME, DEFAULT_NAME), config.get(CONF_ARGUMENTS))]
|
|
|
|
)
|
2016-12-11 22:59:12 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
class VlcDevice(MediaPlayerEntity):
|
2016-12-11 22:59:12 +00:00
|
|
|
"""Representation of a vlc player."""
|
|
|
|
|
2017-01-14 21:30:24 +00:00
|
|
|
def __init__(self, name, arguments):
|
2016-12-11 22:59:12 +00:00
|
|
|
"""Initialize the vlc device."""
|
2017-01-14 21:30:24 +00:00
|
|
|
self._instance = vlc.Instance(arguments)
|
2016-12-11 22:59:12 +00:00
|
|
|
self._vlc = self._instance.media_player_new()
|
|
|
|
self._name = name
|
|
|
|
self._volume = None
|
|
|
|
self._muted = None
|
|
|
|
self._state = None
|
|
|
|
self._media_position_updated_at = None
|
|
|
|
self._media_position = None
|
|
|
|
self._media_duration = None
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest details from the device."""
|
|
|
|
status = self._vlc.get_state()
|
|
|
|
if status == vlc.State.Playing:
|
|
|
|
self._state = STATE_PLAYING
|
|
|
|
elif status == vlc.State.Paused:
|
|
|
|
self._state = STATE_PAUSED
|
|
|
|
else:
|
|
|
|
self._state = STATE_IDLE
|
2019-07-31 19:25:30 +00:00
|
|
|
self._media_duration = self._vlc.get_length() / 1000
|
2017-01-03 20:13:02 +00:00
|
|
|
position = self._vlc.get_position() * self._media_duration
|
|
|
|
if position != self._media_position:
|
|
|
|
self._media_position_updated_at = dt_util.utcnow()
|
|
|
|
self._media_position = position
|
2016-12-11 22:59:12 +00:00
|
|
|
|
|
|
|
self._volume = self._vlc.audio_get_volume() / 100
|
2019-07-31 19:25:30 +00:00
|
|
|
self._muted = self._vlc.audio_get_mute() == 1
|
2016-12-11 22:59:12 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Volume level of the media player (0..1)."""
|
|
|
|
return self._volume
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Boolean if volume is currently muted."""
|
|
|
|
return self._muted
|
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2016-12-11 22:59:12 +00:00
|
|
|
return SUPPORT_VLC
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_content_type(self):
|
|
|
|
"""Content type of current playing media."""
|
|
|
|
return MEDIA_TYPE_MUSIC
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_duration(self):
|
|
|
|
"""Duration of current playing media in seconds."""
|
|
|
|
return self._media_duration
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_position(self):
|
|
|
|
"""Position of current playing media in seconds."""
|
|
|
|
return self._media_position
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_position_updated_at(self):
|
|
|
|
"""When was the position of the current playing media valid."""
|
|
|
|
return self._media_position_updated_at
|
|
|
|
|
|
|
|
def media_seek(self, position):
|
|
|
|
"""Seek the media to a specific location."""
|
2019-07-31 19:25:30 +00:00
|
|
|
track_length = self._vlc.get_length() / 1000
|
|
|
|
self._vlc.set_position(position / track_length)
|
2016-12-11 22:59:12 +00:00
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Mute the volume."""
|
|
|
|
self._vlc.audio_set_mute(mute)
|
|
|
|
self._muted = mute
|
|
|
|
|
|
|
|
def set_volume_level(self, volume):
|
|
|
|
"""Set volume level, range 0..1."""
|
|
|
|
self._vlc.audio_set_volume(int(volume * 100))
|
|
|
|
self._volume = volume
|
|
|
|
|
|
|
|
def media_play(self):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Send play command."""
|
2016-12-11 22:59:12 +00:00
|
|
|
self._vlc.play()
|
|
|
|
self._state = STATE_PLAYING
|
|
|
|
|
|
|
|
def media_pause(self):
|
|
|
|
"""Send pause command."""
|
|
|
|
self._vlc.pause()
|
|
|
|
self._state = STATE_PAUSED
|
|
|
|
|
2018-02-01 08:49:39 +00:00
|
|
|
def media_stop(self):
|
|
|
|
"""Send stop command."""
|
|
|
|
self._vlc.stop()
|
|
|
|
self._state = STATE_IDLE
|
|
|
|
|
2016-12-11 22:59:12 +00:00
|
|
|
def play_media(self, media_type, media_id, **kwargs):
|
|
|
|
"""Play media from a URL or file."""
|
2021-03-20 00:27:04 +00:00
|
|
|
if media_type != MEDIA_TYPE_MUSIC:
|
2016-12-11 22:59:12 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Invalid media type %s. Only %s is supported",
|
2019-07-31 19:25:30 +00:00
|
|
|
media_type,
|
|
|
|
MEDIA_TYPE_MUSIC,
|
|
|
|
)
|
2016-12-11 22:59:12 +00:00
|
|
|
return
|
|
|
|
self._vlc.set_media(self._instance.media_new(media_id))
|
|
|
|
self._vlc.play()
|
|
|
|
self._state = STATE_PLAYING
|