2020-06-03 12:01:56 +00:00
|
|
|
"""Dune HD implementation of the media player."""
|
2016-12-03 19:46:04 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
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
|
|
|
SUPPORT_NEXT_TRACK,
|
|
|
|
SUPPORT_PAUSE,
|
|
|
|
SUPPORT_PLAY,
|
|
|
|
SUPPORT_PREVIOUS_TRACK,
|
|
|
|
SUPPORT_TURN_OFF,
|
|
|
|
SUPPORT_TURN_ON,
|
|
|
|
)
|
2020-06-03 12:01:56 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
2016-11-28 07:42:57 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_PAUSED,
|
|
|
|
STATE_PLAYING,
|
|
|
|
)
|
2018-09-09 12:26:06 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2020-06-03 12:01:56 +00:00
|
|
|
from .const import ATTR_MANUFACTURER, DEFAULT_NAME, DOMAIN
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SOURCES = "sources"
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_SOURCES): vol.Schema({cv.string: cv.string}),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DUNEHD_PLAYER_SUPPORT = (
|
|
|
|
SUPPORT_PAUSE
|
|
|
|
| SUPPORT_TURN_ON
|
|
|
|
| SUPPORT_TURN_OFF
|
|
|
|
| SUPPORT_PREVIOUS_TRACK
|
|
|
|
| SUPPORT_NEXT_TRACK
|
|
|
|
| SUPPORT_PLAY
|
|
|
|
)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
|
2020-06-03 15:46:42 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2020-06-03 12:01:56 +00:00
|
|
|
"""Set up the Dune HD media player platform."""
|
2017-08-08 18:21:33 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
2020-06-03 12:01:56 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data={CONF_HOST: host}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Add Dune HD entities from a config_entry."""
|
|
|
|
unique_id = config_entry.entry_id
|
|
|
|
|
|
|
|
player = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
|
|
|
|
async_add_entities([DuneHDPlayerEntity(player, DEFAULT_NAME, unique_id)], True)
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:00:57 +00:00
|
|
|
class DuneHDPlayerEntity(MediaPlayerEntity):
|
2016-11-28 07:42:57 +00:00
|
|
|
"""Implementation of the Dune HD player."""
|
|
|
|
|
2020-06-03 12:01:56 +00:00
|
|
|
def __init__(self, player, name, unique_id):
|
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._media_title = None
|
|
|
|
self._state = None
|
2020-06-03 12:01:56 +00:00
|
|
|
self._unique_id = unique_id
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
if "playback_position" in self._state:
|
2016-11-28 07:42:57 +00:00
|
|
|
state = STATE_PLAYING
|
2020-06-03 12:01:56 +00:00
|
|
|
if self._state.get("player_state") in ("playing", "buffering", "photo_viewer"):
|
2016-11-28 07:42:57 +00:00
|
|
|
state = STATE_PLAYING
|
2019-07-31 19:25:30 +00:00
|
|
|
if int(self._state.get("playback_speed", 1234)) == 0:
|
2016-11-28 07:42:57 +00:00
|
|
|
state = STATE_PAUSED
|
2020-05-28 10:27:15 +00:00
|
|
|
if self._state.get("player_state") == "navigator":
|
2016-11-28 07:42:57 +00:00
|
|
|
state = STATE_ON
|
|
|
|
return state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
2020-05-28 10:27:15 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return bool(self._state)
|
|
|
|
|
2020-06-03 12:01:56 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self._unique_id)},
|
|
|
|
"name": DEFAULT_NAME,
|
|
|
|
"manufacturer": ATTR_MANUFACTURER,
|
|
|
|
}
|
|
|
|
|
2016-11-28 07:42:57 +00:00
|
|
|
@property
|
|
|
|
def volume_level(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the volume level of the media player (0..1)."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return int(self._state.get("playback_volume", 0)) / 100
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return a boolean if volume is currently muted."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return int(self._state.get("playback_mute", 0)) == 1
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
@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()
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Turn off media player."""
|
|
|
|
self._state = self._player.turn_on()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def media_pause(self):
|
|
|
|
"""Pause media player."""
|
|
|
|
self._state = self._player.pause()
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
def __update_title(self):
|
2020-05-28 10:27:15 +00:00
|
|
|
if self._state.get("player_state") == "bluray_playback":
|
2019-07-31 19:25:30 +00:00
|
|
|
self._media_title = "Blu-Ray"
|
2020-06-03 12:01:56 +00:00
|
|
|
elif self._state.get("player_state") == "photo_viewer":
|
|
|
|
self._media_title = "Photo Viewer"
|
|
|
|
elif self._state.get("playback_url"):
|
|
|
|
self._media_title = self._state["playback_url"].split("/")[-1]
|
|
|
|
else:
|
|
|
|
self._media_title = None
|
2016-11-28 07:42:57 +00:00
|
|
|
|
|
|
|
def media_previous_track(self):
|
|
|
|
"""Send previous track command."""
|
|
|
|
self._state = self._player.previous_track()
|
|
|
|
|
|
|
|
def media_next_track(self):
|
|
|
|
"""Send next track command."""
|
|
|
|
self._state = self._player.next_track()
|