2020-05-20 19:17:33 +00:00
|
|
|
# type: ignore
|
2023-12-29 19:17:15 +00:00
|
|
|
import logging
|
2023-08-29 13:04:28 +00:00
|
|
|
import re
|
2023-11-07 01:15:41 +00:00
|
|
|
import socket
|
2021-09-24 15:18:11 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
import httpx
|
2020-05-20 19:17:33 +00:00
|
|
|
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
|
|
|
|
|
2023-11-21 20:58:41 +00:00
|
|
|
from kasa import (
|
2023-12-29 19:17:15 +00:00
|
|
|
Credentials,
|
2023-11-21 20:58:41 +00:00
|
|
|
DeviceType,
|
|
|
|
Discover,
|
|
|
|
SmartDevice,
|
|
|
|
SmartDeviceException,
|
|
|
|
protocol,
|
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
from kasa.deviceconfig import (
|
|
|
|
ConnectionType,
|
|
|
|
DeviceConfig,
|
|
|
|
DeviceFamilyType,
|
|
|
|
EncryptType,
|
|
|
|
)
|
2023-11-20 13:17:10 +00:00
|
|
|
from kasa.discover import DiscoveryResult, _DiscoverProtocol, json_dumps
|
|
|
|
from kasa.exceptions import AuthenticationException, UnsupportedDeviceException
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
from .conftest import bulb, bulb_iot, dimmer, lightstrip, new_discovery, plug, strip
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
UNSUPPORTED = {
|
|
|
|
"result": {
|
|
|
|
"device_id": "xx",
|
|
|
|
"owner": "xx",
|
|
|
|
"device_type": "SMART.TAPOXMASTREE",
|
|
|
|
"device_model": "P110(EU)",
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"mac": "48-22xxx",
|
|
|
|
"is_support_iot_cloud": True,
|
|
|
|
"obd_src": "tplink",
|
|
|
|
"factory_default": False,
|
|
|
|
"mgt_encrypt_schm": {
|
|
|
|
"is_support_https": False,
|
|
|
|
"encrypt_type": "AES",
|
|
|
|
"http_port": 80,
|
|
|
|
"lv": 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"error_code": 0,
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:17:33 +00:00
|
|
|
|
|
|
|
@plug
|
|
|
|
async def test_type_detection_plug(dev: SmartDevice):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-05-20 19:17:33 +00:00
|
|
|
assert d.is_plug
|
|
|
|
assert d.device_type == DeviceType.Plug
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2020-05-20 19:17:33 +00:00
|
|
|
async def test_type_detection_bulb(dev: SmartDevice):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-07-19 20:32:17 +00:00
|
|
|
# TODO: light_strip is a special case for now to force bulb tests on it
|
|
|
|
if not d.is_light_strip:
|
|
|
|
assert d.is_bulb
|
|
|
|
assert d.device_type == DeviceType.Bulb
|
2020-05-20 19:17:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@strip
|
|
|
|
async def test_type_detection_strip(dev: SmartDevice):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-05-20 19:17:33 +00:00
|
|
|
assert d.is_strip
|
|
|
|
assert d.device_type == DeviceType.Strip
|
|
|
|
|
|
|
|
|
|
|
|
@dimmer
|
|
|
|
async def test_type_detection_dimmer(dev: SmartDevice):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-05-20 19:17:33 +00:00
|
|
|
assert d.is_dimmer
|
|
|
|
assert d.device_type == DeviceType.Dimmer
|
|
|
|
|
|
|
|
|
2020-07-19 20:32:17 +00:00
|
|
|
@lightstrip
|
|
|
|
async def test_type_detection_lightstrip(dev: SmartDevice):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-07-19 20:32:17 +00:00
|
|
|
assert d.is_light_strip
|
|
|
|
assert d.device_type == DeviceType.LightStrip
|
|
|
|
|
|
|
|
|
2020-05-20 19:17:33 +00:00
|
|
|
async def test_type_unknown():
|
|
|
|
invalid_info = {"system": {"get_sysinfo": {"type": "nosuchtype"}}}
|
2023-11-20 13:17:10 +00:00
|
|
|
with pytest.raises(UnsupportedDeviceException):
|
2020-05-20 19:17:33 +00:00
|
|
|
Discover._get_device_class(invalid_info)
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
2023-07-09 23:55:27 +00:00
|
|
|
@pytest.mark.parametrize("custom_port", [123, None])
|
2023-12-04 18:50:05 +00:00
|
|
|
# @pytest.mark.parametrize("discovery_mock", [("127.0.0.1",123), ("127.0.0.1",None)], indirect=True)
|
|
|
|
async def test_discover_single(discovery_mock, custom_port, mocker):
|
2021-09-24 15:18:11 +00:00
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
2023-08-29 13:04:28 +00:00
|
|
|
host = "127.0.0.1"
|
2023-12-04 18:50:05 +00:00
|
|
|
discovery_mock.ip = host
|
|
|
|
discovery_mock.port_override = custom_port
|
2023-08-29 13:04:28 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
|
|
|
update_mock = mocker.patch.object(device_class, "update")
|
|
|
|
|
|
|
|
x = await Discover.discover_single(
|
|
|
|
host, port=custom_port, credentials=Credentials()
|
|
|
|
)
|
2021-09-24 15:18:11 +00:00
|
|
|
assert issubclass(x.__class__, SmartDevice)
|
2023-12-04 18:50:05 +00:00
|
|
|
assert x._discovery_info is not None
|
|
|
|
assert x.port == custom_port or x.port == discovery_mock.default_port
|
2023-12-29 19:17:15 +00:00
|
|
|
assert update_mock.call_count == 0
|
2024-01-11 15:12:02 +00:00
|
|
|
if discovery_mock.default_port == 80:
|
|
|
|
assert x.alias is None
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
ct = ConnectionType.from_values(
|
2024-01-03 21:46:08 +00:00
|
|
|
discovery_mock.device_type,
|
|
|
|
discovery_mock.encrypt_type,
|
|
|
|
discovery_mock.login_version,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
|
|
|
uses_http = discovery_mock.default_port == 80
|
|
|
|
config = DeviceConfig(
|
2024-01-03 21:46:08 +00:00
|
|
|
host=host,
|
|
|
|
port_override=custom_port,
|
|
|
|
connection_type=ct,
|
|
|
|
uses_http=uses_http,
|
|
|
|
credentials=Credentials(),
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
|
|
|
assert x.config == config
|
2023-08-29 13:04:28 +00:00
|
|
|
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
async def test_discover_single_hostname(discovery_mock, mocker):
|
2023-11-07 01:15:41 +00:00
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
|
|
|
host = "foobar"
|
|
|
|
ip = "127.0.0.1"
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
discovery_mock.ip = ip
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
|
|
|
update_mock = mocker.patch.object(device_class, "update")
|
2023-11-07 01:15:41 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
x = await Discover.discover_single(host, credentials=Credentials())
|
2023-11-07 01:15:41 +00:00
|
|
|
assert issubclass(x.__class__, SmartDevice)
|
2023-12-04 18:50:05 +00:00
|
|
|
assert x._discovery_info is not None
|
2023-11-07 01:15:41 +00:00
|
|
|
assert x.host == host
|
2023-12-29 19:17:15 +00:00
|
|
|
assert update_mock.call_count == 0
|
2023-11-07 01:15:41 +00:00
|
|
|
|
|
|
|
mocker.patch("socket.getaddrinfo", side_effect=socket.gaierror())
|
|
|
|
with pytest.raises(SmartDeviceException):
|
2023-12-29 19:17:15 +00:00
|
|
|
x = await Discover.discover_single(host, credentials=Credentials())
|
2023-11-07 01:15:41 +00:00
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
async def test_discover_single_unsupported(unsupported_device_info, mocker):
|
2023-08-29 13:04:28 +00:00
|
|
|
"""Make sure that discover_single handles unsupported devices correctly."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
|
|
|
# Test with a valid unsupported response
|
|
|
|
with pytest.raises(
|
|
|
|
UnsupportedDeviceException,
|
|
|
|
):
|
|
|
|
await Discover.discover_single(host)
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
async def test_discover_single_no_response(mocker):
|
|
|
|
"""Make sure that discover_single handles no response correctly."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
mocker.patch.object(_DiscoverProtocol, "do_discover")
|
2023-08-29 13:04:28 +00:00
|
|
|
with pytest.raises(
|
|
|
|
SmartDeviceException, match=f"Timed out getting discovery response for {host}"
|
|
|
|
):
|
2023-12-29 19:17:15 +00:00
|
|
|
await Discover.discover_single(host, discovery_timeout=0)
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
INVALIDS = [
|
|
|
|
("No 'system' or 'get_sysinfo' in response", {"no": "data"}),
|
|
|
|
(
|
|
|
|
"Unable to find the device type field",
|
|
|
|
{"system": {"get_sysinfo": {"missing_type": 1}}},
|
|
|
|
),
|
|
|
|
("Unknown device type: foo", {"system": {"get_sysinfo": {"type": "foo"}}}),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("msg, data", INVALIDS)
|
|
|
|
async def test_discover_invalid_info(msg, data, mocker):
|
|
|
|
"""Make sure that invalid discovery information raises an exception."""
|
2023-08-29 13:04:28 +00:00
|
|
|
host = "127.0.0.1"
|
|
|
|
|
|
|
|
def mock_discover(self):
|
|
|
|
self.datagram_received(
|
|
|
|
protocol.TPLinkSmartHomeProtocol.encrypt(json_dumps(data))[4:], (host, 9999)
|
|
|
|
)
|
|
|
|
|
|
|
|
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
|
|
|
|
|
2021-09-24 15:18:11 +00:00
|
|
|
with pytest.raises(SmartDeviceException, match=msg):
|
2023-08-29 13:04:28 +00:00
|
|
|
await Discover.discover_single(host)
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_send(mocker):
|
|
|
|
"""Test discovery parameters."""
|
|
|
|
proto = _DiscoverProtocol()
|
|
|
|
assert proto.discovery_packets == 3
|
|
|
|
assert proto.target == ("255.255.255.255", 9999)
|
2021-10-29 00:44:51 +00:00
|
|
|
transport = mocker.patch.object(proto, "transport")
|
2021-09-24 15:18:11 +00:00
|
|
|
proto.do_discover()
|
2023-08-29 13:04:28 +00:00
|
|
|
assert transport.sendto.call_count == proto.discovery_packets * 2
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_datagram_received(mocker, discovery_data):
|
|
|
|
"""Verify that datagram received fills discovered_devices."""
|
|
|
|
proto = _DiscoverProtocol()
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2021-09-24 21:25:43 +00:00
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "decrypt")
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
addr = "127.0.0.1"
|
2023-12-04 18:50:05 +00:00
|
|
|
port = 20002 if "result" in discovery_data else 9999
|
|
|
|
|
|
|
|
mocker.patch("kasa.discover.json_loads", return_value=discovery_data)
|
|
|
|
proto.datagram_received("<placeholder data>", (addr, port))
|
|
|
|
|
2023-08-29 13:04:28 +00:00
|
|
|
addr2 = "127.0.0.2"
|
2023-12-04 18:50:05 +00:00
|
|
|
mocker.patch("kasa.discover.json_loads", return_value=UNSUPPORTED)
|
2023-08-29 13:04:28 +00:00
|
|
|
proto.datagram_received("<placeholder data>", (addr2, 20002))
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
# Check that device in discovered_devices is initialized correctly
|
|
|
|
assert len(proto.discovered_devices) == 1
|
2023-08-29 13:04:28 +00:00
|
|
|
# Check that unsupported device is 1
|
2023-12-19 12:50:33 +00:00
|
|
|
assert len(proto.unsupported_device_exceptions) == 1
|
2021-09-24 15:18:11 +00:00
|
|
|
dev = proto.discovered_devices[addr]
|
|
|
|
assert issubclass(dev.__class__, SmartDevice)
|
|
|
|
assert dev.host == addr
|
2021-10-29 00:44:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("msg, data", INVALIDS)
|
|
|
|
async def test_discover_invalid_responses(msg, data, mocker):
|
|
|
|
"""Verify that we don't crash whole discovery if some devices in the network are sending unexpected data."""
|
|
|
|
proto = _DiscoverProtocol()
|
2023-06-17 23:03:04 +00:00
|
|
|
mocker.patch("kasa.discover.json_loads", return_value=data)
|
2021-10-29 00:44:51 +00:00
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "encrypt")
|
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "decrypt")
|
|
|
|
|
2023-08-29 13:04:28 +00:00
|
|
|
proto.datagram_received(data, ("127.0.0.1", 9999))
|
2021-10-29 00:44:51 +00:00
|
|
|
assert len(proto.discovered_devices) == 0
|
2023-11-20 13:17:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
AUTHENTICATION_DATA_KLAP = {
|
|
|
|
"result": {
|
|
|
|
"device_id": "xx",
|
|
|
|
"owner": "xx",
|
|
|
|
"device_type": "IOT.SMARTPLUGSWITCH",
|
|
|
|
"device_model": "HS100(UK)",
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"mac": "12-34-56-78-90-AB",
|
|
|
|
"is_support_iot_cloud": True,
|
|
|
|
"obd_src": "tplink",
|
|
|
|
"factory_default": False,
|
|
|
|
"mgt_encrypt_schm": {
|
|
|
|
"is_support_https": False,
|
|
|
|
"encrypt_type": "KLAP",
|
|
|
|
"http_port": 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"error_code": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
@new_discovery
|
|
|
|
async def test_discover_single_authentication(discovery_mock, mocker):
|
2023-11-20 13:17:10 +00:00
|
|
|
"""Make sure that discover_single handles authenticating devices correctly."""
|
|
|
|
host = "127.0.0.1"
|
2023-12-29 19:17:15 +00:00
|
|
|
discovery_mock.ip = host
|
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
2023-11-20 13:17:10 +00:00
|
|
|
mocker.patch.object(
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class,
|
2023-11-20 13:17:10 +00:00
|
|
|
"update",
|
|
|
|
side_effect=AuthenticationException("Failed to authenticate"),
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
AuthenticationException,
|
|
|
|
match="Failed to authenticate",
|
|
|
|
):
|
2023-12-29 19:17:15 +00:00
|
|
|
device = await Discover.discover_single(
|
|
|
|
host, credentials=Credentials("foo", "bar")
|
|
|
|
)
|
2023-11-21 20:58:41 +00:00
|
|
|
await device.update()
|
2023-11-20 13:17:10 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
mocker.patch.object(device_class, "update")
|
|
|
|
device = await Discover.discover_single(host, credentials=Credentials("foo", "bar"))
|
2023-11-21 20:58:41 +00:00
|
|
|
await device.update()
|
2023-12-29 19:17:15 +00:00
|
|
|
assert isinstance(device, device_class)
|
2023-11-20 13:17:10 +00:00
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
@new_discovery
|
|
|
|
async def test_device_update_from_new_discovery_info(discovery_data):
|
2023-11-20 13:17:10 +00:00
|
|
|
device = SmartDevice("127.0.0.7")
|
2023-12-29 19:17:15 +00:00
|
|
|
discover_info = DiscoveryResult(**discovery_data["result"])
|
2023-11-20 13:17:10 +00:00
|
|
|
discover_dump = discover_info.get_dict()
|
2023-12-29 19:17:15 +00:00
|
|
|
discover_dump["alias"] = "foobar"
|
|
|
|
discover_dump["model"] = discover_dump["device_model"]
|
2023-11-20 13:17:10 +00:00
|
|
|
device.update_from_discover_info(discover_dump)
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
assert device.alias == "foobar"
|
2023-11-20 13:17:10 +00:00
|
|
|
assert device.mac == discover_dump["mac"].replace("-", ":")
|
2023-12-29 19:17:15 +00:00
|
|
|
assert device.model == discover_dump["device_model"]
|
2023-11-20 13:17:10 +00:00
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
SmartDeviceException,
|
|
|
|
match=re.escape("You need to await update() to access the data"),
|
|
|
|
):
|
|
|
|
assert device.supported_modules
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_single_http_client(discovery_mock, mocker):
|
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_mock.ip = host
|
|
|
|
|
|
|
|
http_client = httpx.AsyncClient()
|
|
|
|
|
|
|
|
x: SmartDevice = await Discover.discover_single(host)
|
|
|
|
|
|
|
|
assert x.config.uses_http == (discovery_mock.default_port == 80)
|
|
|
|
|
|
|
|
if discovery_mock.default_port == 80:
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client != http_client
|
2023-12-29 19:17:15 +00:00
|
|
|
x.config.http_client = http_client
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client == http_client
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_http_client(discovery_mock, mocker):
|
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_mock.ip = host
|
|
|
|
|
|
|
|
http_client = httpx.AsyncClient()
|
|
|
|
|
|
|
|
devices = await Discover.discover(discovery_timeout=0)
|
|
|
|
x: SmartDevice = devices[host]
|
|
|
|
assert x.config.uses_http == (discovery_mock.default_port == 80)
|
|
|
|
|
|
|
|
if discovery_mock.default_port == 80:
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client != http_client
|
2023-12-29 19:17:15 +00:00
|
|
|
x.config.http_client = http_client
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client == http_client
|