2019-04-03 15:40:03 +00:00
|
|
|
"""DuneHD implementation of the media player."""
|
2016-12-03 19:46:04 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-11-28 07:42:57 +00:00
|
|
|
from homeassistant.components.media_player import (
|
2019-02-08 22:18:18 +00:00
|
|
|
MediaPlayerDevice, PLATFORM_SCHEMA)
|
|
|
|
from homeassistant.components.media_player.const import (
|
|
|
|
SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PLAY,
|
2018-09-09 12:26:06 +00:00
|
|
|
SUPPORT_PREVIOUS_TRACK, SUPPORT_SELECT_SOURCE, SUPPORT_TURN_OFF,
|
2019-02-08 22:18:18 +00:00
|
|
|
SUPPORT_TURN_ON)
|
2016-11-28 07:42:57 +00:00
|
|
|
from homeassistant.const import (
|
2018-09-09 12:26:06 +00:00
|
|
|
CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2016-12-03 19:46:04 +00:00
|
|
|
DEFAULT_NAME = 'DuneHD'
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2016-12-03 19:46:04 +00:00
|
|
|
CONF_SOURCES = 'sources'
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
2017-04-30 17:55:03 +00:00
|
|
|
vol.Optional(CONF_SOURCES): vol.Schema({cv.string: cv.string}),
|
2016-11-28 07:42:57 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
DUNEHD_PLAYER_SUPPORT = \
|
|
|
|
SUPPORT_PAUSE | SUPPORT_TURN_ON | SUPPORT_TURN_OFF | \
|
2017-01-09 00:09:30 +00:00
|
|
|
SUPPORT_SELECT_SOURCE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
|
|
|
|
SUPPORT_PLAY
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Set up the DuneHD media player platform."""
|
2017-08-08 18:21:33 +00:00
|
|
|
from pdunehd import DuneHDPlayer
|
|
|
|
|
2016-11-28 07:42:57 +00:00
|
|
|
sources = config.get(CONF_SOURCES, {})
|
2017-08-08 18:21:33 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
name = config.get(CONF_NAME)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([DuneHDPlayerEntity(DuneHDPlayer(host), name, sources)], True)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DuneHDPlayerEntity(MediaPlayerDevice):
|
|
|
|
"""Implementation of the Dune HD player."""
|
|
|
|
|
|
|
|
def __init__(self, player, name, sources):
|
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
|
|
|
|
self._name = name
|
|
|
|
self._sources = sources
|
|
|
|
self._media_title = None
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update internal status of the entity."""
|
|
|
|
self._state = self._player.update_state()
|
|
|
|
self.__update_title()
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return player state."""
|
|
|
|
state = STATE_OFF
|
|
|
|
if 'playback_position' in self._state:
|
|
|
|
state = STATE_PLAYING
|
|
|
|
if self._state['player_state'] in ('playing', 'buffering'):
|
|
|
|
state = STATE_PLAYING
|
|
|
|
if int(self._state.get('playback_speed', 1234)) == 0:
|
|
|
|
state = STATE_PAUSED
|
|
|
|
if self._state['player_state'] == 'navigator':
|
|
|
|
state = STATE_ON
|
|
|
|
return state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the volume level of the media player (0..1)."""
|
2016-11-28 07:42:57 +00:00
|
|
|
return int(self._state.get('playback_volume', 0)) / 100
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return a boolean if volume is currently muted."""
|
2016-11-28 07:42:57 +00:00
|
|
|
return int(self._state.get('playback_mute', 0)) == 1
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return a list of available input sources."""
|
2016-11-28 07:42:57 +00:00
|
|
|
return list(self._sources.keys())
|
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2016-11-28 07:42:57 +00:00
|
|
|
return DUNEHD_PLAYER_SUPPORT
|
|
|
|
|
|
|
|
def volume_up(self):
|
|
|
|
"""Volume up media player."""
|
|
|
|
self._state = self._player.volume_up()
|
|
|
|
|
|
|
|
def volume_down(self):
|
|
|
|
"""Volume down media player."""
|
|
|
|
self._state = self._player.volume_down()
|
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Mute/unmute player volume."""
|
|
|
|
self._state = self._player.mute(mute)
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn off media player."""
|
|
|
|
self._media_title = None
|
|
|
|
self._state = self._player.turn_off()
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Turn off media player."""
|
|
|
|
self._state = self._player.turn_on()
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def media_play(self):
|
2017-10-05 19:55:09 +00:00
|
|
|
"""Play media player."""
|
2016-11-28 07:42:57 +00:00
|
|
|
self._state = self._player.play()
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def media_pause(self):
|
|
|
|
"""Pause media player."""
|
|
|
|
self._state = self._player.pause()
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media_title(self):
|
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
|
|
|
|
return self._state.get('playback_url', 'Not playing')
|
|
|
|
|
|
|
|
def __update_title(self):
|
|
|
|
if self._state['player_state'] == 'bluray_playback':
|
|
|
|
self._media_title = 'Blu-Ray'
|
|
|
|
elif 'playback_url' in self._state:
|
|
|
|
sources = self._sources
|
|
|
|
sval = sources.values()
|
|
|
|
skey = sources.keys()
|
|
|
|
pburl = self._state['playback_url']
|
|
|
|
if pburl in sval:
|
|
|
|
self._media_title = list(skey)[list(sval).index(pburl)]
|
|
|
|
else:
|
|
|
|
self._media_title = pburl
|
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Select input source."""
|
|
|
|
self._media_title = source
|
|
|
|
self._state = self._player.launch_media_url(self._sources.get(source))
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def media_previous_track(self):
|
|
|
|
"""Send previous track command."""
|
|
|
|
self._state = self._player.previous_track()
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def media_next_track(self):
|
|
|
|
"""Send next track command."""
|
|
|
|
self._state = self._player.next_track()
|
|
|
|
self.schedule_update_ha_state()
|