2019-04-03 15:40:03 +00:00
|
|
|
"""Support for controlling projector via the PJLink protocol."""
|
2018-08-09 17:58:16 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
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_SELECT_SOURCE, SUPPORT_TURN_OFF, SUPPORT_TURN_ON,
|
|
|
|
SUPPORT_VOLUME_MUTE)
|
2018-08-09 17:58:16 +00:00
|
|
|
from homeassistant.const import (
|
2018-09-09 12:26:06 +00:00
|
|
|
CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_PORT, STATE_OFF, STATE_ON)
|
2018-08-09 17:58:16 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CONF_ENCODING = 'encoding'
|
|
|
|
|
|
|
|
DEFAULT_PORT = 4352
|
|
|
|
DEFAULT_ENCODING = 'utf-8'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
SUPPORT_PJLINK = SUPPORT_VOLUME_MUTE | \
|
|
|
|
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-08-09 17:58:16 +00:00
|
|
|
"""Set up the PJLink platform."""
|
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
port = config.get(CONF_PORT)
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
encoding = config.get(CONF_ENCODING)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
|
|
|
|
if 'pjlink' not in hass.data:
|
|
|
|
hass.data['pjlink'] = {}
|
|
|
|
hass_data = hass.data['pjlink']
|
|
|
|
|
|
|
|
device_label = "{}:{}".format(host, port)
|
|
|
|
if device_label in hass_data:
|
|
|
|
return
|
|
|
|
|
|
|
|
device = PjLinkDevice(host, port, name, encoding, password)
|
|
|
|
hass_data[device_label] = device
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([device], True)
|
2018-08-09 17:58:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_input_source(input_source_name, input_source_number):
|
|
|
|
"""Format input source for display in UI."""
|
|
|
|
return "{} {}".format(input_source_name, input_source_number)
|
|
|
|
|
|
|
|
|
|
|
|
class PjLinkDevice(MediaPlayerDevice):
|
|
|
|
"""Representation of a PJLink device."""
|
|
|
|
|
|
|
|
def __init__(self, host, port, name, encoding, password):
|
|
|
|
"""Iinitialize the PJLink device."""
|
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
self._name = name
|
|
|
|
self._password = password
|
|
|
|
self._encoding = encoding
|
|
|
|
self._muted = False
|
|
|
|
self._pwstate = STATE_OFF
|
|
|
|
self._current_source = None
|
|
|
|
with self.projector() as projector:
|
|
|
|
if not self._name:
|
|
|
|
self._name = projector.get_name()
|
|
|
|
inputs = projector.get_inputs()
|
|
|
|
self._source_name_mapping = \
|
|
|
|
{format_input_source(*x): x for x in inputs}
|
|
|
|
self._source_list = sorted(self._source_name_mapping.keys())
|
|
|
|
|
|
|
|
def projector(self):
|
|
|
|
"""Create PJLink Projector instance."""
|
|
|
|
from pypjlink import Projector
|
2018-09-09 12:26:06 +00:00
|
|
|
projector = Projector.from_address(
|
|
|
|
self._host, self._port, self._encoding)
|
2018-08-09 17:58:16 +00:00
|
|
|
projector.authenticate(self._password)
|
|
|
|
return projector
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest state from the device."""
|
2019-02-01 13:53:40 +00:00
|
|
|
from pypjlink.projector import ProjectorError
|
2018-08-09 17:58:16 +00:00
|
|
|
with self.projector() as projector:
|
2019-02-01 13:53:40 +00:00
|
|
|
try:
|
|
|
|
pwstate = projector.get_power()
|
|
|
|
if pwstate in ('on', 'warm-up'):
|
|
|
|
self._pwstate = STATE_ON
|
|
|
|
else:
|
|
|
|
self._pwstate = STATE_OFF
|
|
|
|
self._muted = projector.get_mute()[1]
|
|
|
|
self._current_source = \
|
|
|
|
format_input_source(*projector.get_input())
|
|
|
|
except KeyError as err:
|
|
|
|
if str(err) == "'OK'":
|
|
|
|
self._pwstate = STATE_OFF
|
|
|
|
self._muted = False
|
|
|
|
self._current_source = None
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
except ProjectorError as err:
|
|
|
|
if str(err) == 'unavailable time':
|
|
|
|
self._pwstate = STATE_OFF
|
|
|
|
self._muted = False
|
|
|
|
self._current_source = None
|
|
|
|
else:
|
|
|
|
raise
|
2018-08-09 17:58:16 +00:00
|
|
|
|
|
|
|
@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._pwstate
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Return boolean indicating mute status."""
|
|
|
|
return self._muted
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return current input source."""
|
|
|
|
return self._current_source
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""Return all available input sources."""
|
|
|
|
return self._source_list
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Return projector supported features."""
|
|
|
|
return SUPPORT_PJLINK
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn projector off."""
|
|
|
|
with self.projector() as projector:
|
|
|
|
projector.set_power('off')
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Turn projector on."""
|
|
|
|
with self.projector() as projector:
|
|
|
|
projector.set_power('on')
|
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Mute (true) of unmute (false) media player."""
|
|
|
|
with self.projector() as projector:
|
|
|
|
from pypjlink import MUTE_AUDIO
|
|
|
|
projector.set_mute(MUTE_AUDIO, mute)
|
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Set the input source."""
|
|
|
|
source = self._source_name_mapping[source]
|
|
|
|
with self.projector() as projector:
|
|
|
|
projector.set_input(*source)
|