mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Add klap support for TAPO protocol by splitting out Transports and Protocols (#557)
* Add support for TAPO/SMART KLAP and seperate transports from protocols * Add tests and some review changes * Update following review * Updates following review
This commit is contained in:
@@ -15,18 +15,18 @@ try:
|
||||
except ImportError:
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from kasa.aesprotocol import TPLinkAes
|
||||
from kasa.credentials import Credentials
|
||||
from kasa.exceptions import UnsupportedDeviceException
|
||||
from kasa.json import dumps as json_dumps
|
||||
from kasa.json import loads as json_loads
|
||||
from kasa.klapprotocol import TPLinkKlap
|
||||
from kasa.protocol import TPLinkProtocol, TPLinkSmartHomeProtocol
|
||||
from kasa.protocol import TPLinkSmartHomeProtocol
|
||||
from kasa.smartdevice import SmartDevice, SmartDeviceException
|
||||
from kasa.smartplug import SmartPlug
|
||||
from kasa.tapo.tapoplug import TapoPlug
|
||||
|
||||
from .device_factory import get_device_class_from_info
|
||||
from .device_factory import (
|
||||
get_device_class_from_sys_info,
|
||||
get_device_class_from_type_name,
|
||||
get_protocol_from_connection_name,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -348,7 +348,16 @@ class Discover:
|
||||
@staticmethod
|
||||
def _get_device_class(info: dict) -> Type[SmartDevice]:
|
||||
"""Find SmartDevice subclass for device described by passed data."""
|
||||
return get_device_class_from_info(info)
|
||||
if "result" in info:
|
||||
discovery_result = DiscoveryResult(**info["result"])
|
||||
dev_class = get_device_class_from_type_name(discovery_result.device_type)
|
||||
if not dev_class:
|
||||
raise UnsupportedDeviceException(
|
||||
"Unknown device type: %s" % discovery_result.device_type
|
||||
)
|
||||
return dev_class
|
||||
else:
|
||||
return get_device_class_from_sys_info(info)
|
||||
|
||||
@staticmethod
|
||||
def _get_device_instance_legacy(data: bytes, ip: str, port: int) -> SmartDevice:
|
||||
@@ -384,24 +393,17 @@ class Discover:
|
||||
encrypt_type_ = (
|
||||
f"{type_.split('.')[0]}.{discovery_result.mgt_encrypt_schm.encrypt_type}"
|
||||
)
|
||||
device_class = None
|
||||
|
||||
supported_device_types: dict[str, Type[SmartDevice]] = {
|
||||
"SMART.TAPOPLUG": TapoPlug,
|
||||
"SMART.KASAPLUG": TapoPlug,
|
||||
"IOT.SMARTPLUGSWITCH": SmartPlug,
|
||||
}
|
||||
supported_device_protocols: dict[str, Type[TPLinkProtocol]] = {
|
||||
"IOT.KLAP": TPLinkKlap,
|
||||
"SMART.AES": TPLinkAes,
|
||||
}
|
||||
|
||||
if (device_class := supported_device_types.get(type_)) is None:
|
||||
if (device_class := get_device_class_from_type_name(type_)) is None:
|
||||
_LOGGER.warning("Got unsupported device type: %s", type_)
|
||||
raise UnsupportedDeviceException(
|
||||
f"Unsupported device {ip} of type {type_}: {info}"
|
||||
)
|
||||
if (protocol_class := supported_device_protocols.get(encrypt_type_)) is None:
|
||||
if (
|
||||
protocol := get_protocol_from_connection_name(
|
||||
encrypt_type_, ip, credentials=credentials
|
||||
)
|
||||
) is None:
|
||||
_LOGGER.warning("Got unsupported device type: %s", encrypt_type_)
|
||||
raise UnsupportedDeviceException(
|
||||
f"Unsupported encryption scheme {ip} of type {encrypt_type_}: {info}"
|
||||
@@ -409,7 +411,7 @@ class Discover:
|
||||
|
||||
_LOGGER.debug("[DISCOVERY] %s << %s", ip, info)
|
||||
device = device_class(ip, port=port, credentials=credentials)
|
||||
device.protocol = protocol_class(ip, credentials=credentials)
|
||||
device.protocol = protocol
|
||||
device.update_from_discover_info(discovery_result.get_dict())
|
||||
return device
|
||||
|
||||
|
Reference in New Issue
Block a user