Yamaha: added mapping and exclude lists (#1880)

* added mapping and exclude lists

* reworked pr

* made code more pythonic
pull/1930/merge
Dennis Karpienski 2016-04-28 12:03:24 +02:00 committed by Paulus Schoutsen
parent 0d261be6ce
commit 953223b81b
1 changed files with 38 additions and 10 deletions

View File

@ -16,21 +16,30 @@ REQUIREMENTS = ['rxv==0.1.11']
_LOGGER = logging.getLogger(__name__)
SUPPORT_YAMAHA = SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE
CONF_SOURCE_NAMES = 'source_names'
CONF_SOURCE_IGNORE = 'source_ignore'
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Yamaha platform."""
import rxv
add_devices(YamahaDevice(config.get("name"), receiver)
for receiver in rxv.find())
source_ignore = config.get(CONF_SOURCE_IGNORE, [])
source_names = config.get(CONF_SOURCE_NAMES, {})
add_devices(
YamahaDevice(config.get("name"), receiver, source_ignore, source_names)
for receiver in rxv.find())
class YamahaDevice(MediaPlayerDevice):
"""Representation of a Yamaha device."""
# pylint: disable=too-many-public-methods, abstract-method
def __init__(self, name, receiver):
# pylint: disable=too-many-instance-attributes
def __init__(self, name, receiver, source_ignore, source_names):
"""Initialize the Yamaha Receiver."""
self._receiver = receiver
self._muted = False
@ -38,6 +47,9 @@ class YamahaDevice(MediaPlayerDevice):
self._pwstate = STATE_OFF
self._current_source = None
self._source_list = None
self._source_ignore = source_ignore
self._source_names = source_names
self._reverse_mapping = None
self.update()
self._name = name
@ -48,9 +60,24 @@ class YamahaDevice(MediaPlayerDevice):
else:
self._pwstate = STATE_OFF
self._muted = self._receiver.mute
self._volume = (self._receiver.volume/100) + 1
self._current_source = self._receiver.input
self._source_list = list(self._receiver.inputs().keys())
self._volume = (self._receiver.volume / 100) + 1
if self.source_list is None:
self.build_source_list()
current_source = self._receiver.input
self._current_source = self._source_names.get(current_source,
current_source)
def build_source_list(self):
"""Build the source list."""
self._reverse_mapping = {alias: source for source, alias in
self._source_names.items()}
self._source_list = sorted(
self._source_names.get(source, source) for source in
self._receiver.inputs()
if source not in self._source_ignore)
@property
def name(self):
@ -93,7 +120,7 @@ class YamahaDevice(MediaPlayerDevice):
def set_volume_level(self, volume):
"""Set volume level, range 0..1."""
receiver_vol = 100-(volume * 100)
receiver_vol = 100 - (volume * 100)
negative_receiver_vol = -receiver_vol
self._receiver.volume = negative_receiver_vol
@ -104,8 +131,9 @@ class YamahaDevice(MediaPlayerDevice):
def turn_on(self):
"""Turn the media player on."""
self._receiver.on = True
self._volume = (self._receiver.volume/100) + 1
self._volume = (self._receiver.volume / 100) + 1
def select_source(self, source):
"""Select input source."""
self._receiver.input = source
self._receiver.input = self._reverse_mapping.get(source,
source)