1818 AsyncZeroconfServiceTypes ,
1919)
2020
21+ _PENDING_TASKS : set [asyncio .Task ] = set ()
22+
2123
2224def async_on_service_state_change (
2325 zeroconf : Zeroconf , service_type : str , name : str , state_change : ServiceStateChange
2426) -> None :
2527 print (f"Service { name } of type { service_type } state changed: { state_change } " )
2628 if state_change is not ServiceStateChange .Added :
2729 return
28- asyncio .ensure_future (async_display_service_info (zeroconf , service_type , name ))
30+ task = asyncio .ensure_future (async_display_service_info (zeroconf , service_type , name ))
31+ _PENDING_TASKS .add (task )
32+ task .add_done_callback (_PENDING_TASKS .discard )
2933
3034
31- async def async_display_service_info (
32- zeroconf : Zeroconf , service_type : str , name : str
33- ) -> None :
35+ async def async_display_service_info (zeroconf : Zeroconf , service_type : str , name : str ) -> None :
3436 info = AsyncServiceInfo (service_type , name )
3537 await info .async_request (zeroconf , 3000 )
3638 print ("Info from zeroconf.get_service_info: %r" % (info ))
3739 if info :
38- addresses = [
39- "%s:%d" % (addr , cast (int , info .port ))
40- for addr in info .parsed_scoped_addresses ()
41- ]
40+ addresses = ["%s:%d" % (addr , cast (int , info .port )) for addr in info .parsed_scoped_addresses ()]
4241 print (" Name: %s" % name )
4342 print (" Addresses: %s" % ", " .join (addresses ))
4443 print (" Weight: %d, priority: %d" % (info .weight , info .priority ))
@@ -66,9 +65,7 @@ async def async_run(self) -> None:
6665 services = ["_http._tcp.local." , "_hap._tcp.local." ]
6766 if self .args .find :
6867 services = list (
69- await AsyncZeroconfServiceTypes .async_find (
70- aiozc = self .aiozc , ip_version = ip_version
71- )
68+ await AsyncZeroconfServiceTypes .async_find (aiozc = self .aiozc , ip_version = ip_version )
7269 )
7370
7471 print ("\n Browsing %s service(s), press Ctrl-C to exit...\n " % services )
@@ -90,9 +87,7 @@ async def async_close(self) -> None:
9087
9188 parser = argparse .ArgumentParser ()
9289 parser .add_argument ("--debug" , action = "store_true" )
93- parser .add_argument (
94- "--find" , action = "store_true" , help = "Browse all available services"
95- )
90+ parser .add_argument ("--find" , action = "store_true" , help = "Browse all available services" )
9691 version_group = parser .add_mutually_exclusive_group ()
9792 version_group .add_argument ("--v6" , action = "store_true" )
9893 version_group .add_argument ("--v6-only" , action = "store_true" )
0 commit comments