2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Epson projector."""
|
2018-06-13 05:28:59 +00:00
|
|
|
import logging
|
2018-09-09 12:26:06 +00:00
|
|
|
|
2019-10-12 20:04:14 +00:00
|
|
|
from epson_projector.const import (
|
|
|
|
BACK,
|
|
|
|
BUSY,
|
|
|
|
CMODE,
|
|
|
|
CMODE_LIST,
|
|
|
|
CMODE_LIST_SET,
|
|
|
|
DEFAULT_SOURCES,
|
|
|
|
EPSON_CODES,
|
|
|
|
FAST,
|
|
|
|
INV_SOURCES,
|
|
|
|
MUTE,
|
|
|
|
PAUSE,
|
|
|
|
PLAY,
|
|
|
|
POWER,
|
|
|
|
SOURCE,
|
|
|
|
SOURCE_LIST,
|
2020-11-10 08:55:42 +00:00
|
|
|
STATE_UNAVAILABLE as EPSON_STATE_UNAVAILABLE,
|
2019-10-12 20:04:14 +00:00
|
|
|
TURN_OFF,
|
2019-12-09 13:10:04 +00:00
|
|
|
TURN_ON,
|
2019-10-12 20:04:14 +00:00
|
|
|
VOL_DOWN,
|
|
|
|
VOL_UP,
|
2019-12-09 13:10:04 +00:00
|
|
|
VOLUME,
|
2019-10-12 20:04:14 +00:00
|
|
|
)
|
2019-12-09 13:10:04 +00:00
|
|
|
import voluptuous as vol
|
2019-10-12 20:04:14 +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
|
|
|
SUPPORT_NEXT_TRACK,
|
|
|
|
SUPPORT_PREVIOUS_TRACK,
|
|
|
|
SUPPORT_SELECT_SOURCE,
|
|
|
|
SUPPORT_TURN_OFF,
|
|
|
|
SUPPORT_TURN_ON,
|
|
|
|
SUPPORT_VOLUME_MUTE,
|
|
|
|
SUPPORT_VOLUME_STEP,
|
|
|
|
)
|
2020-11-07 17:15:29 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
2020-11-10 11:58:00 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, STATE_OFF, STATE_ON
|
2020-11-07 17:15:29 +00:00
|
|
|
from homeassistant.helpers import entity_platform
|
2018-06-13 05:28:59 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-09 13:10:04 +00:00
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
from .const import ATTR_CMODE, DEFAULT_NAME, DOMAIN, SERVICE_SELECT_CMODE
|
2018-06-13 05:28:59 +00:00
|
|
|
|
2018-09-09 12:26:06 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_EPSON = (
|
|
|
|
SUPPORT_TURN_ON
|
|
|
|
| SUPPORT_TURN_OFF
|
|
|
|
| SUPPORT_SELECT_SOURCE
|
|
|
|
| SUPPORT_VOLUME_MUTE
|
|
|
|
| SUPPORT_VOLUME_STEP
|
|
|
|
| SUPPORT_NEXT_TRACK
|
|
|
|
| SUPPORT_PREVIOUS_TRACK
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=80): cv.port,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Epson projector from a config entry."""
|
|
|
|
unique_id = config_entry.entry_id
|
|
|
|
projector = hass.data[DOMAIN][unique_id]
|
|
|
|
projector_entity = EpsonProjectorMediaPlayer(
|
|
|
|
projector, config_entry.title, unique_id
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-11-07 17:15:29 +00:00
|
|
|
async_add_entities([projector_entity], True)
|
|
|
|
platform = entity_platform.current_platform.get()
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_SELECT_CMODE,
|
|
|
|
{vol.Required(ATTR_CMODE): vol.All(cv.string, vol.Any(*CMODE_LIST_SET))},
|
|
|
|
SERVICE_SELECT_CMODE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-11-07 17:15:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up the Epson projector."""
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-06-13 05:28:59 +00:00
|
|
|
|
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
class EpsonProjectorMediaPlayer(MediaPlayerEntity):
|
2018-06-13 05:28:59 +00:00
|
|
|
"""Representation of Epson Projector Device."""
|
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
def __init__(self, projector, name, unique_id):
|
2018-06-13 05:28:59 +00:00
|
|
|
"""Initialize entity to control Epson projector."""
|
2018-09-09 12:26:06 +00:00
|
|
|
self._name = name
|
2020-11-07 17:15:29 +00:00
|
|
|
self._projector = projector
|
2020-11-10 11:58:00 +00:00
|
|
|
self._available = False
|
2018-06-13 05:28:59 +00:00
|
|
|
self._cmode = None
|
|
|
|
self._source_list = list(DEFAULT_SOURCES.values())
|
|
|
|
self._source = None
|
|
|
|
self._volume = None
|
|
|
|
self._state = None
|
2020-11-07 17:15:29 +00:00
|
|
|
self._unique_id = unique_id
|
2018-06-13 05:28:59 +00:00
|
|
|
|
2018-11-19 12:47:52 +00:00
|
|
|
async def async_update(self):
|
2018-06-13 05:28:59 +00:00
|
|
|
"""Update state of device."""
|
2020-11-07 17:15:29 +00:00
|
|
|
power_state = await self._projector.get_property(POWER)
|
|
|
|
_LOGGER.debug("Projector status: %s", power_state)
|
2020-11-10 11:58:00 +00:00
|
|
|
if not power_state or power_state == EPSON_STATE_UNAVAILABLE:
|
|
|
|
self._available = False
|
2020-11-07 17:15:29 +00:00
|
|
|
return
|
2020-11-10 11:58:00 +00:00
|
|
|
self._available = True
|
2020-11-07 17:15:29 +00:00
|
|
|
if power_state == EPSON_CODES[POWER]:
|
2018-06-13 05:28:59 +00:00
|
|
|
self._state = STATE_ON
|
2020-11-07 17:15:29 +00:00
|
|
|
self._source_list = list(DEFAULT_SOURCES.values())
|
2018-06-13 05:28:59 +00:00
|
|
|
cmode = await self._projector.get_property(CMODE)
|
|
|
|
self._cmode = CMODE_LIST.get(cmode, self._cmode)
|
|
|
|
source = await self._projector.get_property(SOURCE)
|
|
|
|
self._source = SOURCE_LIST.get(source, self._source)
|
|
|
|
volume = await self._projector.get_property(VOLUME)
|
|
|
|
if volume:
|
|
|
|
self._volume = volume
|
2020-11-07 17:15:29 +00:00
|
|
|
elif power_state == BUSY:
|
2018-06-13 05:28:59 +00:00
|
|
|
self._state = STATE_ON
|
|
|
|
else:
|
|
|
|
self._state = STATE_OFF
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique ID."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2018-06-13 05:28:59 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
|
|
|
return self._state
|
|
|
|
|
2020-11-10 11:58:00 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if projector is available."""
|
|
|
|
return self._available
|
|
|
|
|
2018-06-13 05:28:59 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
|
|
|
return SUPPORT_EPSON
|
|
|
|
|
|
|
|
async def async_turn_on(self):
|
|
|
|
"""Turn on epson."""
|
2019-04-11 16:32:25 +00:00
|
|
|
if self._state == STATE_OFF:
|
|
|
|
await self._projector.send_command(TURN_ON)
|
2018-06-13 05:28:59 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self):
|
|
|
|
"""Turn off epson."""
|
2019-04-11 16:32:25 +00:00
|
|
|
if self._state == STATE_ON:
|
|
|
|
await self._projector.send_command(TURN_OFF)
|
2018-06-13 05:28:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return self._source_list
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Get current input sources."""
|
|
|
|
return self._source
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Return the volume level of the media player (0..1)."""
|
|
|
|
return self._volume
|
|
|
|
|
|
|
|
async def select_cmode(self, cmode):
|
|
|
|
"""Set color mode in Epson."""
|
|
|
|
await self._projector.send_command(CMODE_LIST_SET[cmode])
|
|
|
|
|
|
|
|
async def async_select_source(self, source):
|
|
|
|
"""Select input source."""
|
|
|
|
selected_source = INV_SOURCES[source]
|
|
|
|
await self._projector.send_command(selected_source)
|
|
|
|
|
|
|
|
async def async_mute_volume(self, mute):
|
|
|
|
"""Mute (true) or unmute (false) sound."""
|
|
|
|
await self._projector.send_command(MUTE)
|
|
|
|
|
|
|
|
async def async_volume_up(self):
|
|
|
|
"""Increase volume."""
|
|
|
|
await self._projector.send_command(VOL_UP)
|
|
|
|
|
|
|
|
async def async_volume_down(self):
|
|
|
|
"""Decrease volume."""
|
|
|
|
await self._projector.send_command(VOL_DOWN)
|
|
|
|
|
|
|
|
async def async_media_play(self):
|
|
|
|
"""Play media via Epson."""
|
|
|
|
await self._projector.send_command(PLAY)
|
|
|
|
|
|
|
|
async def async_media_pause(self):
|
|
|
|
"""Pause media via Epson."""
|
|
|
|
await self._projector.send_command(PAUSE)
|
|
|
|
|
|
|
|
async def async_media_next_track(self):
|
|
|
|
"""Skip to next."""
|
|
|
|
await self._projector.send_command(FAST)
|
|
|
|
|
|
|
|
async def async_media_previous_track(self):
|
|
|
|
"""Skip to previous."""
|
|
|
|
await self._projector.send_command(BACK)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return device specific state attributes."""
|
2020-10-06 16:08:53 +00:00
|
|
|
if self._cmode is None:
|
|
|
|
return {}
|
|
|
|
return {ATTR_CMODE: self._cmode}
|