2023-12-29 19:17:15 +00:00
|
|
|
"""Device creation via DeviceConfig."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-11-21 22:48:53 +00:00
|
|
|
import logging
|
|
|
|
import time
|
2024-04-17 13:39:24 +00:00
|
|
|
from typing import Any
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
from .aestransport import AesTransport
|
2024-02-04 15:20:08 +00:00
|
|
|
from .device import Device
|
2024-03-01 18:32:45 +00:00
|
|
|
from .device_type import DeviceType
|
2023-12-29 19:17:15 +00:00
|
|
|
from .deviceconfig import DeviceConfig
|
2024-02-21 15:52:55 +00:00
|
|
|
from .exceptions import KasaException, UnsupportedDeviceError
|
2024-10-16 15:53:52 +00:00
|
|
|
from .experimental.smartcamera import SmartCamera
|
|
|
|
from .experimental.smartcameraprotocol import SmartCameraProtocol
|
|
|
|
from .experimental.sslaestransport import SslAesTransport
|
2024-03-01 18:32:45 +00:00
|
|
|
from .iot import (
|
|
|
|
IotBulb,
|
|
|
|
IotDevice,
|
|
|
|
IotDimmer,
|
|
|
|
IotLightStrip,
|
|
|
|
IotPlug,
|
|
|
|
IotStrip,
|
|
|
|
IotWallSwitch,
|
|
|
|
)
|
2023-12-04 18:50:05 +00:00
|
|
|
from .iotprotocol import IotProtocol
|
2023-12-29 19:17:15 +00:00
|
|
|
from .klaptransport import KlapTransport, KlapTransportV2
|
|
|
|
from .protocol import (
|
2024-01-22 15:28:30 +00:00
|
|
|
BaseProtocol,
|
2023-12-29 19:17:15 +00:00
|
|
|
BaseTransport,
|
|
|
|
)
|
2024-04-29 17:19:44 +00:00
|
|
|
from .smart import SmartDevice
|
2023-12-04 18:50:05 +00:00
|
|
|
from .smartprotocol import SmartProtocol
|
2024-01-26 09:11:31 +00:00
|
|
|
from .xortransport import XorTransport
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-10-01 11:47:36 +00:00
|
|
|
GET_SYSINFO_QUERY: dict[str, dict[str, dict]] = {
|
|
|
|
"system": {"get_sysinfo": {}},
|
2023-12-29 19:17:15 +00:00
|
|
|
}
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
async def connect(*, host: str | None = None, config: DeviceConfig) -> Device:
|
2023-12-29 19:17:15 +00:00
|
|
|
"""Connect to a single device by the given hostname or device configuration.
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
This method avoids the UDP based discovery process and
|
2023-12-29 19:17:15 +00:00
|
|
|
will connect directly to the device.
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
It is generally preferred to avoid :func:`discover_single()` and
|
|
|
|
use this function instead as it should perform better when
|
|
|
|
the WiFi network is congested or the device is not responding
|
|
|
|
to discovery requests.
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
Do not use this function directly, use SmartDevice.connect()
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
:param host: Hostname of device to query
|
2023-12-29 19:17:15 +00:00
|
|
|
:param config: Connection parameters to ensure the correct protocol
|
|
|
|
and connection options are used.
|
2023-11-21 22:48:53 +00:00
|
|
|
:rtype: SmartDevice
|
|
|
|
:return: Object for querying/controlling found device.
|
|
|
|
"""
|
2023-12-29 19:17:15 +00:00
|
|
|
if host and config or (not host and not config):
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("One of host or config must be provded and not both")
|
2023-12-29 19:17:15 +00:00
|
|
|
if host:
|
|
|
|
config = DeviceConfig(host=host)
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-02-14 17:03:50 +00:00
|
|
|
if (protocol := get_protocol(config=config)) is None:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise UnsupportedDeviceError(
|
2024-02-14 17:03:50 +00:00
|
|
|
f"Unsupported device for {config.host}: "
|
2024-10-22 17:09:35 +00:00
|
|
|
+ f"{config.connection_type.device_family.value}",
|
|
|
|
host=config.host,
|
2024-02-14 17:03:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return await _connect(config, protocol)
|
|
|
|
except:
|
|
|
|
await protocol.close()
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
async def _connect(config: DeviceConfig, protocol: BaseProtocol) -> Device:
|
2023-12-29 19:17:15 +00:00
|
|
|
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
|
2023-11-21 22:48:53 +00:00
|
|
|
if debug_enabled:
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
def _perf_log(has_params, perf_type):
|
|
|
|
nonlocal start_time
|
2023-11-21 22:48:53 +00:00
|
|
|
if debug_enabled:
|
|
|
|
end_time = time.perf_counter()
|
|
|
|
_LOGGER.debug(
|
2024-08-30 14:13:14 +00:00
|
|
|
"Device %s with connection params %s took %.2f seconds to %s",
|
|
|
|
config.host,
|
|
|
|
has_params,
|
|
|
|
end_time - start_time,
|
|
|
|
perf_type,
|
2023-11-21 22:48:53 +00:00
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
start_time = time.perf_counter()
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
device_class: type[Device] | None
|
|
|
|
device: Device | None = None
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-01-26 09:11:31 +00:00
|
|
|
if isinstance(protocol, IotProtocol) and isinstance(
|
|
|
|
protocol._transport, XorTransport
|
|
|
|
):
|
2023-12-29 19:17:15 +00:00
|
|
|
info = await protocol.query(GET_SYSINFO_QUERY)
|
|
|
|
_perf_log(True, "get_sysinfo")
|
|
|
|
device_class = get_device_class_from_sys_info(info)
|
|
|
|
device = device_class(config.host, protocol=protocol)
|
|
|
|
device.update_from_discover_info(info)
|
|
|
|
await device.update()
|
|
|
|
_perf_log(True, "update")
|
|
|
|
return device
|
|
|
|
elif device_class := get_device_class_from_family(
|
2024-10-22 17:09:35 +00:00
|
|
|
config.connection_type.device_family.value, https=config.connection_type.https
|
2023-12-29 19:17:15 +00:00
|
|
|
):
|
|
|
|
device = device_class(host=config.host, protocol=protocol)
|
|
|
|
await device.update()
|
|
|
|
_perf_log(True, "update")
|
|
|
|
return device
|
|
|
|
else:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise UnsupportedDeviceError(
|
2023-12-29 19:17:15 +00:00
|
|
|
f"Unsupported device for {config.host}: "
|
2024-10-22 17:09:35 +00:00
|
|
|
+ f"{config.connection_type.device_family.value}",
|
|
|
|
host=config.host,
|
2023-11-21 22:48:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def _get_device_type_from_sys_info(info: dict[str, Any]) -> DeviceType:
|
2023-11-21 22:48:53 +00:00
|
|
|
"""Find SmartDevice subclass for device described by passed data."""
|
|
|
|
if "system" not in info or "get_sysinfo" not in info["system"]:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("No 'system' or 'get_sysinfo' in response")
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
sysinfo: dict[str, Any] = info["system"]["get_sysinfo"]
|
|
|
|
type_: str | None = sysinfo.get("type", sysinfo.get("mic_type"))
|
2023-11-21 22:48:53 +00:00
|
|
|
if type_ is None:
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Unable to find the device type field!")
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
if "dev_name" in sysinfo and "Dimmer" in sysinfo["dev_name"]:
|
2024-03-01 18:32:45 +00:00
|
|
|
return DeviceType.Dimmer
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
if "smartplug" in type_.lower():
|
|
|
|
if "children" in sysinfo:
|
2024-03-01 18:32:45 +00:00
|
|
|
return DeviceType.Strip
|
|
|
|
if (dev_name := sysinfo.get("dev_name")) and "light" in dev_name.lower():
|
|
|
|
return DeviceType.WallSwitch
|
|
|
|
return DeviceType.Plug
|
2023-11-21 22:48:53 +00:00
|
|
|
|
|
|
|
if "smartbulb" in type_.lower():
|
|
|
|
if "length" in sysinfo: # strips have length
|
2024-03-01 18:32:45 +00:00
|
|
|
return DeviceType.LightStrip
|
2023-11-21 22:48:53 +00:00
|
|
|
|
2024-03-01 18:32:45 +00:00
|
|
|
return DeviceType.Bulb
|
2024-02-21 15:52:55 +00:00
|
|
|
raise UnsupportedDeviceError("Unknown device type: %s" % type_)
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def get_device_class_from_sys_info(sysinfo: dict[str, Any]) -> type[IotDevice]:
|
2024-03-01 18:32:45 +00:00
|
|
|
"""Find SmartDevice subclass for device described by passed data."""
|
|
|
|
TYPE_TO_CLASS = {
|
|
|
|
DeviceType.Bulb: IotBulb,
|
|
|
|
DeviceType.Plug: IotPlug,
|
|
|
|
DeviceType.Dimmer: IotDimmer,
|
|
|
|
DeviceType.Strip: IotStrip,
|
|
|
|
DeviceType.WallSwitch: IotWallSwitch,
|
|
|
|
DeviceType.LightStrip: IotLightStrip,
|
|
|
|
}
|
|
|
|
return TYPE_TO_CLASS[_get_device_type_from_sys_info(sysinfo)]
|
|
|
|
|
|
|
|
|
2024-10-22 17:09:35 +00:00
|
|
|
def get_device_class_from_family(
|
|
|
|
device_type: str, *, https: bool
|
|
|
|
) -> type[Device] | None:
|
2023-12-04 18:50:05 +00:00
|
|
|
"""Return the device class from the type name."""
|
2024-04-17 13:39:24 +00:00
|
|
|
supported_device_types: dict[str, type[Device]] = {
|
2024-02-22 13:34:55 +00:00
|
|
|
"SMART.TAPOPLUG": SmartDevice,
|
2024-04-29 17:19:44 +00:00
|
|
|
"SMART.TAPOBULB": SmartDevice,
|
|
|
|
"SMART.TAPOSWITCH": SmartDevice,
|
2024-02-22 13:34:55 +00:00
|
|
|
"SMART.KASAPLUG": SmartDevice,
|
2024-02-22 22:09:38 +00:00
|
|
|
"SMART.TAPOHUB": SmartDevice,
|
2024-10-22 17:09:35 +00:00
|
|
|
"SMART.TAPOHUB.HTTPS": SmartCamera,
|
2024-04-22 14:24:15 +00:00
|
|
|
"SMART.KASAHUB": SmartDevice,
|
2024-04-29 17:19:44 +00:00
|
|
|
"SMART.KASASWITCH": SmartDevice,
|
2024-10-22 17:09:35 +00:00
|
|
|
"SMART.IPCAMERA.HTTPS": SmartCamera,
|
2024-02-04 15:20:08 +00:00
|
|
|
"IOT.SMARTPLUGSWITCH": IotPlug,
|
|
|
|
"IOT.SMARTBULB": IotBulb,
|
2023-12-04 18:50:05 +00:00
|
|
|
}
|
2024-10-22 17:09:35 +00:00
|
|
|
lookup_key = f"{device_type}{'.HTTPS' if https else ''}"
|
2024-04-25 06:36:30 +00:00
|
|
|
if (
|
2024-10-22 17:09:35 +00:00
|
|
|
cls := supported_device_types.get(lookup_key)
|
2024-04-25 06:36:30 +00:00
|
|
|
) is None and device_type.startswith("SMART."):
|
|
|
|
_LOGGER.warning("Unknown SMART device with %s, using SmartDevice", device_type)
|
|
|
|
cls = SmartDevice
|
|
|
|
|
|
|
|
return cls
|
2023-12-04 18:50:05 +00:00
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
def get_protocol(
|
|
|
|
config: DeviceConfig,
|
2024-04-17 13:39:24 +00:00
|
|
|
) -> BaseProtocol | None:
|
2023-12-04 18:50:05 +00:00
|
|
|
"""Return the protocol from the connection name."""
|
2023-12-29 19:17:15 +00:00
|
|
|
protocol_name = config.connection_type.device_family.value.split(".")[0]
|
2024-10-16 15:53:52 +00:00
|
|
|
ctype = config.connection_type
|
2023-12-29 19:17:15 +00:00
|
|
|
protocol_transport_key = (
|
2024-10-16 15:53:52 +00:00
|
|
|
protocol_name
|
|
|
|
+ "."
|
|
|
|
+ ctype.encryption_type.value
|
|
|
|
+ (".HTTPS" if ctype.https else "")
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2024-04-17 13:39:24 +00:00
|
|
|
supported_device_protocols: dict[
|
|
|
|
str, tuple[type[BaseProtocol], type[BaseTransport]]
|
2023-12-04 18:50:05 +00:00
|
|
|
] = {
|
2024-01-26 09:11:31 +00:00
|
|
|
"IOT.XOR": (IotProtocol, XorTransport),
|
2023-12-04 18:50:05 +00:00
|
|
|
"IOT.KLAP": (IotProtocol, KlapTransport),
|
|
|
|
"SMART.AES": (SmartProtocol, AesTransport),
|
2023-12-29 19:17:15 +00:00
|
|
|
"SMART.KLAP": (SmartProtocol, KlapTransportV2),
|
2023-12-04 18:50:05 +00:00
|
|
|
}
|
2024-10-16 15:53:52 +00:00
|
|
|
if not (prot_tran_cls := supported_device_protocols.get(protocol_transport_key)):
|
2024-10-29 09:30:30 +00:00
|
|
|
from .experimental import Experimental
|
2024-10-16 15:53:52 +00:00
|
|
|
|
2024-10-29 09:30:30 +00:00
|
|
|
if Experimental.enabled() and protocol_transport_key == "SMART.AES.HTTPS":
|
2024-10-16 15:53:52 +00:00
|
|
|
prot_tran_cls = (SmartCameraProtocol, SslAesTransport)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
return prot_tran_cls[0](transport=prot_tran_cls[1](config=config))
|