From dc551b825fb94fa6a708aed3524642073291065b Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 14 Dec 2016 05:04:40 +0100 Subject: [PATCH] =?UTF-8?q?Added=20a=20volume=20set=20option=20and=20autod?= =?UTF-8?q?iscovery=20functions=20to=20Denon=20AVR=20rece=E2=80=A6=20(#484?= =?UTF-8?q?5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added Volume Set option and autodiscovery functions to Denon AVR receivers * Corrected issues in SSDP discovery and in case no host could be discovered * Corrected discovery handling / added denonavr to discovery platform * No needless discoveries anymore / add_devices() with list instead of loop --- homeassistant/components/discovery.py | 3 +- .../components/media_player/denonavr.py | 78 ++++++++++++++++--- requirements_all.txt | 4 +- 3 files changed, 73 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/discovery.py b/homeassistant/components/discovery.py index 3c4dff6eac5..74dc5d69220 100644 --- a/homeassistant/components/discovery.py +++ b/homeassistant/components/discovery.py @@ -14,7 +14,7 @@ import voluptuous as vol from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.helpers.discovery import load_platform, discover -REQUIREMENTS = ['netdisco==0.8.0'] +REQUIREMENTS = ['netdisco==0.8.1'] DOMAIN = 'discovery' @@ -36,6 +36,7 @@ SERVICE_HANDLERS = { 'yamaha': ('media_player', 'yamaha'), 'logitech_mediaserver': ('media_player', 'squeezebox'), 'directv': ('media_player', 'directv'), + 'denonavr': ('media_player', 'denonavr'), } CONFIG_SCHEMA = vol.Schema({ diff --git a/homeassistant/components/media_player/denonavr.py b/homeassistant/components/media_player/denonavr.py index 6db223bc6ec..5784fd6c829 100644 --- a/homeassistant/components/media_player/denonavr.py +++ b/homeassistant/components/media_player/denonavr.py @@ -13,26 +13,27 @@ from homeassistant.components.media_player import ( SUPPORT_TURN_OFF, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_STEP, SUPPORT_SELECT_SOURCE, SUPPORT_PLAY_MEDIA, MEDIA_TYPE_CHANNEL, MediaPlayerDevice, PLATFORM_SCHEMA, SUPPORT_TURN_ON, - MEDIA_TYPE_MUSIC) + MEDIA_TYPE_MUSIC, SUPPORT_VOLUME_SET) from homeassistant.const import ( CONF_HOST, STATE_OFF, STATE_PLAYING, STATE_PAUSED, CONF_NAME, STATE_ON) import homeassistant.helpers.config_validation as cv -REQUIREMENTS = ['denonavr==0.1.6'] +REQUIREMENTS = ['denonavr==0.2.2'] _LOGGER = logging.getLogger(__name__) DEFAULT_NAME = None +KEY_DENON_CACHE = 'denonavr_hosts' SUPPORT_DENON = SUPPORT_VOLUME_STEP | SUPPORT_VOLUME_MUTE | \ SUPPORT_TURN_ON | SUPPORT_TURN_OFF | \ SUPPORT_SELECT_SOURCE | SUPPORT_PLAY_MEDIA | \ SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | \ - SUPPORT_NEXT_TRACK + SUPPORT_NEXT_TRACK | SUPPORT_VOLUME_SET PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_HOST): cv.string, + vol.Optional(CONF_HOST): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, }) @@ -41,11 +42,53 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the Denon platform.""" import denonavr - receiver = denonavr.DenonAVR(config.get(CONF_HOST), config.get(CONF_NAME)) + # Initialize list with receivers to be started + receivers = [] - add_devices([DenonDevice(receiver)]) - _LOGGER.info("Denon receiver at host %s initialized", - config.get(CONF_HOST)) + cache = hass.data.get(KEY_DENON_CACHE) + if cache is None: + cache = hass.data[KEY_DENON_CACHE] = set() + + # Start assignment of host and name + # 1. option: manual setting + if config.get(CONF_HOST) is not None: + host = config.get(CONF_HOST) + name = config.get(CONF_NAME) + # Check if host not in cache, append it and save for later starting + if host not in cache: + cache.add(host) + receivers.append( + DenonDevice(denonavr.DenonAVR(host, name))) + _LOGGER.info("Denon receiver at host %s initialized", host) + # 2. option: discovery using netdisco + if discovery_info is not None: + host = discovery_info[0] + name = discovery_info[1] + # Check if host not in cache, append it and save for later starting + if host not in cache: + cache.add(host) + receivers.append( + DenonDevice(denonavr.DenonAVR(host, name))) + _LOGGER.info("Denon receiver at host %s initialized", host) + # 3. option: discovery using denonavr library + if config.get(CONF_HOST) is None and discovery_info is None: + d_receivers = denonavr.discover() + # More than one receiver could be discovered by that method + if d_receivers is not None: + for d_receiver in d_receivers: + host = d_receiver["host"] + name = d_receiver["friendlyName"] + # Check if host not in cache, append it and save for later + # starting + if host not in cache: + cache.add(host) + receivers.append( + DenonDevice(denonavr.DenonAVR(host, name))) + _LOGGER.info("Denon receiver at host %s initialized", host) + + # Add all freshly discovered receivers + if receivers: + add_devices(receivers) class DenonDevice(MediaPlayerDevice): @@ -107,7 +150,8 @@ class DenonDevice(MediaPlayerDevice): @property def volume_level(self): """Volume level of the media player (0..1).""" - # Volume is send in a format like -50.0. Minimum is around -80.0 + # Volume is sent in a format like -50.0. Minimum is -80.0, + # maximum is 18.0 return (float(self._volume) + 80) / 100 @property @@ -240,6 +284,22 @@ class DenonDevice(MediaPlayerDevice): """Volume down media player.""" return self._receiver.volume_down() + def set_volume_level(self, volume): + """Set volume level, range 0..1.""" + # Volume has to be sent in a format like -50.0. Minimum is -80.0, + # maximum is 18.0 + volume_denon = float((volume * 100) - 80) + if volume_denon > 18: + volume_denon = float(18) + try: + if self._receiver.set_volume(volume_denon): + self._volume = volume_denon + return True + else: + return False + except ValueError: + return False + def mute_volume(self, mute): """Send mute command.""" return self._receiver.mute(mute) diff --git a/requirements_all.txt b/requirements_all.txt index c85e3285e7e..924f534145d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -76,7 +76,7 @@ colorlog>2.1,<3 concord232==0.14 # homeassistant.components.media_player.denonavr -denonavr==0.1.6 +denonavr==0.2.2 # homeassistant.components.media_player.directv directpy==0.1 @@ -302,7 +302,7 @@ mficlient==0.3.0 miflora==0.1.13 # homeassistant.components.discovery -netdisco==0.8.0 +netdisco==0.8.1 # homeassistant.components.sensor.neurio_energy neurio==0.2.10