2016-03-23 22:29:21 +00:00
|
|
|
"""
|
|
|
|
Support for Yamaha Receivers.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/media_player.yamaha/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.media_player import (
|
|
|
|
SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET,
|
2016-04-14 04:13:12 +00:00
|
|
|
SUPPORT_SELECT_SOURCE, MediaPlayerDevice)
|
2016-03-23 22:29:21 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2016-04-14 04:13:12 +00:00
|
|
|
|
2016-04-05 18:17:42 +00:00
|
|
|
REQUIREMENTS = ['rxv==0.1.11']
|
2016-04-14 04:13:12 +00:00
|
|
|
|
2016-03-23 22:29:21 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SUPPORT_YAMAHA = SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
|
2016-04-28 10:03:24 +00:00
|
|
|
SUPPORT_TURN_ON | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE
|
|
|
|
|
|
|
|
CONF_SOURCE_NAMES = 'source_names'
|
|
|
|
CONF_SOURCE_IGNORE = 'source_ignore'
|
2016-03-23 22:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
|
|
"""Setup the Yamaha platform."""
|
|
|
|
import rxv
|
2016-04-28 10:03:24 +00:00
|
|
|
|
|
|
|
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())
|
2016-03-23 22:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class YamahaDevice(MediaPlayerDevice):
|
|
|
|
"""Representation of a Yamaha device."""
|
|
|
|
|
|
|
|
# pylint: disable=too-many-public-methods, abstract-method
|
2016-04-28 10:03:24 +00:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
def __init__(self, name, receiver, source_ignore, source_names):
|
2016-03-23 22:29:21 +00:00
|
|
|
"""Initialize the Yamaha Receiver."""
|
|
|
|
self._receiver = receiver
|
|
|
|
self._muted = False
|
|
|
|
self._volume = 0
|
|
|
|
self._pwstate = STATE_OFF
|
2016-04-14 04:13:12 +00:00
|
|
|
self._current_source = None
|
|
|
|
self._source_list = None
|
2016-04-28 10:03:24 +00:00
|
|
|
self._source_ignore = source_ignore
|
|
|
|
self._source_names = source_names
|
|
|
|
self._reverse_mapping = None
|
2016-03-23 22:29:21 +00:00
|
|
|
self.update()
|
|
|
|
self._name = name
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest details from the device."""
|
|
|
|
if self._receiver.on:
|
|
|
|
self._pwstate = STATE_ON
|
|
|
|
else:
|
|
|
|
self._pwstate = STATE_OFF
|
|
|
|
self._muted = self._receiver.mute
|
2016-04-28 10:03:24 +00:00
|
|
|
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)
|
2016-03-23 22:29:21 +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 volume_level(self):
|
|
|
|
"""Volume level of the media player (0..1)."""
|
|
|
|
return self._volume
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Boolean if volume is currently muted."""
|
|
|
|
return self._muted
|
|
|
|
|
2016-04-14 04:13:12 +00:00
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return the current input source."""
|
|
|
|
return self._current_source
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return self._source_list
|
|
|
|
|
2016-03-23 22:29:21 +00:00
|
|
|
@property
|
|
|
|
def supported_media_commands(self):
|
|
|
|
"""Flag of media commands that are supported."""
|
|
|
|
return SUPPORT_YAMAHA
|
|
|
|
|
|
|
|
def turn_off(self):
|
|
|
|
"""Turn off media player."""
|
|
|
|
self._receiver.on = False
|
|
|
|
|
|
|
|
def set_volume_level(self, volume):
|
|
|
|
"""Set volume level, range 0..1."""
|
2016-04-28 10:03:24 +00:00
|
|
|
receiver_vol = 100 - (volume * 100)
|
2016-03-23 22:29:21 +00:00
|
|
|
negative_receiver_vol = -receiver_vol
|
|
|
|
self._receiver.volume = negative_receiver_vol
|
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
|
|
|
"""Mute (true) or unmute (false) media player."""
|
|
|
|
self._receiver.mute = mute
|
|
|
|
|
|
|
|
def turn_on(self):
|
|
|
|
"""Turn the media player on."""
|
|
|
|
self._receiver.on = True
|
2016-04-28 10:03:24 +00:00
|
|
|
self._volume = (self._receiver.volume / 100) + 1
|
2016-04-14 04:13:12 +00:00
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Select input source."""
|
2016-04-28 10:03:24 +00:00
|
|
|
self._receiver.input = self._reverse_mapping.get(source,
|
|
|
|
source)
|