From 9030a3ea770b9e92cfdb040848e7dc826e73fc67 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Nov 2023 10:04:30 -0600 Subject: [PATCH] relo --- kasa/tests/test_device_factory.py | 71 +++++++++++++++++++++++++++++++ kasa/tests/test_device_type.py | 35 +++++++++++++++ kasa/tests/test_discovery.py | 49 +-------------------- 3 files changed, 107 insertions(+), 48 deletions(-) create mode 100644 kasa/tests/test_device_factory.py create mode 100644 kasa/tests/test_device_type.py diff --git a/kasa/tests/test_device_factory.py b/kasa/tests/test_device_factory.py new file mode 100644 index 00000000..ec03e5bc --- /dev/null +++ b/kasa/tests/test_device_factory.py @@ -0,0 +1,71 @@ +# type: ignore +import re +import socket +import sys +from typing import Type + +import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342 + +from kasa import ( + DeviceType, + Discover, + SmartBulb, + SmartDevice, + SmartDeviceException, + SmartDimmer, + SmartLightStrip, + SmartPlug, + protocol, +) +from kasa.device_factory import DEVICE_TYPE_TO_CLASS, connect +from kasa.discover import _DiscoverProtocol, json_dumps +from kasa.exceptions import UnsupportedDeviceException + +from .conftest import bulb, dimmer, lightstrip, plug, strip + + +@pytest.mark.parametrize("custom_port", [123, None]) +async def test_connect(discovery_data: dict, mocker, custom_port): + """Make sure that connect_single returns an initialized SmartDevice instance.""" + host = "127.0.0.1" + mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=discovery_data) + + dev = await connect(host, port=custom_port) + assert issubclass(dev.__class__, SmartDevice) + assert dev.port == custom_port or dev.port == 9999 + + +@pytest.mark.parametrize("custom_port", [123, None]) +@pytest.mark.parametrize( + ("device_type", "klass"), + ( + (DeviceType.Plug, SmartPlug), + (DeviceType.Bulb, SmartBulb), + (DeviceType.Dimmer, SmartDimmer), + (DeviceType.LightStrip, SmartLightStrip), + (DeviceType.Unknown, SmartDevice), + ), +) +async def test_connect_passed_device_type( + discovery_data: dict, + mocker, + device_type: DeviceType, + klass: Type[SmartDevice], + custom_port, +): + """Make sure that connect_single with a passed device type.""" + host = "127.0.0.1" + mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=discovery_data) + + dev = await connect(host, port=custom_port, device_type=device_type) + assert isinstance(dev, klass) + assert dev.port == custom_port or dev.port == 9999 + + +async def test_connect_query_fails(discovery_data: dict, mocker): + """Make sure that connect_single fails when query fails.""" + host = "127.0.0.1" + mocker.patch("kasa.TPLinkSmartHomeProtocol.query", side_effect=SmartDeviceException) + + with pytest.raises(SmartDeviceException): + await connect(host) diff --git a/kasa/tests/test_device_type.py b/kasa/tests/test_device_type.py new file mode 100644 index 00000000..8058d27e --- /dev/null +++ b/kasa/tests/test_device_type.py @@ -0,0 +1,35 @@ +import inspect +from datetime import datetime +from unittest.mock import patch + +import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342 + +import kasa +from kasa import Credentials, SmartDevice, SmartDeviceException +from kasa.smartdevice import DeviceType +from kasa.smartstrip import SmartStripPlug + +from .conftest import handle_turn_on, has_emeter, no_emeter, turn_on +from .newfakes import PLUG_SCHEMA, TZ_SCHEMA, FakeTransportProtocol + + +async def test_device_type_from_value(): + """Make sure that every device type can be created from its value.""" + for name in DeviceType: + assert DeviceType.from_value(name.value) is not None + + assert DeviceType.from_value("nonexistent") is DeviceType.Unknown + assert DeviceType.from_value("plug") is DeviceType.Plug + assert DeviceType.Plug.value == "plug" + + assert DeviceType.from_value("bulb") is DeviceType.Bulb + assert DeviceType.Bulb.value == "bulb" + + assert DeviceType.from_value("dimmer") is DeviceType.Dimmer + assert DeviceType.Dimmer.value == "dimmer" + + assert DeviceType.from_value("strip") is DeviceType.Strip + assert DeviceType.Strip.value == "strip" + + assert DeviceType.from_value("lightstrip") is DeviceType.LightStrip + assert DeviceType.LightStrip.value == "lightstrip" diff --git a/kasa/tests/test_discovery.py b/kasa/tests/test_discovery.py index ff427837..79852d88 100644 --- a/kasa/tests/test_discovery.py +++ b/kasa/tests/test_discovery.py @@ -1,8 +1,8 @@ # type: ignore import re -from typing import Type import socket import sys +from typing import Type import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342 @@ -111,53 +111,6 @@ async def test_discover_single_hostname(discovery_data: dict, mocker): 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.""" - host = "127.0.0.1" - mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=discovery_data) - - dev = await Discover.connect_single(host, port=custom_port) - assert issubclass(dev.__class__, SmartDevice) - assert dev.port == custom_port or dev.port == 9999 - - -@pytest.mark.parametrize("custom_port", [123, None]) -@pytest.mark.parametrize( - ("device_type", "klass"), - ( - (DeviceType.Plug, SmartPlug), - (DeviceType.Bulb, SmartBulb), - (DeviceType.Dimmer, SmartDimmer), - (DeviceType.LightStrip, SmartLightStrip), - (DeviceType.Unknown, SmartDevice), - ), -) -async def test_connect_single_passed_device_type( - discovery_data: dict, - mocker, - device_type: DeviceType, - klass: Type[SmartDevice], - custom_port, -): - """Make sure that connect_single with a passed device type.""" - host = "127.0.0.1" - mocker.patch("kasa.TPLinkSmartHomeProtocol.query", return_value=discovery_data) - - dev = await Discover.connect_single(host, port=custom_port, device_type=device_type) - assert isinstance(dev, klass) - assert dev.port == custom_port or dev.port == 9999 - - -async def test_connect_single_query_fails(discovery_data: dict, mocker): - """Make sure that connect_single fails when query fails.""" - host = "127.0.0.1" - mocker.patch("kasa.TPLinkSmartHomeProtocol.query", side_effect=SmartDeviceException) - - with pytest.raises(SmartDeviceException): - await Discover.connect_single(host) - - UNSUPPORTED = { "result": { "device_id": "xx",