From 1cd1e84f78128e2e8401293afc08dcd8c39056e9 Mon Sep 17 00:00:00 2001 From: Dmitrii Eliuseev Date: Tue, 28 Jul 2020 23:20:58 +0200 Subject: [PATCH] 'Interface' parameter added to discovery process (#79) * 'Interface' parameter added to discovery process Some systems (for example dd-wrt) requires specifying the network interface name, otherwise the broadcast does not work * 'Interface' variable type and check fixed * Formatting fixed * Update kasa/discover.py Co-authored-by: Teemu R. Co-authored-by: Teemu R. --- kasa/discover.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kasa/discover.py b/kasa/discover.py index 9fe0f451..4a3d0a74 100755 --- a/kasa/discover.py +++ b/kasa/discover.py @@ -3,7 +3,7 @@ import asyncio import json import logging import socket -from typing import Awaitable, Callable, Dict, Mapping, Type, Union, cast +from typing import Awaitable, Callable, Dict, Mapping, Optional, Type, Union, cast from kasa.protocol import TPLinkSmartHomeProtocol from kasa.smartbulb import SmartBulb @@ -35,10 +35,12 @@ class _DiscoverProtocol(asyncio.DatagramProtocol): target: str = "255.255.255.255", timeout: int = 5, discovery_packets: int = 3, + interface: Optional[str] = None, ): self.transport = None self.tries = discovery_packets self.timeout = timeout + self.interface = interface self.on_discovered = on_discovered self.protocol = TPLinkSmartHomeProtocol() self.target = (target, Discover.DISCOVERY_PORT) @@ -51,6 +53,8 @@ class _DiscoverProtocol(asyncio.DatagramProtocol): sock = transport.get_extra_info("socket") sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + if self.interface is not None: + sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, self.interface.encode()) self.do_discover() @@ -145,6 +149,7 @@ class Discover: timeout=5, discovery_packets=3, return_raw=False, + interface=None, ) -> Mapping[str, Union[SmartDevice, Dict]]: """Discover supported devices. @@ -171,6 +176,7 @@ class Discover: on_discovered=on_discovered, timeout=timeout, discovery_packets=discovery_packets, + interface=interface, ), local_addr=("0.0.0.0", 0), )