Do not regenerate aes key pair (#1114)

And read it from `device_config` if provided.

This is required as key generation can eat up cpu when a device is not fully available and the library is retrying.
This commit is contained in:
Steven B.
2024-09-10 17:24:38 +01:00
committed by GitHub
parent 2a89e58ae0
commit fcf8f07232
3 changed files with 46 additions and 4 deletions

View File

@@ -34,7 +34,7 @@ Living Room Bulb
import logging
from dataclasses import asdict, dataclass, field, fields, is_dataclass
from enum import Enum
from typing import TYPE_CHECKING, Dict, Optional, Union
from typing import TYPE_CHECKING, Dict, Optional, TypedDict, Union
from .credentials import Credentials
from .exceptions import KasaException
@@ -45,6 +45,13 @@ if TYPE_CHECKING:
_LOGGER = logging.getLogger(__name__)
class KeyPairDict(TypedDict):
"""Class to represent a public/private key pair."""
private: str
public: str
class DeviceEncryptionType(Enum):
"""Encrypt type enum."""
@@ -182,7 +189,7 @@ class DeviceConfig:
#: The batch size for protoools supporting multiple request batches.
connection_type: DeviceConnectionParameters = field(
default_factory=lambda: DeviceConnectionParameters(
DeviceFamily.IotSmartPlugSwitch, DeviceEncryptionType.Xor, 1
DeviceFamily.IotSmartPlugSwitch, DeviceEncryptionType.Xor
)
)
#: True if the device uses http. Consumers should retrieve rather than set this
@@ -193,6 +200,8 @@ class DeviceConfig:
#: Set a custom http_client for the device to use.
http_client: Optional["ClientSession"] = field(default=None, compare=False)
aes_keys: Optional[KeyPairDict] = None
def __post_init__(self):
if self.connection_type is None:
self.connection_type = DeviceConnectionParameters(