Skip to content

Commit

Permalink
Fixes TXT record not available when making direct query with name and…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
raul-marquez-csa committed Feb 7, 2024
1 parent e0df481 commit 903238b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/python_testing/mdns_discovery/mdns_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from enum import Enum
from typing import Dict, List, Optional

from zeroconf import IPVersion, ServiceStateChange, Zeroconf
from zeroconf import IPVersion, ServiceListener, ServiceStateChange, Zeroconf
from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconfServiceTypes


Expand Down Expand Up @@ -75,6 +75,20 @@ class MdnsServiceType(Enum):
BORDER_ROUTER = "_meshcop._udp.local."


class DummyServiceListener(ServiceListener):
"""
A service listener required for the TXT record data to get populated and come back
"""
def add_service(self, zeroconf: Zeroconf, service_type: str, name: str) -> None:
pass

def remove_service(self, zeroconf: Zeroconf, service_type: str, name: str) -> None:
pass

def update_service(self, zeroconf: Zeroconf, service_type: str, name: str) -> None:
pass


class MdnsDiscovery:

DISCOVERY_TIMEOUT_SEC = 15
Expand Down Expand Up @@ -160,10 +174,13 @@ async def get_operational_service(self, service_name: str = None,
mdns_service_info = await self._get_service(MdnsServiceType.OPERATIONAL, log_output, discovery_timeout_sec)
else:
print(f"Looking for MDNS service type '{service_type}', service name '{service_name}'")

# Get service info
service_listener = DummyServiceListener()
self._zc.add_service_listener(MdnsServiceType.OPERATIONAL.value, service_listener)
# Adds delay so TXT record is able to get populated
await asyncio.sleep(1)
service_info = AsyncServiceInfo(service_type, service_name)
is_discovered = await service_info.async_request(self._zc, 3000)
self._zc.remove_service_listener(service_listener)
if is_discovered:
mdns_service_info = self._to_mdns_service_info_class(service_info)
self._discovered_services = {}
Expand Down

0 comments on commit 903238b

Please sign in to comment.