Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit d3c964b

Browse files
authored
Fix mdns discovery (#253)
I sent the wrong squash on the first go
1 parent 5fc8162 commit d3c964b

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

netdisco/mdns.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
"""Add support for discovering mDNS services."""
22
from 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

714
class 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

1524
class 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

Comments
 (0)