mirror of
https://github.com/home-assistant/core.git
synced 2025-06-02 20:27:14 +00:00
small sonos cleanup (#11607)
This commit is contained in:
parent
5e81736f88
commit
b2b836d4c1
@ -263,27 +263,14 @@ def _get_entity_from_soco(hass, soco):
|
||||
raise ValueError("No entity for SoCo device")
|
||||
|
||||
|
||||
def soco_error(funct):
|
||||
"""Catch soco 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 soco_error(errorcodes=None):
|
||||
"""Filter out specified UPnP errors from logs and avoid exceptions."""
|
||||
def decorator(funct):
|
||||
"""Decorator function."""
|
||||
@ft.wraps(funct)
|
||||
def wrapper(*args, **kwargs):
|
||||
"""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
|
||||
# UPnP exception otherwise
|
||||
@ -295,7 +282,9 @@ def soco_filter_upnperror(errorcodes=None):
|
||||
if err.error_code in errorcodes:
|
||||
pass
|
||||
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:
|
||||
_SOCO_SERVICES_LOGGER.disabled = False
|
||||
|
||||
@ -901,32 +890,32 @@ class SonosDevice(MediaPlayerDevice):
|
||||
|
||||
return supported
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def volume_up(self):
|
||||
"""Volume up media player."""
|
||||
self._player.volume += self.volume_increment
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def volume_down(self):
|
||||
"""Volume down media player."""
|
||||
self._player.volume -= self.volume_increment
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def set_volume_level(self, volume):
|
||||
"""Set volume level, range 0..1."""
|
||||
self._player.volume = str(int(volume * 100))
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def set_shuffle(self, shuffle):
|
||||
"""Enable/Disable shuffle mode."""
|
||||
self._player.play_mode = 'SHUFFLE' if shuffle else 'NORMAL'
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def mute_volume(self, mute):
|
||||
"""Mute (true) or unmute (false) media player."""
|
||||
self._player.mute = mute
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def select_source(self, source):
|
||||
"""Select input source."""
|
||||
@ -1008,64 +997,61 @@ class SonosDevice(MediaPlayerDevice):
|
||||
|
||||
return self._source_name
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def turn_off(self):
|
||||
"""Turn off media player."""
|
||||
if self._support_stop:
|
||||
self.media_stop()
|
||||
|
||||
@soco_error
|
||||
@soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE)
|
||||
@soco_error(UPNP_ERRORS_TO_IGNORE)
|
||||
@soco_coordinator
|
||||
def media_play(self):
|
||||
"""Send play command."""
|
||||
self._player.play()
|
||||
|
||||
@soco_error
|
||||
@soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE)
|
||||
@soco_error(UPNP_ERRORS_TO_IGNORE)
|
||||
@soco_coordinator
|
||||
def media_stop(self):
|
||||
"""Send stop command."""
|
||||
self._player.stop()
|
||||
|
||||
@soco_error
|
||||
@soco_filter_upnperror(UPNP_ERRORS_TO_IGNORE)
|
||||
@soco_error(UPNP_ERRORS_TO_IGNORE)
|
||||
@soco_coordinator
|
||||
def media_pause(self):
|
||||
"""Send pause command."""
|
||||
self._player.pause()
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def media_next_track(self):
|
||||
"""Send next track command."""
|
||||
self._player.next()
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def media_previous_track(self):
|
||||
"""Send next track command."""
|
||||
self._player.previous()
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def media_seek(self, position):
|
||||
"""Send seek command."""
|
||||
self._player.seek(str(datetime.timedelta(seconds=int(position))))
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def clear_playlist(self):
|
||||
"""Clear players playlist."""
|
||||
self._player.clear_queue()
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def turn_on(self):
|
||||
"""Turn the media player on."""
|
||||
if self.support_play:
|
||||
self.media_play()
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def play_media(self, media_type, media_id, **kwargs):
|
||||
"""
|
||||
@ -1084,7 +1070,7 @@ class SonosDevice(MediaPlayerDevice):
|
||||
else:
|
||||
self._player.play_uri(media_id)
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def join(self, master):
|
||||
"""Join the player to a group."""
|
||||
coord = [device for device in self.hass.data[DATA_SONOS]
|
||||
@ -1099,13 +1085,13 @@ class SonosDevice(MediaPlayerDevice):
|
||||
else:
|
||||
_LOGGER.error("Master not found %s", master)
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def unjoin(self):
|
||||
"""Unjoin the player from a group."""
|
||||
self._player.unjoin()
|
||||
self._coordinator = None
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def snapshot(self, with_group=True):
|
||||
"""Snapshot the player."""
|
||||
from soco.snapshot import Snapshot
|
||||
@ -1120,7 +1106,7 @@ class SonosDevice(MediaPlayerDevice):
|
||||
else:
|
||||
self._snapshot_group = None
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def restore(self, with_group=True):
|
||||
"""Restore snapshot for the player."""
|
||||
from soco.exceptions import SoCoException
|
||||
@ -1170,19 +1156,19 @@ class SonosDevice(MediaPlayerDevice):
|
||||
if s_dev != old.coordinator:
|
||||
s_dev.join(old.coordinator)
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def set_sleep_timer(self, sleep_time):
|
||||
"""Set the timer on the player."""
|
||||
self._player.set_sleep_timer(sleep_time)
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def clear_sleep_timer(self):
|
||||
"""Clear the timer on the player."""
|
||||
self._player.set_sleep_timer(None)
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
@soco_coordinator
|
||||
def update_alarm(self, **data):
|
||||
"""Set the alarm clock on the player."""
|
||||
@ -1206,7 +1192,7 @@ class SonosDevice(MediaPlayerDevice):
|
||||
a.include_linked_zones = data[ATTR_INCLUDE_LINKED_ZONES]
|
||||
a.save()
|
||||
|
||||
@soco_error
|
||||
@soco_error()
|
||||
def update_option(self, **data):
|
||||
"""Modify playback options."""
|
||||
if ATTR_NIGHT_SOUND in data and self.night_sound is not None:
|
||||
|
Loading…
x
Reference in New Issue
Block a user