mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-08 22:07:06 +00:00
'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. <tpr@iki.fi> Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
parent
c67bda8230
commit
1cd1e84f78
@ -3,7 +3,7 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import socket
|
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.protocol import TPLinkSmartHomeProtocol
|
||||||
from kasa.smartbulb import SmartBulb
|
from kasa.smartbulb import SmartBulb
|
||||||
@ -35,10 +35,12 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
|
|||||||
target: str = "255.255.255.255",
|
target: str = "255.255.255.255",
|
||||||
timeout: int = 5,
|
timeout: int = 5,
|
||||||
discovery_packets: int = 3,
|
discovery_packets: int = 3,
|
||||||
|
interface: Optional[str] = None,
|
||||||
):
|
):
|
||||||
self.transport = None
|
self.transport = None
|
||||||
self.tries = discovery_packets
|
self.tries = discovery_packets
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
self.interface = interface
|
||||||
self.on_discovered = on_discovered
|
self.on_discovered = on_discovered
|
||||||
self.protocol = TPLinkSmartHomeProtocol()
|
self.protocol = TPLinkSmartHomeProtocol()
|
||||||
self.target = (target, Discover.DISCOVERY_PORT)
|
self.target = (target, Discover.DISCOVERY_PORT)
|
||||||
@ -51,6 +53,8 @@ class _DiscoverProtocol(asyncio.DatagramProtocol):
|
|||||||
sock = transport.get_extra_info("socket")
|
sock = transport.get_extra_info("socket")
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 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()
|
self.do_discover()
|
||||||
|
|
||||||
@ -145,6 +149,7 @@ class Discover:
|
|||||||
timeout=5,
|
timeout=5,
|
||||||
discovery_packets=3,
|
discovery_packets=3,
|
||||||
return_raw=False,
|
return_raw=False,
|
||||||
|
interface=None,
|
||||||
) -> Mapping[str, Union[SmartDevice, Dict]]:
|
) -> Mapping[str, Union[SmartDevice, Dict]]:
|
||||||
"""Discover supported devices.
|
"""Discover supported devices.
|
||||||
|
|
||||||
@ -171,6 +176,7 @@ class Discover:
|
|||||||
on_discovered=on_discovered,
|
on_discovered=on_discovered,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
discovery_packets=discovery_packets,
|
discovery_packets=discovery_packets,
|
||||||
|
interface=interface,
|
||||||
),
|
),
|
||||||
local_addr=("0.0.0.0", 0),
|
local_addr=("0.0.0.0", 0),
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user