8000 Update post review · python-kasa/python-kasa@ad73e58 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad73e58

Browse files
committed
Update post review
1 parent 0b4dad7 commit ad73e58

25 files changed

+233
-226
lines changed

kasa/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
"""
1414
from importlib.metadata import version
1515

16-
from kasa.connectionparams import (
17-
ConnectionParameters,
16+
from kasa.credentials import Credentials
17+
from kasa.deviceconfig import (
1818
ConnectionType,
19+
DeviceConfig,
1920
DeviceFamilyType,
2021
EncryptType,
2122
)
22-
from kasa.credentials import Credentials
2323
from kasa.discover import Discover
2424
from kasa.emeterstatus import EmeterStatus
2525
from kasa.exceptions import (
@@ -61,7 +61,7 @@
6161
"AuthenticationException",
6262
"UnsupportedDeviceException",
6363
"Credentials",
64-
"ConnectionParameters",
64+
"DeviceConfig",
6565
"ConnectionType",
6666
"EncryptType",
6767
"DeviceFamilyType",

kasa/aestransport.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from cryptography.hazmat.primitives.asymmetric import rsa
1717
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
1818

19-
from .connectionparams import ConnectionParameters
19+
from .deviceconfig import DeviceConfig
2020
from .exceptions import (
2121
SMART_AUTHENTICATION_ERRORS,
2222
SMART_RETRYABLE_ERRORS,
@@ -58,14 +58,13 @@ class AesTransport(BaseTransport):
5858
def __init__(
5959
self,
6060
*,
61-
cparams: ConnectionParameters,
61+
config: DeviceConfig,
6262
) -> None:
63-
super().__init__(cparams=cparams)
64-
self._port = cparams.port or self.DEFAULT_PORT
63+
super().__init__(config=config)
64+
self._port = config.port or self.DEFAULT_PORT
65+
66+
self._default_http_client: Optional[httpx.AsyncClient] = None
6567

66-
self._http_client: httpx.AsyncClient = (
67-
cparams.http_client or httpx.AsyncClient()
68-
)
6968
self._handshake_done = False
7069

7170
self._encryption_session: Optional[AesEncyptionSession] = None
@@ -77,6 +76,14 @@ def __init__(
7776

7877
_LOGGER.debug("Created AES transport for %s", self._host)
7978

79+
@property
80+
def _http_client(self) -> httpx.AsyncClient:
81+
if self._config.http_client:
82+
return self._config.http_client
83+
if not self._default_http_client:
84+
self._default_http_client = httpx.AsyncClient()
85+
return self._default_http_client
86+
8087
def hash_credentials(self, login_v2):
8188
"""Hash the credentials."""
8289
if login_v2:
@@ -259,8 +266,8 @@ async def send(self, request: str):
259266

260267
async def close(self) -> None:
261268
"""Close the protocol."""
262-
client = self._http_client
263-
self._http_client = None
269+
client = self._default_http_client
270+
self._default_http_client = None
264271
self._handshake_done = False
265272
self._login_token = None
266273
if client:

kasa/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
from kasa import (
1414
AuthenticationException,
15-
ConnectionParameters,
1615
ConnectionType,
1716
Credentials,
17+
DeviceConfig,
1818
DeviceFamilyType,
1919
Discover,
2020
EncryptType,
@@ -286,10 +286,10 @@ def _nop_echo(*args, **kwargs):
286286
DeviceFamilyType(device_family),
287287
EncryptType(encrypt_type),
288288
)
289-
cparams = ConnectionParameters(
289+
config = DeviceConfig(
290290
host=host, credentials=credentials, timeout=timeout, connection_type=ctype
291291
)
292-
dev = await SmartDevice.connect(cparams=cparams)
292+
dev = await SmartDevice.connect(config=config)
293293
else:
294294
echo("No --type or --device-family and --encrypt-type defined, discovering..")
295295
dev = await Discover.discover_single(

kasa/credentials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
class Credentials:
99
"""Credentials for authentication."""
1010

11-
username: Optional[str] = field(default=None, repr=False)
12-
password: Optional[str] = field(default=None, repr=False)
11+
username: Optional[str] = field(default="", repr=False)
12+
password: Optional[str] = field(default="", repr=False)

kasa/device_factory.py

100644100755
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"""Device creation via ConnectionParameters."""
1+
"""Device creation via DeviceConfig."""
22
import logging
33
import time
44
from typing import Any, Dict, Optional, Type
55

6-
from kasa.connectionparams import ConnectionParameters
6+
from kasa.deviceconfig import DeviceConfig
77
from kasa.protocol import TPLinkSmartHomeProtocol
88
from kasa.smartbulb import SmartBulb
99
from kasa.smartdevice import SmartDevice
@@ -23,7 +23,7 @@
2323
}
2424

2525

26-
async def connect(*, cparams: ConnectionParameters) -> "SmartDevice":
26+
async def connect(*, config: DeviceConfig) -> "SmartDevice":
2727
"""Connect to a single device by the given connection parameters.
2828
2929
Do not use this function directly, use SmartDevice.Connect()
@@ -37,15 +37,15 @@ def _perf_log(has_params, perf_type):
3737
if debug_enabled:
3838
end_time = time.perf_counter()
3939
_LOGGER.debug(
40-
f"Device {cparams.host} with connection params {has_params} "
40+
f"Device {config.host} with connection params {has_params} "
4141
+ f"took {end_time - start_time:.2f} seconds to {perf_type}",
4242
)
4343
start_time = time.perf_counter()
4444

45-
if (protocol := get_protocol(cparams=cparams)) is None:
45+
if (protocol := get_protocol(config=config)) is None:
4646
raise UnsupportedDeviceException(
47-
f"Unsupported device for {cparams.host}: "
48-
+ f"{cparams.connection_type.device_family.value}"
47+
f"Unsupported device for {config.host}: "
48+
+ f"{config.connection_type.device_family.value}"
4949
)
5050

5151
device_class: Optional[Type[SmartDevice]]
@@ -54,29 +54,29 @@ def _perf_log(has_params, perf_type):
5454
info = await protocol.query(GET_SYSINFO_QUERY)
5555
_perf_log(True, "get_sysinfo")
5656
device_class = get_device_class_from_sys_info(info)
57-
device = device_class(cparams.host, port=cparams.port, timeout=cparams.timeout)
57+
device = device_class(config.host, port=config.port, timeout=config.timeout)
5858
device.update_from_discover_info(info)
5959
device.protocol = protocol
6060
await device.update()
6161
_perf_log(True, "update")
6262
return device
6363
elif device_class := get_device_class_from_family(
64-
cparams.connection_type.device_family.value
64+
config.connection_type.device_family.value
6565
):
6666
device = device_class(
67-
cparams.host,
68-
port=cparams.port,
69-
timeout=cparams.timeout,
70-
credentials=cparams.credentials,
67+
config.host,
68+
port=config.port,
69+
timeout=config.timeout,
70+
credentials=config.credentials,
7171
)
7272
device.protocol = protocol
7373
await device.update()
7474
_perf_log(True, "update")
7575
return device
7676
else:
7777
raise UnsupportedDeviceException(
78-
f"Unsupported device for {cparams.host}: "
79-
+ f"{cparams.connection_type.device_family.value}"
78+
f"Unsupported device for {config.host}: "
79+
+ f"{config.connection_type.device_family.value}"
8080
)
8181

8282

kasa/device_type.py

100644100755
File mode changed.

kasa/connectionparams.py renamed to kasa/deviceconfig.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def to_dict(self) -> Dict[str, str]:
109109

110110

111111
@dataclass
112-
class ConnectionParameters:
112+
class DeviceConfig:
113113
"""Class to represent paramaters that determine how to connect to devices."""
114114

115115
DEFAULT_TIMEOUT = 5
@@ -126,6 +126,7 @@ class ConnectionParameters:
126126
)
127127
)
128128

129+
uses_http: bool = False
129130
# compare=False will be excluded from the serialization and object comparison.
130131
http_client: Optional[httpx.AsyncClient] = field(default=None, compare=False)
131132

@@ -142,6 +143,6 @@ def to_dict(self) -> Dict[str, Dict[str, str]]:
142143
return _dataclass_to_dict(self)
143144

144145
@staticmethod
145-
def from_dict(cparam_dict: Dict[str, Dict[str, str]]) -> "ConnectionParameters":
146+
def from_dict(cparam_dict: Dict[str, Dict[str, str]]) -> "DeviceConfig":
146147
"""Return connection parameters from dict."""
147-
return _dataclass_from_dict(ConnectionParameters, cparam_dict)
148+
return _dataclass_from_dict(DeviceConfig, cparam_dict)

0 commit comments

Comments
 (0)
0