From 2285a6761c646c2759288f93c9248f46a6e75cce Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 23 Mar 2016 16:29:21 -0600 Subject: [PATCH] Initial Yamaha Receiver Implementation Text Update Additional additions and better support for volume, and mute. Cleanup Added rxv to requirements_all Added yamaha.py to .coveragerc Made uppercase, and removed tabs Added requirements variable Added doc string for lint Removed global variable, and simplified state as per balloobs suggestion Refactored the component with balloobs suggestions -Added import in the method - Only get receiver information on init - A bit of cleanup Remove up and down volume Uneeded as this is handled by set volume instead Fixed a lint build error More lint fixes Removed unused imports Lint Fixes Simplified if statement Minor refactoring since the init calls update. Fixed lint error Just variable naming change Added support for an optional name for the receiver. Better error handling, a bit of refactoring based on balloobs suggestions Fixed lint error. Another lint error fix Changed raise to return Disable pylint error handling Pylint broad exception Made exception handling in the setup platform instead of the constructor. Lint error fix Refactored the way devices are found. This allows for multiple receivers --- .coveragerc | 1 + .../components/media_player/yamaha.py | 91 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 95 insertions(+) create mode 100644 homeassistant/components/media_player/yamaha.py diff --git a/.coveragerc b/.coveragerc index fa1eb376906..1286dc23e9a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -107,6 +107,7 @@ omit = homeassistant/components/media_player/snapcast.py homeassistant/components/media_player/sonos.py homeassistant/components/media_player/squeezebox.py + homeassistant/components/media_player/yamaha.py homeassistant/components/notify/free_mobile.py homeassistant/components/notify/googlevoice.py homeassistant/components/notify/instapush.py diff --git a/homeassistant/components/media_player/yamaha.py b/homeassistant/components/media_player/yamaha.py new file mode 100644 index 00000000000..53e7b996991 --- /dev/null +++ b/homeassistant/components/media_player/yamaha.py @@ -0,0 +1,91 @@ +""" +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, + MediaPlayerDevice) +from homeassistant.const import STATE_OFF, STATE_ON +REQUIREMENTS = ['rxv==0.1.9'] +_LOGGER = logging.getLogger(__name__) + +SUPPORT_YAMAHA = SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \ + SUPPORT_TURN_ON | SUPPORT_TURN_OFF + + +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()) + + +class YamahaDevice(MediaPlayerDevice): + """Representation of a Yamaha device.""" + + # pylint: disable=too-many-public-methods, abstract-method + def __init__(self, name, receiver): + """Initialize the Yamaha Receiver.""" + self._receiver = receiver + self._muted = False + self._volume = 0 + self._pwstate = STATE_OFF + 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 + self._volume = (self._receiver.volume/100) + 1 + + @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 + + @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.""" + receiver_vol = 100-(volume * 100) + 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 + self._volume = (self._receiver.volume/100) + 1 diff --git a/requirements_all.txt b/requirements_all.txt index 397cf7cabbf..f3640bc12ec 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -230,6 +230,9 @@ pywemo==0.3.19 # homeassistant.components.thermostat.radiotherm radiotherm==1.2 +# homeassistant.components.media_player.yamaha +rxv==0.1.9 + # homeassistant.components.media_player.samsungtv samsungctl==0.5.1