The soco module¶
SoCo (Sonos Controller) is a simple library to control Sonos speakers
- soco.discover(timeout=1, include_invisible=False)¶
Discover Sonos zones on the local network.
Return an set of visible SoCo instances for each zone found. Include invisible zones (bridges and slave zones in stereo pairs if include_invisible is True. Will block for up to timeout seconds, after which return None if no zones found.
- class soco.SonosDiscovery¶
Retained for backward compatibility only. Will be removed in future releases
Deprecated since version 0.7: Use discover() instead.
- static get_speaker_ips()¶
Deprecated in favour of discover()
- class soco.SoCo(ip_address)¶
A simple class for controlling a Sonos speaker.
For any given set of arguments to __init__, only one instance of this class may be created. Subsequent attempts to create an instance with the same arguments will return the previously created instance. This means that all SoCo instances created with the same ip address are in fact the same SoCo instance, reflecting the real world position.
Public functions:
play -- Plays the current item. play_uri -- Plays a track or a music stream by URI. play_from_queue -- Plays an item in the queue. pause -- Pause the currently playing track. stop -- Stop the currently playing track. seek -- Move the currently playing track a given elapsed time. next -- Go to the next track. previous -- Go back to the previous track. switch_to_line_in -- Switch the speaker's input to line-in. switch_to_tv -- Switch the speaker's input to TV. get_current_track_info -- Get information about the currently playing track. get_speaker_info -- Get information about the Sonos speaker. partymode -- Put all the speakers in the network in the same group. join -- Join this speaker to another "master" speaker. unjoin -- Remove this speaker from a group. get_queue -- Get information about the queue. get_artists -- Get artists from the music library get_album_artists -- Get album artists from the music library get_albums -- Get albums from the music library get_genres -- Get genres from the music library get_composers -- Get composers from the music library get_tracks -- Get tracks from the music library get_playlists -- Get playlists from the music library get_music_library_information -- Get information from the music library get_current_transport_info -- get speakers playing state browse_by_idstring -- Browse (get sub-elements) a given type add_uri_to_queue -- Adds an URI to the queue add_to_queue -- Add a track to the end of the queue remove_from_queue -- Remove a track from the queue clear_queue -- Remove all tracks from queue get_favorite_radio_shows -- Get favorite radio shows from Sonos' Radio app. get_favorite_radio_stations -- Get favorite radio stations. create_sonos_playlist -- Creates a new Sonos' playlist add_item_to_sonos_playlist -- Adds a queueable item to a Sonos' playlist
Properties:
uid -- The speaker's unique identifier mute -- The speaker's mute status. volume -- The speaker's volume. bass -- The speaker's bass EQ. treble -- The speaker's treble EQ. loudness -- The status of the speaker's loudness compensation. cross_fade -- The status of the speaker's crossfade. status_light -- The state of the Sonos status light. player_name -- The speaker's name. play_mode -- The queue's repeat/shuffle settings.
Warning
These properties are not cached and will obtain information over the network, so may take longer than expected to set or return a value. It may be a good idea for you to cache the value in your own code.
- add_item_to_sonos_playlist(queueable_item, sonos_playlist)¶
Adds a queueable item to a Sonos’ playlist :param queueable_item: the item to add to the Sonos’ playlist :param sonos_playlist: the Sonos’ playlist to which the item should
be added
- add_to_queue(queueable_item)¶
Adds a queueable item to the queue
- add_uri_to_queue(uri)¶
Adds the URI to the queue
Parameters: uri (str) – The URI to be added to the queue
- all_groups¶
Return a set of all the available groups
- all_zones¶
Return a set of all the available zones
- bass¶
The speaker’s bass EQ. An integer between -10 and 10.
- browse(ml_item=None, start=0, max_items=100, full_album_art_uri=False)¶
Browse (get sub-elements) a music library item
- Keyword arguments:
- ml_item (MusicLibraryItem): The MusicLibraryItem to browse, if left
- out or passed None, the items at the base level will be returned
start (int): The starting index of the results max_items (int): The maximum number of items to return full_album_art_uri(bool): If the album art URI should include the
IP address- Returns:
- dict: A SearchResult object
- Raises:
AttributeError: If ml_item has no item_id attribute SoCoUPnPException: With error_code='701' if the item cannot be
browsed
- browse_by_idstring(search_type, idstring, start=0, max_items=100, full_album_art_uri=False)¶
Browse (get sub-elements) a given type
Parameters: - search_type – The kind of information to retrieve. Can be one of: ‘artists’, ‘album_artists’, ‘albums’, ‘genres’, ‘composers’, ‘tracks’, ‘share’, ‘sonos_playlists’, and ‘playlists’, where playlists are the imported file based playlists from the music library
- idstring – String ID to search for
- start – Starting number of returned matches
- max_items – Maximum number of returned matches. NOTE: The maximum may be restricted by the unit, presumably due to transfer size consideration, so check the returned number against the requested.
- full_album_art_uri – If the album art URI should include the IP address
Returns: A dictionary with metadata for the search, with the keys ‘number_returned’, ‘update_id’, ‘total_matches’ and an ‘item_list’ list with the search results.
- clear_queue()¶
Removes all tracks from the queue.
Returns: True if the Sonos speaker cleared the queue.
Raises SoCoException (or a subclass) upon errors.
- create_sonos_playlist(title)¶
Create a new Sonos’ playlist .
Params title: Name of the playlist Returns: An instance of MLSonosPlaylist
- cross_fade¶
The speaker’s cross fade state. True if enabled, False otherwise
- get_album_artists(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’album_artists’. For details on remaining arguments refer to the docstring for that method.
- get_albums(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’albums’. For details on remaining arguments refer to the docstring for that method.
- get_artists(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’artists’. For details on remaining arguments refer to the docstring for that method.
- get_composers(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’composers’. For details on remaining arguments refer to the docstring for that method.
- get_current_track_info()¶
Get information about the currently playing track.
Returns: A dictionary containing the following information about the currently playing track: playlist_position, duration, title, artist, album, position and a link to the album art.
If we’re unable to return data for a field, we’ll return an empty string. This can happen for all kinds of reasons so be sure to check values. For example, a track may not have complete metadata and be missing an album name. In this case track[‘album’] will be an empty string.
- get_current_transport_info()¶
Get the current playback state
Returns: A dictionary containing the following information about the speakers playing state current_transport_state (PLAYING, PAUSED_PLAYBACK, STOPPED), current_trasnport_status (OK, ?), current_speed(1,?)
This allows us to know if speaker is playing or not. Don’t know other states of CurrentTransportStatus and CurrentSpeed.
- get_favorite_radio_shows(start=0, max_items=100)¶
Get favorite radio shows from Sonos’ Radio app.
Returns: A list containing the total number of favorites, the number of favorites returned, and the actual list of favorite radio shows, represented as a dictionary with title and uri keys.
Depending on what you’re building, you’ll want to check to see if the total number of favorites is greater than the amount you requested (max_items), if it is, use start to page through and get the entire list of favorites.
- get_favorite_radio_stations(start=0, max_items=100)¶
Get favorite radio stations from Sonos’ Radio app.
Returns: A list containing the total number of favorites, the number of favorites returned, and the actual list of favorite radio stations, represented as a dictionary with title and uri keys.
Depending on what you’re building, you’ll want to check to see if the total number of favorites is greater than the amount you requested (max_items), if it is, use start to page through and get the entire list of favorites.
- get_genres(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’genres’. For details on remaining arguments refer to the docstring for that method.
- get_group_coordinator(zone_name)¶
Deprecated since version 0.8.
Use group() or all_groups() instead.
- get_music_library_information(search_type, start=0, max_items=100, full_album_art_uri=False)¶
Retrieve information about the music library
Parameters: - search_type – The kind of information to retrieve. Can be one of: ‘artists’, ‘album_artists’, ‘albums’, ‘genres’, ‘composers’, ‘tracks’, ‘share’, ‘sonos_playlists’, and ‘playlists’, where playlists are the imported file based playlists from the music library
- start – Starting number of returned matches
- max_items – Maximum number of returned matches. NOTE: The maximum may be restricted by the unit, presumably due to transfer size consideration, so check the returned number against the requested.
- full_album_art_uri – If the album art URI should include the IP address
Returns: A SearchResult object
Raises: SoCoException upon errors
NOTE: The playlists that are returned with the ‘playlists’ search, are the playlists imported from (files in) the music library, they are not the Sonos playlists.
The information about the which searches can be performed and the form of the query has been gathered from the Janos project: http://sourceforge.net/projects/janos/ Props to the authors of that project.
- get_playlists(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’playlists’. For details on remaining arguments refer to the docstring for that method.
NOTE: The playlists that are referred to here are the playlist (files) imported from the music library, they are not the Sonos playlists.
- get_queue(start=0, max_items=100, full_album_art_uri=False)¶
Get information about the queue
Parameters: - start – Starting number of returned matches
- max_items – Maximum number of returned matches
- full_album_art_uri – If the album art URI should include the IP address
Returns: A Queue object
This method is heavly based on Sam Soffes (aka soffes) ruby implementation
- get_sonos_playlists(start=0, max_items=100, full_album_art_uri=False)¶
Convenience method for: get_music_library_information(‘sonos_playlists’) Refer to the docstring for that method
- get_speaker_info(refresh=False)¶
Get information about the Sonos speaker.
Arguments: refresh – Refresh the speaker info cache.
Returns: Information about the Sonos speaker, such as the UID, MAC Address, and Zone Name.
- get_speakers_ip(refresh=False)¶
Get the IP addresses of all the Sonos speakers in the network.
Arguments: refresh – Refresh the speakers IP cache. Ignored. For backward
compatibility onlyReturns: a set of IP addresses of the Sonos speakers.
Deprecated since version 0.8.
- get_tracks(start=0, max_items=100, full_album_art_uri=False)¶
Convinience method for get_music_library_information() with search_type=’tracks’. For details on remaining arguments refer to the docstring for that method.
- group¶
The Zone Group of which this device is a member.
group will be None if this zone is a slave in a stereo pair.
- is_bridge¶
Is this zone a bridge?
- is_coordinator¶
Return True if this zone is a group coordinator, otherwise False.
return True or False
- is_visible¶
Is this zone visible? A zone might be invisible if, for example it is a bridge, or the slave part of stereo pair.
return True or False
- join(master)¶
Join this speaker to another “master” speaker.
Note
The signature of this method has changed in 0.8. It now requires a SoCo instance to be passed as master, not an IP address
- loudness¶
The Sonos speaker’s loudness compensation. True if on, otherwise False.
Loudness is a complicated topic. You can find a nice summary about this feature here: http://forums.sonos.com/showthread.php?p=4698#post4698
- mute¶
The speaker’s mute state. True if muted, False otherwise
- next()¶
Go to the next track.
Returns: True if the Sonos speaker successfully skipped to the next track.
Raises SoCoException (or a subclass) upon errors.
Keep in mind that next() can return errors for a variety of reasons. For example, if the Sonos is streaming Pandora and you call next() several times in quick succession an error code will likely be returned (since Pandora has limits on how many songs can be skipped).
- partymode()¶
Put all the speakers in the network in the same group, a.k.a Party Mode.
This blog shows the initial research responsible for this: http://blog.travelmarx.com/2010/06/exploring-sonos-via-upnp.html
The trick seems to be (only tested on a two-speaker setup) to tell each speaker which to join. There’s probably a bit more to it if multiple groups have been defined.
- pause()¶
Pause the currently playing track.
Returns: True if the Sonos speaker successfully paused the track.
Raises SoCoException (or a subclass) upon errors.
- play()¶
Play the currently selected track.
Returns: True if the Sonos speaker successfully started playing the track.
Raises SoCoException (or a subclass) upon errors.
- play_from_queue(index)¶
Play a track from the queue by index. The index number is required as an argument, where the first index is 0.
index: the index of the track to play; first item in the queue is 0
Returns: True if the Sonos speaker successfully started playing the track.
Raises SoCoException (or a subclass) upon errors.
- play_mode¶
The queue’s play mode. Case-insensitive options are:
NORMAL – Turns off shuffle and repeat. REPEAT_ALL – Turns on repeat and turns off shuffle. SHUFFLE – Turns on shuffle and repeat. (It’s strange, I know.) SHUFFLE_NOREPEAT – Turns on shuffle and turns off repeat.
- play_uri(uri=u'', meta=u'')¶
Play a given stream. Pauses the queue.
Arguments: uri – URI of a stream to be played. meta — The track metadata to show in the player, DIDL format.
Returns: True if the Sonos speaker successfully started playing the track.
Raises SoCoException (or a subclass) upon errors.
- player_name¶
The speaker’s name. A string.
- previous()¶
Go back to the previously played track.
Returns: True if the Sonos speaker successfully went to the previous track.
Raises SoCoException (or a subclass) upon errors.
Keep in mind that previous() can return errors for a variety of reasons. For example, previous() will return an error code (error code 701) if the Sonos is streaming Pandora since you can’t go back on tracks.
- remove_from_queue(index)¶
Remove a track from the queue by index. The index number is required as an argument, where the first index is 0.
index: the index of the track to remove; first item in the queue is 0
- Returns:
- True if the Sonos speaker successfully removed the track
Raises SoCoException (or a subclass) upon errors.
- seek(timestamp)¶
Seeks to a given timestamp in the current track, specified in the format of HH:MM:SS or H:MM:SS.
Returns: True if the Sonos speaker successfully seeked to the timecode.
Raises SoCoException (or a subclass) upon errors.
- speaker_ip¶
Retained for backward compatibility only. Will be removed in future releases
Deprecated since version 0.7: Use ip_address instead.
- status_light¶
The white Sonos status light between the mute button and the volume up button on the speaker. True if on, otherwise False.
- stop()¶
Stop the currently playing track.
Returns: True if the Sonos speaker successfully stopped the playing track.
Raises SoCoException (or a subclass) upon errors.
- switch_to_line_in()¶
Switch the speaker’s input to line-in.
Returns: True if the Sonos speaker successfully switched to line-in.
If an error occurs, we’ll attempt to parse the error and return a UPnP error code. If that fails, the raw response sent back from the Sonos speaker will be returned.
Raises SoCoException (or a subclass) upon errors.
- switch_to_tv()¶
Switch the speaker’s input to TV.
Returns: True if the Sonos speaker successfully switched to TV.
If an error occurs, we’ll attempt to parse the error and return a UPnP error code. If that fails, the raw response sent back from the Sonos speaker will be returned.
Raises SoCoException (or a subclass) upon errors.
- treble¶
The speaker’s treble EQ. An integer between -10 and 10.
- uid¶
A unique identifier. Looks like: RINCON_000XXXXXXXXXX1400
- unjoin()¶
Remove this speaker from a group.
Seems to work ok even if you remove what was previously the group master from it’s own group. If the speaker was not in a group also returns ok.
Returns: True if this speaker has left the group.
Raises SoCoException (or a subclass) upon errors.
- visible_zones¶
Return an set of all visible zones
- volume¶
The speaker’s volume. An integer between 0 and 100.
- exception soco.SoCoException¶
base exception raised by SoCo, containing the UPnP error code
- exception soco.UnknownSoCoException¶
raised if reason of the error can not be extracted
The exception object will contain the raw response sent back from the speaker