Skip to content

Commit

Permalink
Issue CharlesBlonde#27: Calling browser.cancel()
Browse files Browse the repository at this point in the history
Allow zeroconf as external instance
  • Loading branch information
chwiede committed Mar 18, 2018
1 parent 25163a7 commit 016cb5c
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libsoundtouch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def soundtouch_device(host, port=8090):
return s_device


def discover_devices(timeout=5):
def discover_devices(timeout=5, zeroconf=None):
"""Discover devices on the local network.
:param timeout: Max time to wait in seconds. Default 5
:param zeroconf: Global singleton zeroconf instance. Default None
"""
devices = []
# Using Queue as a timeout timer
Expand All @@ -38,12 +39,17 @@ def add_device_function(name, host, port):
_LOGGER.info("%s discovered (host: %s, port: %i)", name, host, port)
devices.append(soundtouch_device(host, port))

zeroconf = Zeroconf()
if zeroconf is None:
zeroconf = Zeroconf()

listener = SoundtouchDeviceListener(add_device_function)
_LOGGER.debug("Starting discovery...")
ServiceBrowser(zeroconf, "_soundtouch._tcp.local.", listener)
browser = ServiceBrowser(zeroconf, "_soundtouch._tcp.local.", listener)
try:
add_devices_queue.get(timeout=timeout)
except Empty:
_LOGGER.debug("End of discovery...")
finally:
browser.cancel()

return devices

0 comments on commit 016cb5c

Please sign in to comment.