spotifywebapipython.spotifydiscovery

@export
class SpotifyDiscovery:

This class contains methods used to dicover Spotify Connect devices on a local network. The ZeroConf (aka MDNS, etc) service is used to detect devices, and adds them to a device list as they are discovered.

Click the Sample Code links in the individual methods for sample code examples.

SpotifyDiscovery( zeroconfClient: zeroconf._core.Zeroconf = None, printToConsole: bool = False)

Initializes a new instance of the class.

Arguments:
  • zeroconfClient (Zeroconf) A Zeroconf client instance that will be used to discover Spotify Connect devices, or null to create a new instance of Zeroconf. Default is null.
  • printToConsole (bool): True to print discovered device information to the console as the devices are discovered; otherwise, False to not print anything to the console. Default is False.
def ContainsId(self, value: str) -> bool:

Returns True if the DiscoveryResults collection contains the specified id value; otherwise, False.

DiscoveredDeviceNames: dict

A dictionary of discovered device names that were detected by the discovery process.

Dictionary keys will be in the form of "'name' (address:port)", where "name" is the device name, "address" is the device server address and the "port" is the ipv4 port number the Spotify Connect device is listening on.

Dictionary values will be the device names (e.g. "Web Player (Chrome)", etc.). This SHOULD match the name of the device as displayed in the Spotify App, but is not guaranteed.

DiscoveryResults: list[spotifywebapipython.models.zeroconfdiscoveryresult.ZeroconfDiscoveryResult]

An array of ZeroconfDiscoveryResult items that contain discovery details for each service that was discovered.

def GetIndexByKey(self, value: str) -> int:

Returns the index of the DiscoveryResults collection that contains the specified device key value; otherwise, -1.

def GetResultById(self, value: str) -> bool:

Returns a ZeroconfDiscoveryResult instance if the DiscoveryResults collection contains the specified device id value; otherwise, None.

ZeroconfClient: zeroconf._core.Zeroconf

Zeroconf client instance that will be used to discover Spotify Connect devices.

def DiscoverDevices(self, timeout: float = 2) -> dict:

Discover Spotify Connect devices on the local network via the ZeroConf (aka MDNS) service.

Arguments:
  • timeout (float): Maximum amount of time to wait (in seconds) for the discovery to complete.
    Default is 2 seconds.
Returns:

A dictionary of ZeroconfDiscoveryResult objects.

Sample Code

from spotifywebapipython import *

try:

    print("Test Starting\n")

    # create a new instance of the discovery class.
    # we will print device details to the console as they are discovered.
    discovery:SpotifyDiscovery = SpotifyDiscovery(printToConsole=True)

    # discover Spotify Connect devices on the network, waiting up to 
    # 2 seconds for all devices to be discovered.
    discovery.DiscoverDevices(timeout=2)

    # print all discovered devices.
    print("\n%s" % (discovery.ToString(True)))

except Exception as ex:

    print(str(ex))
    raise

finally:

    print("\nTests Completed")

def ToString(self, includeItems: bool = False) -> str:

Returns a displayable string representation of the class.

Arguments:
  • includeItems (bool): True to include all items in the list; otherwise False to only include the base list.