Fix repr for device created with no sysinfo or discovery info" (#1266)

Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
Steven B.
2024-11-18 13:14:39 +00:00
committed by GitHub
parent fd5258c28b
commit 9d46996e9b
7 changed files with 101 additions and 18 deletions

View File

@@ -9,7 +9,7 @@ from kasa import Device
from kasa.device_type import DeviceType
from kasa.protocols.smartprotocol import _ChildProtocolWrapper
from kasa.smart.smartchilddevice import SmartChildDevice
from kasa.smart.smartdevice import NON_HUB_PARENT_ONLY_MODULES
from kasa.smart.smartdevice import NON_HUB_PARENT_ONLY_MODULES, SmartDevice
from .conftest import (
parametrize,
@@ -139,3 +139,19 @@ async def test_child_time(dev: Device, freezer: FrozenDateTimeFactory):
assert dev.parent is None
for child in dev.children:
assert child.time != fallback_time
async def test_child_device_type_unknown(caplog):
"""Test for device type when category is unknown."""
class DummyDevice(SmartChildDevice):
def __init__(self):
super().__init__(
SmartDevice("127.0.0.1"),
{"device_id": "1", "category": "foobar"},
{"device", 1},
)
assert DummyDevice().device_type is DeviceType.Unknown
msg = "Unknown child device type foobar for model None, please open issue"
assert msg in caplog.text

View File

@@ -14,7 +14,15 @@ import zoneinfo
import kasa
from kasa import Credentials, Device, DeviceConfig, DeviceType, KasaException, Module
from kasa.iot import IotDevice
from kasa.iot import (
IotBulb,
IotDevice,
IotDimmer,
IotLightStrip,
IotPlug,
IotStrip,
IotWallSwitch,
)
from kasa.iot.iottimezone import (
TIMEZONE_INDEX,
get_timezone,
@@ -22,6 +30,7 @@ from kasa.iot.iottimezone import (
)
from kasa.iot.modules import IotLightPreset
from kasa.smart import SmartChildDevice, SmartDevice
from kasa.smartcamera import SmartCamera
def _get_subclasses(of_class):
@@ -80,6 +89,41 @@ async def test_device_class_ctors(device_class_name_obj):
assert dev.credentials == credentials
@device_classes
async def test_device_class_repr(device_class_name_obj):
"""Test device repr when update() not called and no discovery info."""
host = "127.0.0.2"
port = 1234
credentials = Credentials("foo", "bar")
config = DeviceConfig(host, port_override=port, credentials=credentials)
klass = device_class_name_obj[1]
if issubclass(klass, SmartChildDevice):
parent = SmartDevice(host, config=config)
dev = klass(
parent, {"dummy": "info", "device_id": "dummy"}, {"dummy": "components"}
)
else:
dev = klass(host, config=config)
CLASS_TO_DEFAULT_TYPE = {
IotDevice: DeviceType.Unknown,
IotBulb: DeviceType.Bulb,
IotPlug: DeviceType.Plug,
IotDimmer: DeviceType.Dimmer,
IotStrip: DeviceType.Strip,
IotWallSwitch: DeviceType.WallSwitch,
IotLightStrip: DeviceType.LightStrip,
SmartChildDevice: DeviceType.Unknown,
SmartDevice: DeviceType.Unknown,
SmartCamera: DeviceType.Camera,
}
type_ = CLASS_TO_DEFAULT_TYPE[klass]
child_repr = "<DeviceType.Unknown(child) of <DeviceType.Unknown at 127.0.0.2 - update() needed>>"
not_child_repr = f"<{type_} at 127.0.0.2 - update() needed>"
expected_repr = child_repr if klass is SmartChildDevice else not_child_repr
assert repr(dev) == expected_repr
async def test_create_device_with_timeout():
"""Make sure timeout is passed to the protocol."""
host = "127.0.0.1"