Improve discovery by sending multiple UDP datagrams (#172)

As UDP datagrams may get lost in transit, e.g.,  due to poor network connectivity,
we can improve the detection rate by sending multiple datagrams and hoping some
will get through to the devices.

Confirmed to improve the detection by @scubanarc: https://github.com/home-assistant/home-assistant/issues/21966

This is a new configurable option, defaulting to three discover packets.
This commit is contained in:
Teemu R 2019-05-20 21:56:01 -04:00 committed by GitHub
parent 29f75d9566
commit 60add6fa66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,7 +16,8 @@ class Discover:
@staticmethod @staticmethod
def discover(protocol: TPLinkSmartHomeProtocol = None, def discover(protocol: TPLinkSmartHomeProtocol = None,
port: int = 9999, port: int = 9999,
timeout: int = 3) -> Dict[str, SmartDevice]: timeout: int = 3,
discovery_packets = 3) -> Dict[str, SmartDevice]:
""" """
Sends discovery message to 255.255.255.255:9999 in order Sends discovery message to 255.255.255.255:9999 in order
to detect available supported devices in the local network, to detect available supported devices in the local network,
@ -42,6 +43,7 @@ class Discover:
_LOGGER.debug("Sending discovery to %s:%s", target, port) _LOGGER.debug("Sending discovery to %s:%s", target, port)
encrypted_req = protocol.encrypt(req) encrypted_req = protocol.encrypt(req)
for i in range(discovery_packets):
sock.sendto(encrypted_req[4:], (target, port)) sock.sendto(encrypted_req[4:], (target, port))
devices = {} devices = {}