Added service select_video_output and video_out attribute (#18081)

* Added service select_video_output and video_out attribute

* Fixed white lines and long lines

* Made line shorter

* Added period to comment according to flake8 rules

* Imported domain const

* Prefixed service with platform name

* changed "video output" to "hdmi output" to better reflect eiscp cammand

* video_output to hdmi_output rename
pull/18119/head
Leothlon 2018-11-02 11:07:36 +01:00 committed by Paulus Schoutsen
parent cb7ae5cdf2
commit 8613694544
1 changed files with 39 additions and 3 deletions

View File

@ -14,8 +14,9 @@ import voluptuous as vol
from homeassistant.components.media_player import (
PLATFORM_SCHEMA, SUPPORT_PLAY, SUPPORT_PLAY_MEDIA, SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,
SUPPORT_VOLUME_STEP, MediaPlayerDevice)
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON
SUPPORT_VOLUME_STEP, MediaPlayerDevice, DOMAIN)
from homeassistant.const import (
CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON, ATTR_ENTITY_ID)
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['onkyo-eiscp==1.2.4']
@ -55,6 +56,16 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
TIMEOUT_MESSAGE = 'Timeout waiting for response.'
ATTR_HDMI_OUTPUT = 'hdmi_output'
ACCEPTED_VALUES = ['no', 'analog', 'yes', 'out',
'out-sub', 'sub', 'hdbaset', 'both', 'up']
ONKYO_SELECT_OUTPUT_SCHEMA = vol.Schema({
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
vol.Required(ATTR_HDMI_OUTPUT): vol.In(ACCEPTED_VALUES)
})
SERVICE_SELECT_HDMI_OUTPUT = 'onkyo_select_hdmi_output'
def determine_zones(receiver):
"""Determine what zones are available for the receiver."""
@ -90,6 +101,19 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
host = config.get(CONF_HOST)
hosts = []
def service_handle(service):
"""Handle for services."""
entity_ids = service.data.get(ATTR_ENTITY_ID)
devices = [d for d in hosts if d.entity_id in entity_ids]
for device in devices:
if service.service == SERVICE_SELECT_HDMI_OUTPUT:
device.select_output(service.data.get(ATTR_HDMI_OUTPUT))
hass.services.register(
DOMAIN, SERVICE_SELECT_HDMI_OUTPUT, service_handle,
schema=ONKYO_SELECT_OUTPUT_SCHEMA)
if CONF_HOST in config and host not in KNOWN_HOSTS:
try:
receiver = eiscp.eISCP(host)
@ -144,6 +168,7 @@ class OnkyoDevice(MediaPlayerDevice):
self._source_list = list(sources.values())
self._source_mapping = sources
self._reverse_mapping = {value: key for key, value in sources.items()}
self._attributes = {}
def command(self, command):
"""Run an eiscp command and catch connection errors."""
@ -174,6 +199,7 @@ class OnkyoDevice(MediaPlayerDevice):
volume_raw = self.command('volume query')
mute_raw = self.command('audio-muting query')
current_source_raw = self.command('input-selector query')
hdmi_out_raw = self.command('hdmi-output-selector query')
if not (volume_raw and mute_raw and current_source_raw):
return
@ -194,6 +220,7 @@ class OnkyoDevice(MediaPlayerDevice):
[i for i in current_source_tuples[1]])
self._muted = bool(mute_raw[1] == 'on')
self._volume = volume_raw[1] / self._max_volume
self._attributes["video_out"] = ','.join(hdmi_out_raw[1])
@property
def name(self):
@ -230,6 +257,11 @@ class OnkyoDevice(MediaPlayerDevice):
"""List of available input sources."""
return self._source_list
@property
def device_state_attributes(self):
"""Return device specific state attributes."""
return self._attributes
def turn_off(self):
"""Turn the media player off."""
self.command('system-power standby')
@ -275,6 +307,10 @@ class OnkyoDevice(MediaPlayerDevice):
source in DEFAULT_PLAYABLE_SOURCES):
self.command('preset {}'.format(media_id))
def select_output(self, output):
"""Set hdmi-out."""
self.command('hdmi-output-selector={}'.format(output))
class OnkyoDeviceZone(OnkyoDevice):
"""Representation of an Onkyo device's extra zone."""
@ -346,7 +382,7 @@ class OnkyoDeviceZone(OnkyoDevice):
def set_volume_level(self, volume):
"""Set volume level, input is range 0..1. Onkyo ranges from 1-80."""
self.command('zone{}.volume={}'.format(self._zone, int(volume*80)))
self.command('zone{}.volume={}'.format(self._zone, int(volume * 80)))
def volume_up(self):
"""Increase volume by 1 step."""