Skip to content

Commit

Permalink
'Interface' parameter added to discovery process (#79)
Browse files Browse the repository at this point in the history
* '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. <tpr@iki.fi>

Co-authored-by: Teemu R. <tpr@iki.fi>
  • Loading branch information
dmitryelj and rytilahti committed Jul 28, 2020
1 parent c67bda8 commit 1cd1e84
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion kasa/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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
Expand Down Expand Up @@ -35,10 +35,12 @@ def __init__(
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)
Expand All @@ -51,6 +53,8 @@ def connection_made(self, transport) -> None:
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()

Expand Down Expand Up @@ -145,6 +149,7 @@ async def discover(
timeout=5,
discovery_packets=3,
return_raw=False,
interface=None,
) -> Mapping[str, Union[SmartDevice, Dict]]:
"""Discover supported devices.
Expand All @@ -171,6 +176,7 @@ async def discover(
on_discovered=on_discovered,
timeout=timeout,
discovery_packets=discovery_packets,
interface=interface,
),
local_addr=("0.0.0.0", 0),
)
Expand Down

0 comments on commit 1cd1e84

Please sign in to comment.