soco.discovery module
This module contains methods for discovering Sonos devices on the network.
- soco.discovery.discover(timeout=5, include_invisible=False, interface_addr=None, allow_network_scan=False, **network_scan_kwargs)[source]
Discover Sonos zones on the local network.
Return a set of
SoCo
instances for each zone found. Include invisible zones (bridges and slave zones in stereo pairs ifinclude_invisible
isTrue
. Will block for up totimeout
seconds, after which returnNone
if no zones found.- Parameters
timeout (int, optional) – block for this many seconds, at most. Defaults to 5.
include_invisible (bool, optional) – include invisible zones in the return set. Defaults to
False
.interface_addr (str or None) – Discovery operates by sending UDP multicast datagrams.
interface_addr
is a string (dotted quad) representation of the network interface address to use as the source of the datagrams (i.e., it is a value forsocket.IP_MULTICAST_IF
). IfNone
or not specified, the system default interface(s) for UDP multicast messages will be used. This is probably what you want to happen. Defaults toNone
.allow_network_scan (bool, optional) – If normal discovery fails, fall back to a scan of the attached network(s) to detect Sonos devices.
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details.
- Returns
a set of
SoCo
instances, one for each zone found, or elseNone
.- Return type
- soco.discovery.any_soco(allow_network_scan=False, **network_scan_kwargs)[source]
Return any visible soco device, for when it doesn’t matter which.
Try to obtain an existing instance, or use
discover
if necessary. Note that this assumes that the existing instance has not left the network.- Parameters
allow_network_scan (bool, optional) – If normal discovery fails, fall back to a scan of the attached network(s) to detect Sonos devices.
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details.
- Returns
A
SoCo
instance (or subclass ifconfig.SOCO_CLASS
is set), orNone
if no instances are found.- Return type
- soco.discovery.by_name(name, allow_network_scan=False, **network_scan_kwargs)[source]
Return a device by name.
- Parameters
name (str) – The name of the device to return.
allow_network_scan (bool, optional) – If normal discovery fails, fall back to a scan of the attached network(s) to detect Sonos devices.
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details.
- Returns
A
SoCo
instance (or subclass ifconfig.SOCO_CLASS
is set), orNone
if no instances are found.- Return type
- soco.discovery.scan_network(include_invisible=False, multi_household=False, max_threads=256, scan_timeout=0.1, min_netmask=24, networks_to_scan=None)[source]
Scan all attached networks for Sonos devices.
This function scans the IPv4 networks to which this node is attached, searching for Sonos devices. Multiple parallel threads are used to scan IP addresses in parallel for faster discovery.
Public, loopback and link local IP ranges are excluded from the scan, and the scope of the search can be controlled by setting a minimum netmask.
Alternatively, a list of networks to scan can be provided.
This function is intended for use when the usual discovery function is not working, perhaps due to multicast problems on the network to which the SoCo host is attached. The function can also be used to find a complete list of speakers when there are multiple Sonos households present. For example, this is the case where there are ‘split’ S1/S2 Sonos systems on the network.
Note that this call may fail to find speakers present on the network, and this can be due to ARP cache misses and ARP requests that don’t complete within the timeout. The call can be retried with longer values for scan_timeout if necessary.
- Parameters
include_invisible (bool, optional) – Whether to include invisible Sonos devices in the set of devices returned.
multi_household (bool, optional) – Whether to find all the speakers on the network exhaustively. If set to
False
, discovery will stop as soon as at least one speaker is found. In the case of multiple households on the attached networks, this means that only the speakers from the first-discovered household will be returned. If set toTrue
, discovery will proceed until all speakers, from all households, have been found.max_threads (int, optional) – The maximum number of threads to use when scanning the network.
scan_timeout (float, optional) – The network timeout in seconds to use when checking each IP address for a Sonos device.
min_netmask (int, optional) – The minimum number of netmask bits. Used to constrain the network search space.
networks_to_scan (list, optional) – A
list
of IPv4 networks to search, each astr
of form “192.168.0.1/24”. Only the specified networks will be searched. The ‘min_netmask’ option (if supplied) is ignored.
- Returns
A set of
SoCo
instances, one for each zone found, or elseNone
.- Return type
- soco.discovery.scan_network_by_household_id(household_id, include_invisible=False, **network_scan_kwargs)[source]
Convenience function to find the zones in a specific Sonos household.
- Parameters
household_id (str) – The Sonos household ID to search for. IDs take the form ‘Sonos_XXXXXXXXXXXXXXXXXXXXXXXXXX’.
include_invisible (bool, optional) – Whether to include invisible Sonos devices in the set of devices returned.
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details. (Note that the argument ‘multi_household’ is forced toTrue
when this function is called.)
- Returns
A set of
SoCo
instances, one for each zone found, or elseNone
.- Return type
- soco.discovery.scan_network_get_household_ids(**network_scan_kwargs)[source]
Convenience function to find the all Sonos households on the attached networks.
- Parameters
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details. (Note that the argument ‘multi_household’ is forced toTrue
when this function is called.)- Returns
A set of Sonos household IDs, each in the form of a
str
like ‘Sonos_XXXXXXXXXXXXXXXXXXXXXXXXXX’.- Return type
- soco.discovery.scan_network_get_by_name(name, household_id=None, **network_scan_kwargs)[source]
Convenience function to use
scan_network
to find a zone by its name.Note that if there are multiple zones with the same name, then only one of the zones will be returned. Optionally, the search can be constrained to a specific household.
- Parameters
name (str) – The name of the zone to find.
household_id (str, optional) – Use this to find the zone in a specific Sonos household.
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details. (Note that the argument ‘multi_household’ is forced toTrue
when this function is called.)
- Returns
A
SoCo
instance representing the zone, orNone
if no matching zone is found. Only returns visible zones.- Return type
- soco.discovery.scan_network_any_soco(household_id=None, **network_scan_kwargs)[source]
Convenience function to use
scan_network
to find any zone, optionally specifying a Sonos household.- Parameters
household_id (str, optional) – Use this to find a zone in a specific Sonos household.
**network_scan_kwargs – Arguments for the
scan_network
function. See its docstring for details.
- Returns
A
SoCo
instance representing the zone, orNone
if no zone is found (or no zone is found that matches a supplied household_id).- Return type