8000 Make discovery on unsupported devices less noisy (#1291) · python-kasa/python-kasa@f2ba233 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2ba233

Browse files
authored
Make discovery on unsupported devices less noisy (#1291)
1 parent 5221fc0 commit f2ba233

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

kasa/device_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def get_device_class_from_family(
164164
and device_type.startswith("SMART.")
165165
and not require_exact
166166
):
167-
_LOGGER.warning("Unknown SMART device with %s, using SmartDevice", device_type)
167+
_LOGGER.debug("Unknown SMART device with %s, using SmartDevice", device_type)
168168
cls = SmartDevice
169169

170170
return cls

kasa/discover.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ def _get_device_instance(
715715
raise KasaException(
716716
f"Unable to read response from device: {config.host}: {ex}"
717717
) from ex
718+
718719
try:
719720
discovery_result = DiscoveryResult.from_dict(info["result"])
720721
except Exception as ex:
@@ -733,6 +734,7 @@ def _get_device_instance(
733734
f"Unable to parse discovery from device: {config.host}: {ex}",
734735
host=config.host,
735736
) from ex
737+
736738
# Decrypt the data
737739
if (
738740
encrypt_info := discovery_result.encrypt_info
@@ -746,11 +748,13 @@ def _get_device_instance(
746748

747749
type_ = discovery_result.device_type
748750
encrypt_schm = discovery_result.mgt_encrypt_schm
751+
749752
try:
750753
if not (encrypt_type := encrypt_schm.encrypt_type) and (
751754
encrypt_info := discovery_result.encrypt_info
752755
):
753756
encrypt_type = encrypt_info.sym_schm
757+
754758
if not encrypt_type:
755759
raise UnsupportedDeviceError(
756760
f"Unsupported device {config.host} of type {type_} "
@@ -771,19 +775,21 @@ def _get_device_instance(
771775
discovery_result=discovery_result.to_dict(),
772776
host=config.host,
773777
) from ex
778+
774779
if (
775780
device_class := get_device_class_from_family(
776781
type_, https=encrypt_schm.is_support_https
777782
)
778783
) is None:
779-
_LOGGER.warning("Got unsupported device type: %s", type_)
784+
_LOGGER.debug("Got unsupported device type: %s", type_)
780785
raise UnsupportedDeviceError(
781786
f"Unsupported device {config.host} of type {type_}: {info}",
782787
discovery_result=discovery_result.to_dict(),
783788
host=config.host,
784789
)
790+
785791
if (protocol := get_protocol(config)) is None:
786-
_LOGGER.warning(
792+
_LOGGER.debug(
787793
"Got unsupported connection type: %s", config.connection_type.to_dict()
788794
)
789795
raise UnsupportedDeviceError(
@@ -800,6 +806,7 @@ def _get_device_instance(
800806
else info
801807
)
802808
_LOGGER.debug("[DISCOVERY] %s << %s", config.host, pf(data))
809+
803810
device = device_class(config.host, protocol=protocol)
804811

805812
di = discovery_result.to_dict()

tests/discovery_fixtures.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
from dataclasses import dataclass
55
from json import dumps as json_dumps
6+
from typing import Any, TypedDict
67

78
import pytest
89

@@ -16,10 +17,21 @@
1617
DISCOVERY_MOCK_IP = "127.0.0.123"
1718

1819

19-
def _make_unsupported(device_family, encrypt_type, *, omit_keys=None):
20+
class DiscoveryResponse(TypedDict):
21+
result: dict[str, Any]
22+
error_code: int
23+
24+
25+
def _make_unsupported(
26+
device_family,
27+
encrypt_type,
28+
*,
29+
https: bool = False,
30+
omit_keys: dict[str, Any] | None = None,
31+
) -> DiscoveryResponse:
2032
if omit_keys is None:
2133
omit_keys = {"encrypt_info": None}
22-
result = {
34+
result: DiscoveryResponse = {
2335
"result": {
2436
"device_id": "xx",
2537
"owner": "xx",
@@ -31,7 +43,7 @@ def _make_unsupported(device_family, encrypt_type, *, omit_keys=None):
3143
"obd_src": "tplink",
3244
"factory_default": False,
3345
"mgt_encrypt_schm": {
34-
"is_support_https": False,
46+
"is_support_https": https,
3547
"encrypt_type": encrypt_type,
3648
"http_port": 80,
3749
"lv": 2,
@@ -51,6 +63,7 @@ def _make_unsupported(device_family, encrypt_type, *, omit_keys=None):
5163

5264
UNSUPPORTED_DEVICES = {
5365
"unknown_device_family": _make_unsupported("SMART.TAPOXMASTREE", "AES"),
66+
"unknown_iot_device_family": _make_unsupported("IOT.IOTXMASTREE", "AES"),
5467
"wrong_encryption_iot": _make_unsupported("IOT.SMARTPLUGSWITCH", "AES"),
5568
"wrong_encryption_smart": _make_unsupported("SMART.TAPOBULB", "IOT"),
5669
"unknown_encryption": _make_unsupported("IOT.SMARTPLUGSWITCH", "FOO"),
@@ -64,6 +77,11 @@ def _make_unsupported(device_family, encrypt_type, *, omit_keys=None):
6477
"FOO",
6578
omit_keys={"mgt_encrypt_schm": None},
6679
),
80+
"invalidinstance": _make_unsupported(
81+
"IOT.SMARTPLUGSWITCH",
82+
"KLAP",
83+
https=True,
84+
),
6785
}
6886

6987

tests/test_device_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,6 @@ async def test_device_types(dev: Device):
195195
async def test_device_class_from_unknown_family(caplog):
196196
"""Verify that unknown SMART devices yield a warning and fallback to SmartDevice."""
197197
dummy_name = "SMART.foo"
198-
with caplog.at_level(logging.WARNING):
198+
with caplog.at_level(logging.DEBUG):
199199
assert get_device_class_from_family(dummy_name, https=False) == SmartDevice
200200
assert f"Unknown SMART device with {dummy_name}" in caplog.text

0 commit comments

Comments
 (0)
0