small sonos cleanup (#11607)

This commit is contained in:
Pascal Vizeli 2018-01-13 09:59:50 +01:00 committed by GitHub
parent 5e81736f88
commit b2b836d4c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -263,27 +263,14 @@ def _get_entity_from_soco(hass, soco):
raise ValueError("No entity for SoCo device") raise ValueError("No entity for SoCo device")
def soco_error(funct): def soco_error(errorcodes=None):
"""Catch soco exceptions.""" """Filter out specified UPnP errors from logs and avoid exceptions."""
@ft.wraps(funct)
def wrapper(*args, **kwargs):
"""Wrap for all soco exception."""
from soco.exceptions import SoCoException
try:
return funct(*args, **kwargs)
except SoCoException as err:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
return wrapper
def soco_filter_upnperror(errorcodes=None):
"""Filter out specified UPnP errors from logs."""
def decorator(funct): def decorator(funct):
"""Decorator function.""" """Decorator function."""
@ft.wraps(funct) @ft.wraps(funct)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
"""Wrap for all soco UPnP exception.""" """Wrap for all soco UPnP exception."""
from soco.exceptions import SoCoUPnPException from soco.exceptions import SoCoUPnPException, SoCoException
# Temporarily disable SoCo logging because it will log the # Temporarily disable SoCo logging because it will log the
# UPnP exception otherwise # UPnP exception otherwise
@ -295,7 +282,9 @@ def soco_filter_upnperror(errorcodes=None):
if err.error_code in errorcodes: if err.error_code in errorcodes:
pass pass
else: else:
raise _LOGGER.error("Error on %s with %s", funct.__name__, err)
except SoCoException as err:
_LOGGER.error("Error on %s with %s", funct.__name__, err)
finally: finally:
_SOCO_SERVICES_LOGGER.disabled = False _SOCO_SERVICES_LOGGER.disabled = False
@ -901,32 +890,32 @@ class SonosDevice(MediaPlayerDevice):
return supported return supported
@soco_error @soco_error()
def volume_up(self): def volume_up(self):
"""Volume up media player.""" """Volume up media player."""
self._player.volume += self.volume_increment self._player.volume += self.volume_increment
@soco_error @soco_error()
def volume_down(self): def volume_down(self):
"""Volume down media player.""" """Volume down media player."""
self._player.volume -= self.volume_increment self._player.volume -= self.volume_increment
@soco_error @soco_error()
def set_volume_level(self, volume): def set_volume_level(self, volume):
"""Set volume level, range 0..1.""" """Set volume level, range 0..1."""
self._player.volume = str(int(volume * 100)) self._player.volume = str(int(volume * 100))
@soco_error @soco_error()
def set_shuffle(self, shuffle): def set_shuffle(self, shuffle):
"""Enable/Disable shuffle mode.""" """Enable/Disable shuffle mode."""
self._player.play_mode = 'SHUFFLE' if shuffle else 'NORMAL' self._player.play_mode = 'SHUFFLE' if shuffle else 'NORMAL'
@soco_error @soco_error()
def mute_volume(self, mute): def mute_volume(self, mute):
"""Mute (true) or unmute (false) media player.""" """Mute (true) or unmute (false) media player."""
self._player.mute = mute self._player.mute = mute
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def select_source(self, source): def select_source(self, source):
"""Select input source.""" """Select input source."""
@ -1008,64 +997,61 @@ class SonosDevice(MediaPlayerDevice):
return self._source_name return self._source_name
@soco_error @soco_error()
def turn_off(self): def turn_off(self):
"""Turn off media player.""" """Turn off media player."""
if self._support_stop: if self._support_stop:
self.media_stop() self.media_stop()
@soco_error @soco_error(UPNP_ERRORS_TO_IGNORE)
@soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE)
@soco_coordinator @soco_coordinator
def media_play(self): def media_play(self):
"""Send play command.""" """Send play command."""
self._player.play() self._player.play()
@soco_error @soco_error(UPNP_ERRORS_TO_IGNORE)
@soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE)
@soco_coordinator @soco_coordinator
def media_stop(self): def media_stop(self):
"""Send stop command.""" """Send stop command."""
self._player.stop() self._player.stop()
@soco_error @soco_error(UPNP_ERRORS_TO_IGNORE)
@soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE)
@soco_coordinator @soco_coordinator
def media_pause(self): def media_pause(self):
"""Send pause command.""" """Send pause command."""
self._player.pause() self._player.pause()
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def media_next_track(self): def media_next_track(self):
"""Send next track command.""" """Send next track command."""
self._player.next() self._player.next()
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def media_previous_track(self): def media_previous_track(self):
"""Send next track command.""" """Send next track command."""
self._player.previous() self._player.previous()
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def media_seek(self, position): def media_seek(self, position):
"""Send seek command.""" """Send seek command."""
self._player.seek(str(datetime.timedelta(seconds=int(position)))) self._player.seek(str(datetime.timedelta(seconds=int(position))))
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def clear_playlist(self): def clear_playlist(self):
"""Clear players playlist.""" """Clear players playlist."""
self._player.clear_queue() self._player.clear_queue()
@soco_error @soco_error()
def turn_on(self): def turn_on(self):
"""Turn the media player on.""" """Turn the media player on."""
if self.support_play: if self.support_play:
self.media_play() self.media_play()
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def play_media(self, media_type, media_id, **kwargs): def play_media(self, media_type, media_id, **kwargs):
""" """
@ -1084,7 +1070,7 @@ class SonosDevice(MediaPlayerDevice):
else: else:
self._player.play_uri(media_id) self._player.play_uri(media_id)
@soco_error @soco_error()
def join(self, master): def join(self, master):
"""Join the player to a group.""" """Join the player to a group."""
coord = [device for device in self.hass.data[DATA_SONOS] coord = [device for device in self.hass.data[DATA_SONOS]
@ -1099,13 +1085,13 @@ class SonosDevice(MediaPlayerDevice):
else: else:
_LOGGER.error("Master not found %s", master) _LOGGER.error("Master not found %s", master)
@soco_error @soco_error()
def unjoin(self): def unjoin(self):
"""Unjoin the player from a group.""" """Unjoin the player from a group."""
self._player.unjoin() self._player.unjoin()
self._coordinator = None self._coordinator = None
@soco_error @soco_error()
def snapshot(self, with_group=True): def snapshot(self, with_group=True):
"""Snapshot the player.""" """Snapshot the player."""
from soco.snapshot import Snapshot from soco.snapshot import Snapshot
@ -1120,7 +1106,7 @@ class SonosDevice(MediaPlayerDevice):
else: else:
self._snapshot_group = None self._snapshot_group = None
@soco_error @soco_error()
def restore(self, with_group=True): def restore(self, with_group=True):
"""Restore snapshot for the player.""" """Restore snapshot for the player."""
from soco.exceptions import SoCoException from soco.exceptions import SoCoException
@ -1170,19 +1156,19 @@ class SonosDevice(MediaPlayerDevice):
if s_dev != old.coordinator: if s_dev != old.coordinator:
s_dev.join(old.coordinator) s_dev.join(old.coordinator)
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def set_sleep_timer(self, sleep_time): def set_sleep_timer(self, sleep_time):
"""Set the timer on the player.""" """Set the timer on the player."""
self._player.set_sleep_timer(sleep_time) self._player.set_sleep_timer(sleep_time)
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def clear_sleep_timer(self): def clear_sleep_timer(self):
"""Clear the timer on the player.""" """Clear the timer on the player."""
self._player.set_sleep_timer(None) self._player.set_sleep_timer(None)
@soco_error @soco_error()
@soco_coordinator @soco_coordinator
def update_alarm(self, **data): def update_alarm(self, **data):
"""Set the alarm clock on the player.""" """Set the alarm clock on the player."""
@ -1206,7 +1192,7 @@ class SonosDevice(MediaPlayerDevice):
a.include_linked_zones = data[ATTR_INCLUDE_LINKED_ZONES] a.include_linked_zones = data[ATTR_INCLUDE_LINKED_ZONES]
a.save() a.save()
@soco_error @soco_error()
def update_option(self, **data): def update_option(self, **data):
"""Modify playback options.""" """Modify playback options."""
if ATTR_NIGHT_SOUND in data and self.night_sound is not None: if ATTR_NIGHT_SOUND in data and self.night_sound is not None: