10000 Update get_device_instance and test_unsupported following review · python-kasa/python-kasa@3964415 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3964415

Browse files
committed
Update get_device_instance and test_unsupported following review
1 parent 1a63981 commit 3964415

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

kasa/aesprotocol.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def __init__(
6464
*,
6565
credentials: Optional[Credentials] = None,
6666
timeout: Optional[int] = None,
67-
login_version: Optional[int] = 1,
6867
) -> None:
6968
super().__init__(host=host, port=self.DEFAULT_PORT)
7069

@@ -91,7 +90,6 @@ def __init__(
9190
self.terminal_uuid = None
9291
self.http_client: Optional[httpx.AsyncClient] = None
9392
self.request_id_generator = SnowflakeId(1, 1)
94-
self.login_version = login_version
9593
self.login_token = None
9694

9795
_LOGGER.debug("Created AES object for %s", self.host)

kasa/discover.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from kasa.json import dumps as json_dumps
2222
from kasa.json import loads as json_loads
2323
from kasa.klapprotocol import TPLinkKlap
24-
from kasa.protocol import TPLinkSmartHomeProtocol
24+
from kasa.protocol import TPLinkProtocol, TPLinkSmartHomeProtocol
2525
from kasa.smartdevice import SmartDevice, SmartDeviceException
2626
from kasa.smartplug import SmartPlug
2727
from kasa.tapo.tapoplug import TapoPlug
@@ -380,37 +380,38 @@ def _get_device_instance(
380380
f"Unable to read response from device: {ip}: {ex}"
381381
) from ex
382382

383-
if discovery_result.mgt_encrypt_schm.encrypt_type in ("KLAP", "AES"):
384-
type_ = discovery_result.device_type
385-
device_class = None
386-
supported_device_types: dict[str, Type[SmartDevice]] = {
387-
"SMART.TAPOPLUG": TapoPlug,
388-
"SMART.KASAPLUG": TapoPlug,
389-
"IOT.SMARTPLUGSWITCH": SmartPlug,
390-
}
391-
392-
if discovery_result.device_type in supported_device_types:
393-
device_class = supported_device_types[discovery_result.device_type]
394-
395-
if device_class:
396-
_LOGGER.debug("[DISCOVERY] %s << %s", ip, info)
397-
device = device_class(ip, port=port, credentials=credentials)
398-
device.update_from_discover_info(discovery_result.get_dict())
399-
if discovery_result.mgt_encrypt_schm.encrypt_type == "KLAP":
400-
device.protocol = TPLinkKlap(ip, credentials=credentials)
401-
else:
402-
device.protocol = TPLinkAes(
403-
ip,
404-
credentials=credentials,
405-
login_version=discovery_result.mgt_encrypt_schm.lv,
406-
)
407-
return device
408-
else:
409-
raise UnsupportedDeviceException(
410-
f"Unsupported device {ip} of type {type_}: {info}"
411-
)
412-
else:
413-
raise UnsupportedDeviceException(f"Unsupported device {ip}: {info}")
383+
type_ = discovery_result.device_type
384+
encrypt_type_ = (
385+
f"{type_.split('.')[0]}.{discovery_result.mgt_encrypt_schm.encrypt_type}"
386+
)
387+
device_class = None
388+
389+
supported_device_types: dict[str, Type[SmartDevice]] = {
390+
"SMART.TAPOPLUG": TapoPlug,
391+
"SMART.KASAPLUG": TapoPlug,
392+
"IOT.SMARTPLUGSWITCH": SmartPlug,
393+
}
394+
supported_device_protocols: dict[str, Type[TPLinkProtocol]] = {
395+
"IOT.KLAP": TPLinkKlap,
396+
"SMART.AES": TPLinkAes,
397+
}
398+
399+
if (device_class := supported_device_types.get(type_)) is None:
400+
_LOGGER.warning("Got unsupported device type: %s", type_)
401+
raise UnsupportedDeviceException(
402+
f"Unsupported device {ip} of type {type_}: {info}"
403+
)
404+
if (protocol_class := supported_device_protocols.get(encrypt_type_)) is None:
405+
_LOGGER.warning("Got unsupported device type: %s", encrypt_type_)
406+
raise UnsupportedDeviceException(
407+
f"Unsupported encryption scheme {ip} of type {encrypt_type_}: {info}"
408+
)
409+
410+
_LOGGER.debug("[DISCOVERY] %s << %s", ip, info)
411+
device = device_class(ip, port=port, credentials=credentials)
412+
device.protocol = protocol_class(ip, credentials=credentials)
413+
device.update_from_discover_info(discovery_result.get_dict())
414+
return device
414415

415416

416417
class DiscoveryResult(BaseModel):

kasa/tests/test_discovery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def mock_discover(self):
114114
"result": {
115115
"device_id": "xx",
116116
"owner": "xx",
117-
"device_type": "SMART.TAPOBULB",
117+
"device_type": "SMART.TAPOXMASTREE",
118118
"device_model": "P110(EU)",
119119
"ip": "127.0.0.1",
120120
"mac": "48-22xxx",
@@ -150,7 +150,7 @@ def mock_discover(self):
150150
discovery_data = UNSUPPORTED
151151
with pytest.raises(
152152
UnsupportedDeviceException,
153-
match=f"Unsupported device {host} of type SMART.TAPOBULB: {re.escape(str(UNSUPPORTED))}",
153+
match=f"Unsupported device {host} of type SMART.TAPOXMASTREE: {re.escape(str(UNSUPPORTED))}",
154154
):
155155
await Discover.discover_single(host)
156156

0 commit comments

Comments
 (0)
0