Update connect_single to allow passing in the device type

This commit is contained in:
J. Nick Koston
2023-10-31 16:11:23 -05:00
parent 805e4b8588
commit e638c7b189
4 changed files with 91 additions and 21 deletions

View File

@@ -4,7 +4,18 @@ import sys
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
from kasa import DeviceType, Discover, SmartDevice, SmartDeviceException, protocol
from kasa import (
DeviceType,
Discover,
SmartBulb,
SmartDevice,
SmartDeviceException,
SmartDimmer,
SmartLightStrip,
SmartPlug,
SmartStrip,
protocol,
)
from kasa.discover import _DiscoverProtocol, json_dumps
from kasa.exceptions import UnsupportedDeviceException
@@ -85,6 +96,33 @@ async def test_connect_single(discovery_data: dict, mocker, custom_port):
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"

View File

@@ -6,6 +6,7 @@ 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
@@ -58,6 +59,16 @@ async def test_initial_update_no_emeter(dev, mocker):
assert spy.call_count == 2
async def test_smart_device_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"
async def test_query_helper(dev):
with pytest.raises(SmartDeviceException):
await dev._query_helper("test", "testcmd", {})