Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_remove_playlist_bad_id(self, soco):
"""Test attempting to remove a Sonos playlist using a bad id."""
# junky bad
with pytest.raises(SoCoUPnPException):
soco.remove_sonos_playlist('SQ:-7')
# realistic non-existing
hpl_i = max([int(x.item_id.split(':')[1])
for x in soco.get_sonos_playlists()])
with pytest.raises(SoCoUPnPException):
soco.remove_sonos_playlist('SQ:{}'.format(hpl_i + 1))
def test_search_track_no_result(self, moco):
moco.contentDirectory.reset_mock()
# Browse returns an exception if the artist can't be found
# s:ClientUPnPError701
moco.contentDirectory.Browse.side_effect = SoCoUPnPException("No such object", "701", "error XML")
result = moco.search_track("artist")
assert len(result) == 0
moco.contentDirectory.Browse.assert_called_once_with([
('ObjectID', 'A:ALBUMARTIST/artist/'),
('BrowseFlag', 'BrowseDirectChildren'),
('Filter', '*'),
('StartingIndex', 0),
('RequestedCount', 100000),
('SortCriteria', '')
])
def test_search_track_no_result(self, moco):
moco.contentDirectory.reset_mock()
# Browse returns an exception if the artist can't be found
# s:ClientUPnPError701
moco.contentDirectory.Browse.side_effect = SoCoUPnPException(
"No such object", "701", "error XML")
result = moco.music_library.search_track("artist")
assert len(result) == 0
moco.contentDirectory.Browse.assert_called_once_with([
('ObjectID', 'A:ALBUMARTIST/artist/'),
('BrowseFlag', 'BrowseDirectChildren'),
('Filter', '*'),
('StartingIndex', 0),
('RequestedCount', 100000),
('SortCriteria', '')
])
# Note: A value of None for sleep_time_seconds is valid, and needs to
# be preserved distinctly separate from 0. 0 means go to sleep now,
# which will immediately start the sound tappering, and could be a
# useful feature, while None means cancel the current timer
try:
if sleep_time_seconds is None:
sleep_time = ''
else:
sleep_time = format(
datetime.timedelta(seconds=int(sleep_time_seconds))
)
self.avTransport.ConfigureSleepTimer([
('InstanceID', 0),
('NewSleepTimerDuration', sleep_time),
])
except SoCoUPnPException as err:
if 'Error 402 received' in str(err):
raise ValueError('invalid sleep_time_seconds, must be integer \
value between 0 and 86399 inclusive or None')
raise
except ValueError:
raise ValueError('invalid sleep_time_seconds, must be integer \
value between 0 and 86399 inclusive or None')
#
#
#
#
# All that matters for our purposes is the errorCode.
# errorDescription is not required, and Sonos does not seem to use it.
# NB need to encode unicode strings before passing to ElementTree
xml_error = xml_error.encode('utf-8')
error = XML.fromstring(xml_error)
log.debug("Error %s", xml_error)
error_code = error.findtext(
'.//{urn:schemas-upnp-org:control-1-0}errorCode')
if error_code is not None:
description = self.UPNP_ERRORS.get(int(error_code), '')
raise SoCoUPnPException(
message='UPnP Error {} received: {} from {}'.format(
error_code, description, self.soco.ip_address),
error_code=error_code,
error_description=description,
error_xml=xml_error
)
else:
# Unknown error, so just return the entire response
log.error("Unknown error received from %s", self.soco.ip_address)
raise UnknownSoCoException(xml_error)
def previous(self, sonos):
"""Play the previous track"""
try:
sonos.previous()
except SoCoUPnPException:
raise SoCoIllegalSeekException('No such track')
return self.get_current_track_info(sonos)
def __init__(self, message, error_code, error_xml, error_description=""):
"""
Args:
message (str): The message from the server.
error_code (str): The UPnP Error Code as a string.
error_xml (str): The xml containing the error, as a utf-8
encoded string.
error_description (str): A description of the error. Default is ""
"""
super(SoCoUPnPException, self).__init__()
self.message = message
self.error_code = error_code
self.error_description = error_description
self.error_xml = error_xml
def play_media(self, media_type, media_id, **kwargs):
"""
Send the play_media command to the media player.
If ATTR_MEDIA_ENQUEUE is True, add `media_id` to the queue.
"""
if self._coordinator:
self._coordinator.play_media(media_type, media_id, **kwargs)
else:
if kwargs.get(ATTR_MEDIA_ENQUEUE):
from soco.exceptions import SoCoUPnPException
try:
self._player.add_uri_to_queue(media_id)
except SoCoUPnPException:
_LOGGER.error('Error parsing media uri "%s", '
"please check it's a valid media resource "
'supported by Sonos', media_id)
else:
self._player.play_uri(media_id)
def play_next(sonos):
""" Play the next track """
try:
sonos.next()
except SoCoUPnPException:
raise SoCoIllegalSeekException('No such track')
return get_current_track_info(sonos)