From 4fa379419d9700fb179a9d3440f4e618d8fe50ec Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Sat, 12 Sep 2015 23:09:51 -0500 Subject: [PATCH 1/2] Don't blow up if no sonos speakers are found Also move the imports up so the latest pep8 doesn't complain --- homeassistant/components/media_player/sonos.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/media_player/sonos.py b/homeassistant/components/media_player/sonos.py index 9020fa41b4d..29ee7358c41 100644 --- a/homeassistant/components/media_player/sonos.py +++ b/homeassistant/components/media_player/sonos.py @@ -15,8 +15,6 @@ media_player: import logging import datetime -REQUIREMENTS = ['SoCo==0.11.1'] - from homeassistant.components.media_player import ( MediaPlayerDevice, SUPPORT_PAUSE, SUPPORT_SEEK, SUPPORT_VOLUME_SET, SUPPORT_VOLUME_MUTE, SUPPORT_PREVIOUS_TRACK, SUPPORT_NEXT_TRACK, @@ -26,6 +24,9 @@ from homeassistant.helpers.event import track_utc_time_change from homeassistant.const import ( STATE_IDLE, STATE_PLAYING, STATE_PAUSED, STATE_UNKNOWN) + +REQUIREMENTS = ['SoCo==0.11.1'] + _LOGGER = logging.getLogger(__name__) SUPPORT_SONOS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |\ @@ -37,7 +38,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the Sonos platform. """ import soco - add_devices(SonosDevice(hass, p) for p in soco.discover()) + + players = soco.discover() + if not players: + _LOGGER.warning('No Sonos speakers found. Disabling: ' + __name__) + return False + + add_devices(SonosDevice(hass, p) for p in players) return True From 57a833f1a7b8d91adfab0f76dcc90d026f9bc893 Mon Sep 17 00:00:00 2001 From: Jeff Schroeder Date: Sat, 12 Sep 2015 23:39:13 -0500 Subject: [PATCH 2/2] Fix a bug which causes the sonos component to occasionally pop Had this happen when Sonos surround sound is playing from a TV. See this for more details: https://github.com/SoCo/SoCo/blob/af9a5152fe942fc665b0269b0f245330db0671ec/soco/core.py#L1060 --- homeassistant/components/media_player/sonos.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/media_player/sonos.py b/homeassistant/components/media_player/sonos.py index 29ee7358c41..3deacd84e10 100644 --- a/homeassistant/components/media_player/sonos.py +++ b/homeassistant/components/media_player/sonos.py @@ -36,15 +36,15 @@ SUPPORT_SONOS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |\ # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): """ Sets up the Sonos platform. """ - import soco players = soco.discover() if not players: - _LOGGER.warning('No Sonos speakers found. Disabling: ' + __name__) + _LOGGER.warning('No Sonos speakers found. Disabling: %s', __name__) return False add_devices(SonosDevice(hass, p) for p in players) + _LOGGER.info('Added %s Sonos speakers', len(players)) return True @@ -120,6 +120,13 @@ class SonosDevice(MediaPlayerDevice): def media_duration(self): """ Duration of current playing media in seconds. """ dur = self._trackinfo.get('duration', '0:00') + + # If the speaker is playing from the "line-in" source, getting + # track metadata can return NOT_IMPLEMENTED, which breaks the + # volume logic below + if dur == 'NOT_IMPLEMENTED': + return None + return sum(60 ** x[0] * int(x[1]) for x in enumerate(reversed(dur.split(':'))))