core/homeassistant/components/epson/media_player.py

257 lines
7.4 KiB
Python
Raw Normal View History

"""Support for Epson projector."""
import logging
2018-09-09 12:26:06 +00:00
import voluptuous as vol
2019-07-31 19:25:30 +00:00
from homeassistant.components.media_player import MediaPlayerDevice, PLATFORM_SCHEMA
from homeassistant.components.media_player.const import (
2019-07-31 19:25:30 +00:00
DOMAIN,
SUPPORT_NEXT_TRACK,
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_STEP,
)
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_ENTITY_ID,
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_SSL,
STATE_OFF,
STATE_ON,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
2018-09-09 12:26:06 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_CMODE = "cmode"
2018-09-09 12:26:06 +00:00
2019-07-31 19:25:30 +00:00
DATA_EPSON = "epson"
DEFAULT_NAME = "EPSON Projector"
2019-07-31 19:25:30 +00:00
SERVICE_SELECT_CMODE = "epson_select_cmode"
SUPPORT_CMODE = 33001
2019-07-31 19:25:30 +00:00
SUPPORT_EPSON = (
SUPPORT_TURN_ON
| SUPPORT_TURN_OFF
| SUPPORT_SELECT_SOURCE
| SUPPORT_CMODE
| SUPPORT_VOLUME_MUTE
| SUPPORT_VOLUME_STEP
| SUPPORT_NEXT_TRACK
| SUPPORT_PREVIOUS_TRACK
)
MEDIA_PLAYER_SCHEMA = vol.Schema({ATTR_ENTITY_ID: cv.comp_entity_ids})
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,
vol.Optional(CONF_SSL, default=False): cv.boolean,
}
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Epson media player platform."""
2019-07-31 19:25:30 +00:00
from epson_projector.const import CMODE_LIST_SET
2018-09-09 12:26:06 +00:00
if DATA_EPSON not in hass.data:
hass.data[DATA_EPSON] = []
2018-09-09 12:26:06 +00:00
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
2018-09-09 12:26:06 +00:00
port = config.get(CONF_PORT)
ssl = config.get(CONF_SSL)
2019-07-31 19:25:30 +00:00
epson = EpsonProjector(
async_get_clientsession(hass, verify_ssl=False), name, host, port, ssl
)
hass.data[DATA_EPSON].append(epson)
async_add_entities([epson], update_before_add=True)
async def async_service_handler(service):
"""Handle for services."""
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
2019-07-31 19:25:30 +00:00
devices = [
device
for device in hass.data[DATA_EPSON]
if device.entity_id in entity_ids
]
else:
devices = hass.data[DATA_EPSON]
for device in devices:
if service.service == SERVICE_SELECT_CMODE:
cmode = service.data.get(ATTR_CMODE)
await device.select_cmode(cmode)
device.async_schedule_update_ha_state(True)
2018-09-09 12:26:06 +00:00
2019-07-31 19:25:30 +00:00
epson_schema = MEDIA_PLAYER_SCHEMA.extend(
{vol.Required(ATTR_CMODE): vol.All(cv.string, vol.Any(*CMODE_LIST_SET))}
)
hass.services.async_register(
2019-07-31 19:25:30 +00:00
DOMAIN, SERVICE_SELECT_CMODE, async_service_handler, schema=epson_schema
)
class EpsonProjector(MediaPlayerDevice):
"""Representation of Epson Projector Device."""
def __init__(self, websession, name, host, port, encryption):
"""Initialize entity to control Epson projector."""
import epson_projector as epson
from epson_projector.const import DEFAULT_SOURCES
2018-09-09 12:26:06 +00:00
self._name = name
2019-07-31 19:25:30 +00:00
self._projector = epson.Projector(host, websession=websession, port=port)
self._cmode = None
self._source_list = list(DEFAULT_SOURCES.values())
self._source = None
self._volume = None
self._state = None
async def async_update(self):
"""Update state of device."""
from epson_projector.const import (
2019-07-31 19:25:30 +00:00
EPSON_CODES,
POWER,
CMODE,
CMODE_LIST,
SOURCE,
VOLUME,
BUSY,
SOURCE_LIST,
)
is_turned_on = await self._projector.get_property(POWER)
_LOGGER.debug("Project turn on/off status: %s", is_turned_on)
if is_turned_on and is_turned_on == EPSON_CODES[POWER]:
self._state = STATE_ON
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
elif is_turned_on == BUSY:
self._state = STATE_ON
else:
self._state = STATE_OFF
@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 supported_features(self):
"""Flag media player features that are supported."""
return SUPPORT_EPSON
async def async_turn_on(self):
"""Turn on epson."""
from epson_projector.const import TURN_ON
2019-07-31 19:25:30 +00:00
if self._state == STATE_OFF:
await self._projector.send_command(TURN_ON)
async def async_turn_off(self):
"""Turn off epson."""
from epson_projector.const import TURN_OFF
2019-07-31 19:25:30 +00:00
if self._state == STATE_ON:
await self._projector.send_command(TURN_OFF)
@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."""
2019-07-31 19:25:30 +00:00
from epson_projector.const import CMODE_LIST_SET
await self._projector.send_command(CMODE_LIST_SET[cmode])
async def async_select_source(self, source):
"""Select input source."""
from epson_projector.const import INV_SOURCES
2019-07-31 19:25:30 +00:00
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."""
from epson_projector.const import MUTE
2019-07-31 19:25:30 +00:00
await self._projector.send_command(MUTE)
async def async_volume_up(self):
"""Increase volume."""
from epson_projector.const import VOL_UP
2019-07-31 19:25:30 +00:00
await self._projector.send_command(VOL_UP)
async def async_volume_down(self):
"""Decrease volume."""
from epson_projector.const import VOL_DOWN
2019-07-31 19:25:30 +00:00
await self._projector.send_command(VOL_DOWN)
async def async_media_play(self):
"""Play media via Epson."""
from epson_projector.const import PLAY
2019-07-31 19:25:30 +00:00
await self._projector.send_command(PLAY)
async def async_media_pause(self):
"""Pause media via Epson."""
from epson_projector.const import PAUSE
2019-07-31 19:25:30 +00:00
await self._projector.send_command(PAUSE)
async def async_media_next_track(self):
"""Skip to next."""
from epson_projector.const import FAST
2019-07-31 19:25:30 +00:00
await self._projector.send_command(FAST)
async def async_media_previous_track(self):
"""Skip to previous."""
from epson_projector.const import BACK
2019-07-31 19:25:30 +00:00
await self._projector.send_command(BACK)
@property
def device_state_attributes(self):
"""Return device specific state attributes."""
attributes = {}
if self._cmode is not None:
attributes[ATTR_CMODE] = self._cmode
return attributes