1
1
"""Add support for discovering mDNS services."""
2
2
from typing import List # noqa: F401
3
3
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
+ )
5
12
6
13
7
14
class FastServiceBrowser (ServiceBrowser ):
8
15
"""ServiceBrowser that does not process record updates."""
9
16
10
17
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 )
13
22
14
23
15
24
class MDNS :
@@ -33,8 +42,19 @@ def start(self):
33
42
self .zeroconf = Zeroconf ()
34
43
self ._created_zeroconf = True
35
44
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 )
38
58
39
59
types = [service .typ for service in self .services ]
40
60
self ._browser = FastServiceBrowser (
0 commit comments