11"""Add support for discovering mDNS services."""
22from typing import List # noqa: F401
33
4- from zeroconf import Zeroconf , ServiceBrowser , ServiceInfo , DNSRecord
4+ from zeroconf import (
5+ Zeroconf ,
6+ ServiceBrowser ,
7+ ServiceInfo ,
8+ DNSRecord ,
9+ DNSPointer ,
10+ ServiceStateChange ,
11+ )
512
613
714class FastServiceBrowser (ServiceBrowser ):
815 """ServiceBrowser that does not process record updates."""
916
1017 def update_record (self , zc : Zeroconf , now : float , record : DNSRecord ) -> None :
11- """Ignore record updates as we only care about the cache anyways."""
12- return
18+ """Ignore record updates for non-ptrs."""
19+ if record .name not in self .types or not isinstance (record , DNSPointer ):
20+ return
21+ super ().update_record (zc , now , record )
1322
1423
1524class MDNS :
@@ -33,8 +42,19 @@ def start(self):
3342 self .zeroconf = Zeroconf ()
3443 self ._created_zeroconf = True
3544
36- def _service_update (* args , ** kwargs ):
37- return
45+ services_by_type = {}
46+
47+ for service in self .services :
48+ services_by_type .setdefault (service .typ , [])
49+ services_by_type [service .typ ].append (service )
50+
51+ def _service_update (zeroconf , service_type , name , state_change ):
52+ if state_change == ServiceStateChange .Added :
53+ for service in services_by_type [service_type ]:
54+ service .add_service (zeroconf , service_type , name )
55+ elif state_change == ServiceStateChange .Removed :
56+ for service in services_by_type [service_type ]:
57+ service .remove_service (zeroconf , service_type , name )
3858
3959 types = [service .typ for service in self .services ]
4060 self ._browser = FastServiceBrowser (
0 commit comments