mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-06 10:44:04 +00:00
Update discover single to handle hostnames (#539)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# type: ignore
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
|
||||
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
|
||||
@@ -74,6 +75,31 @@ async def test_discover_single(discovery_data: dict, mocker, custom_port):
|
||||
assert x.port == custom_port or x.port == 9999
|
||||
|
||||
|
||||
async def test_discover_single_hostname(discovery_data: dict, mocker):
|
||||
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
||||
host = "foobar"
|
||||
ip = "127.0.0.1"
|
||||
|
||||
def mock_discover(self):
|
||||
self.datagram_received(
|
||||
protocol.TPLinkSmartHomeProtocol.encrypt(json_dumps(discovery_data))[4:],
|
||||
(ip, 9999),
|
||||
)
|
||||
|
||||
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
|
||||
mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=discovery_data)
|
||||
mocker.patch("socket.getaddrinfo", return_value=[(None, None, None, None, (ip, 0))])
|
||||
|
||||
x = await Discover.discover_single(host)
|
||||
assert issubclass(x.__class__, SmartDevice)
|
||||
assert x._sys_info is not None
|
||||
assert x.host == host
|
||||
|
||||
mocker.patch("socket.getaddrinfo", side_effect=socket.gaierror())
|
||||
with pytest.raises(SmartDeviceException):
|
||||
x = await Discover.discover_single(host)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("custom_port", [123, None])
|
||||
async def test_connect_single(discovery_data: dict, mocker, custom_port):
|
||||
"""Make sure that connect_single returns an initialized SmartDevice instance."""
|
||||
|
Reference in New Issue
Block a user