Skip to content

Commit

Permalink
Add ability to pass in a zeroconf instance to discovery (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 13, 2020
1 parent 2f0be29 commit 9493cb1
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pychromecast/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_value(key):
self.add_callback(name)


def start_discovery(add_callback=None, remove_callback=None):
def start_discovery(add_callback=None, remove_callback=None, zeroconf_instance=None):
"""
Start discovering chromecasts on the network.
Expand All @@ -97,12 +97,17 @@ def start_discovery(add_callback=None, remove_callback=None):
object. The CastListener object will contain information for the discovered
chromecasts. To stop discovery, call the stop_discovery method with the
ServiceBrowser object.
A shared zeroconf instance can be passed as zeroconf_instance. If no
instance is passed, a new instance will be created.
"""
listener = CastListener(add_callback, remove_callback)
service_browser = False
try:
service_browser = zeroconf.ServiceBrowser(
zeroconf.Zeroconf(), "_googlecast._tcp.local.", listener
zeroconf_instance or zeroconf.Zeroconf(),
"_googlecast._tcp.local.",
listener,
)
except (
zeroconf.BadTypeInNameException,
Expand All @@ -118,6 +123,7 @@ def start_discovery(add_callback=None, remove_callback=None):

def stop_discovery(browser):
"""Stop the chromecast discovery thread."""
browser.cancel()
browser.zc.close()


Expand Down

0 comments on commit 9493cb1

Please sign in to comment.