2024-06-27 17:52:54 +00:00
|
|
|
"""Module for testing device factory.
|
|
|
|
|
|
|
|
As this module tests the factory with discovery data and expects update to be
|
|
|
|
called on devices it uses the discovery_mock handles all the patching of the
|
|
|
|
query methods without actually replacing the device protocol class with one of
|
|
|
|
the testing fake protocols.
|
|
|
|
"""
|
|
|
|
|
2023-11-21 22:48:53 +00:00
|
|
|
import logging
|
2024-06-27 17:52:54 +00:00
|
|
|
from typing import cast
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
import aiohttp
|
2023-11-21 22:48:53 +00:00
|
|
|
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
|
|
|
|
|
|
|
|
from kasa import (
|
2023-12-04 18:50:05 +00:00
|
|
|
Credentials,
|
|
|
|
Discover,
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
2023-11-21 22:48:53 +00:00
|
|
|
)
|
2024-03-01 18:32:45 +00:00
|
|
|
from kasa.device_factory import (
|
2024-06-27 17:52:54 +00:00
|
|
|
Device,
|
|
|
|
SmartDevice,
|
2024-03-01 18:32:45 +00:00
|
|
|
_get_device_type_from_sys_info,
|
|
|
|
connect,
|
2024-04-25 06:36:30 +00:00
|
|
|
get_device_class_from_family,
|
2024-03-01 18:32:45 +00:00
|
|
|
get_protocol,
|
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
from kasa.deviceconfig import (
|
|
|
|
DeviceConfig,
|
2024-06-03 18:06:54 +00:00
|
|
|
DeviceConnectionParameters,
|
|
|
|
DeviceEncryptionType,
|
|
|
|
DeviceFamily,
|
2023-12-04 18:50:05 +00:00
|
|
|
)
|
|
|
|
from kasa.discover import DiscoveryResult
|
2024-06-27 17:52:54 +00:00
|
|
|
|
|
|
|
from .conftest import DISCOVERY_MOCK_IP
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
# Device Factory tests are not relevant for real devices which run against
|
|
|
|
# a single device that has already been created via the factory.
|
|
|
|
pytestmark = [pytest.mark.requires_dummy]
|
|
|
|
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
def _get_connection_type_device_class(discovery_info):
|
|
|
|
if "result" in discovery_info:
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_info)
|
2024-11-12 21:00:04 +00:00
|
|
|
dr = DiscoveryResult.from_dict(discovery_info["result"])
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-06-03 18:06:54 +00:00
|
|
|
connection_type = DeviceConnectionParameters.from_values(
|
2023-12-29 19:17:15 +00:00
|
|
|
dr.device_type, dr.mgt_encrypt_schm.encrypt_type
|
|
|
|
)
|
2023-12-04 18:50:05 +00:00
|
|
|
else:
|
2024-06-03 18:06:54 +00:00
|
|
|
connection_type = DeviceConnectionParameters.from_values(
|
|
|
|
DeviceFamily.IotSmartPlugSwitch.value, DeviceEncryptionType.Xor.value
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_info)
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
return connection_type, device_class
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
async def test_connect(
|
2024-06-27 17:52:54 +00:00
|
|
|
discovery_mock,
|
2023-11-21 22:48:53 +00:00
|
|
|
mocker,
|
|
|
|
):
|
2023-12-29 19:17:15 +00:00
|
|
|
"""Test that if the protocol is passed in it gets set correctly."""
|
2024-06-27 17:52:54 +00:00
|
|
|
host = DISCOVERY_MOCK_IP
|
|
|
|
ctype, device_class = _get_connection_type_device_class(
|
|
|
|
discovery_mock.discovery_data
|
|
|
|
)
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host=host, credentials=Credentials("foor", "bar"), connection_type=ctype
|
|
|
|
)
|
|
|
|
protocol_class = get_protocol(config).__class__
|
2024-02-14 17:03:50 +00:00
|
|
|
close_mock = mocker.patch.object(protocol_class, "close")
|
2024-06-27 17:52:54 +00:00
|
|
|
# mocker.patch.object(SmartDevice, "update")
|
|
|
|
# mocker.patch.object(Device, "update")
|
2023-12-29 19:17:15 +00:00
|
|
|
dev = await connect(
|
|
|
|
config=config,
|
|
|
|
)
|
|
|
|
assert isinstance(dev, device_class)
|
|
|
|
assert isinstance(dev.protocol, protocol_class)
|
|
|
|
|
|
|
|
assert dev.config == config
|
2024-02-14 17:03:50 +00:00
|
|
|
assert close_mock.call_count == 0
|
2024-01-23 22:15:18 +00:00
|
|
|
await dev.disconnect()
|
2024-02-14 17:03:50 +00:00
|
|
|
assert close_mock.call_count == 1
|
2024-01-23 22:15:18 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("custom_port", [123, None])
|
2024-06-27 17:52:54 +00:00
|
|
|
async def test_connect_custom_port(discovery_mock, mocker, custom_port):
|
2023-12-29 19:17:15 +00:00
|
|
|
"""Make sure that connect returns an initialized SmartDevice instance."""
|
2024-06-27 17:52:54 +00:00
|
|
|
host = DISCOVERY_MOCK_IP
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-06-27 17:52:54 +00:00
|
|
|
discovery_data = discovery_mock.discovery_data
|
2024-02-27 17:39:04 +00:00
|
|
|
ctype, _ = _get_connection_type_device_class(discovery_data)
|
2024-01-03 18:26:52 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host=host,
|
|
|
|
port_override=custom_port,
|
|
|
|
connection_type=ctype,
|
|
|
|
credentials=Credentials("dummy_user", "dummy_password"),
|
|
|
|
)
|
2024-02-27 17:39:04 +00:00
|
|
|
default_port = 80 if "result" in discovery_data else 9999
|
|
|
|
|
|
|
|
ctype, _ = _get_connection_type_device_class(discovery_data)
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
dev = await connect(config=config)
|
2024-02-04 15:20:08 +00:00
|
|
|
assert issubclass(dev.__class__, Device)
|
2023-12-29 19:17:15 +00:00
|
|
|
assert dev.port == custom_port or dev.port == default_port
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_connect_logs_connect_time(
|
2024-06-27 17:52:54 +00:00
|
|
|
discovery_mock,
|
2024-02-27 17:39:04 +00:00
|
|
|
caplog: pytest.LogCaptureFixture,
|
2023-11-21 22:48:53 +00:00
|
|
|
):
|
|
|
|
"""Test that the connect time is logged when debug logging is enabled."""
|
2024-06-27 17:52:54 +00:00
|
|
|
discovery_data = discovery_mock.discovery_data
|
2024-02-27 17:39:04 +00:00
|
|
|
ctype, _ = _get_connection_type_device_class(discovery_data)
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-06-27 17:52:54 +00:00
|
|
|
host = DISCOVERY_MOCK_IP
|
2023-12-29 19:17:15 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host=host, credentials=Credentials("foor", "bar"), connection_type=ctype
|
|
|
|
)
|
|
|
|
logging.getLogger("kasa").setLevel(logging.DEBUG)
|
|
|
|
await connect(
|
|
|
|
config=config,
|
|
|
|
)
|
|
|
|
assert "seconds to update" in caplog.text
|
2023-11-28 19:13:15 +00:00
|
|
|
|
|
|
|
|
2024-06-27 17:52:54 +00:00
|
|
|
async def test_connect_query_fails(discovery_mock, mocker):
|
2023-12-29 19:17:15 +00:00
|
|
|
"""Make sure that connect fails when query fails."""
|
2024-06-27 17:52:54 +00:00
|
|
|
host = DISCOVERY_MOCK_IP
|
|
|
|
discovery_data = discovery_mock.discovery_data
|
2024-02-21 15:52:55 +00:00
|
|
|
mocker.patch("kasa.IotProtocol.query", side_effect=KasaException)
|
|
|
|
mocker.patch("kasa.SmartProtocol.query", side_effect=KasaException)
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2024-02-27 17:39:04 +00:00
|
|
|
ctype, _ = _get_connection_type_device_class(discovery_data)
|
2023-12-29 19:17:15 +00:00
|
|
|
config = DeviceConfig(
|
|
|
|
host=host, credentials=Credentials("foor", "bar"), connection_type=ctype
|
|
|
|
)
|
2024-02-14 17:03:50 +00:00
|
|
|
protocol_class = get_protocol(config).__class__
|
|
|
|
close_mock = mocker.patch.object(protocol_class, "close")
|
|
|
|
assert close_mock.call_count == 0
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2023-12-29 19:17:15 +00:00
|
|
|
await connect(config=config)
|
2024-02-14 17:03:50 +00:00
|
|
|
assert close_mock.call_count == 1
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
2024-06-27 17:52:54 +00:00
|
|
|
async def test_connect_http_client(discovery_mock, mocker):
|
2023-12-29 19:17:15 +00:00
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
2024-06-27 17:52:54 +00:00
|
|
|
host = DISCOVERY_MOCK_IP
|
|
|
|
discovery_data = discovery_mock.discovery_data
|
2024-02-27 17:39:04 +00:00
|
|
|
ctype, _ = _get_connection_type_device_class(discovery_data)
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
http_client = aiohttp.ClientSession()
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
config = DeviceConfig(
|
|
|
|
host=host, credentials=Credentials("foor", "bar"), connection_type=ctype
|
2023-12-04 18:50:05 +00:00
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
dev = await connect(config=config)
|
2024-06-03 18:06:54 +00:00
|
|
|
if ctype.encryption_type != DeviceEncryptionType.Xor:
|
2024-01-18 09:57:33 +00:00
|
|
|
assert dev.protocol._transport._http_client.client != http_client
|
2024-02-27 17:39:04 +00:00
|
|
|
await dev.disconnect()
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
config = DeviceConfig(
|
|
|
|
host=host,
|
|
|
|
credentials=Credentials("foor", "bar"),
|
|
|
|
connection_type=ctype,
|
|
|
|
http_client=http_client,
|
|
|
|
)
|
|
|
|
dev = await connect(config=config)
|
2024-06-03 18:06:54 +00:00
|
|
|
if ctype.encryption_type != DeviceEncryptionType.Xor:
|
2024-01-18 09:57:33 +00:00
|
|
|
assert dev.protocol._transport._http_client.client == http_client
|
2024-02-27 17:39:04 +00:00
|
|
|
await dev.disconnect()
|
|
|
|
await http_client.close()
|
2024-03-01 18:32:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_device_types(dev: Device):
|
|
|
|
await dev.update()
|
|
|
|
if isinstance(dev, SmartDevice):
|
2024-06-27 17:52:54 +00:00
|
|
|
assert dev._discovery_info
|
|
|
|
device_type = cast(str, dev._discovery_info["result"]["device_type"])
|
2024-03-01 18:32:45 +00:00
|
|
|
res = SmartDevice._get_device_type_from_components(
|
2024-06-27 17:52:54 +00:00
|
|
|
list(dev._components.keys()), device_type
|
2024-03-01 18:32:45 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
res = _get_device_type_from_sys_info(dev._last_update)
|
|
|
|
|
|
|
|
assert dev.device_type == res
|
2024-04-25 06:36:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_device_class_from_unknown_family(caplog):
|
|
|
|
"""Verify that unknown SMART devices yield a warning and fallback to SmartDevice."""
|
|
|
|
dummy_name = "SMART.foo"
|
|
|
|
with caplog.at_level(logging.WARNING):
|
2024-10-22 17:09:35 +00:00
|
|
|
assert get_device_class_from_family(dummy_name, https=False) == SmartDevice
|
2024-04-25 06:36:30 +00:00
|
|
|
assert f"Unknown SMART device with {dummy_name}" in caplog.text
|