2020-05-20 19:17:33 +00:00
|
|
|
# type: ignore
|
2021-09-24 15:18:11 +00:00
|
|
|
import sys
|
|
|
|
|
2020-05-20 19:17:33 +00:00
|
|
|
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
|
|
|
|
|
2021-09-24 21:25:43 +00:00
|
|
|
from kasa import DeviceType, Discover, SmartDevice, SmartDeviceException, protocol
|
2021-09-24 15:18:11 +00:00
|
|
|
from kasa.discover import _DiscoverProtocol
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2022-11-13 22:34:47 +00:00
|
|
|
from .conftest import bulb, dimmer, lightstrip, plug, strip
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@bulb
|
|
|
|
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"}}}
|
|
|
|
with pytest.raises(SmartDeviceException):
|
|
|
|
Discover._get_device_class(invalid_info)
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info < (3, 8), reason="3.8 is first one with asyncmock")
|
|
|
|
async def test_discover_single(discovery_data: dict, mocker):
|
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
|
|
|
mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=discovery_data)
|
|
|
|
x = await Discover.discover_single("127.0.0.1")
|
|
|
|
assert issubclass(x.__class__, SmartDevice)
|
|
|
|
assert x._sys_info is not None
|
|
|
|
|
|
|
|
|
|
|
|
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.skipif(sys.version_info < (3, 8), reason="3.8 is first one with asyncmock")
|
|
|
|
@pytest.mark.parametrize("msg, data", INVALIDS)
|
|
|
|
async def test_discover_invalid_info(msg, data, mocker):
|
|
|
|
"""Make sure that invalid discovery information raises an exception."""
|
|
|
|
mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=data)
|
|
|
|
with pytest.raises(SmartDeviceException, match=msg):
|
|
|
|
await Discover.discover_single("127.0.0.1")
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
2021-10-29 00:44:51 +00:00
|
|
|
assert transport.sendto.call_count == proto.discovery_packets
|
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()
|
|
|
|
mocker.patch("json.loads", return_value=discovery_data)
|
2021-09-24 21:25:43 +00:00
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "encrypt")
|
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "decrypt")
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
addr = "127.0.0.1"
|
|
|
|
proto.datagram_received("<placeholder data>", (addr, 1234))
|
|
|
|
|
|
|
|
# Check that device in discovered_devices is initialized correctly
|
|
|
|
assert len(proto.discovered_devices) == 1
|
|
|
|
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()
|
|
|
|
mocker.patch("json.loads", return_value=data)
|
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "encrypt")
|
|
|
|
mocker.patch.object(protocol.TPLinkSmartHomeProtocol, "decrypt")
|
|
|
|
|
|
|
|
proto.datagram_received(data, ("127.0.0.1", 1234))
|
|
|
|
assert len(proto.discovered_devices) == 0
|