8000 Move transports into their own package (#1247) · python-kasa/python-kasa@668ba74 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 668ba74

Browse files
authored
Move transports into their own package (#1247)
This moves all transport implementations into a new `transports` package for cleaner main package & easier to understand project structure.
1 parent 71ae06f commit 668ba74

27 files changed

+159
-102
lines changed

devtools/parse_pcap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dpkt.ethernet import ETH_TYPE_IP, Ethernet
1010

1111
from kasa.cli.main import echo
12-
from kasa.xortransport import XorEncryption
12+
from kasa.transports.xortransport import XorEncryption
1313

1414

1515
def read_payloads_from_file(file):

devtools/parse_pcap_klap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
DeviceEncryptionType,
2626
DeviceFamily,
2727
)
28-
from kasa.klaptransport import KlapEncryptionSession, KlapTransportV2
2928
from kasa.protocol import DEFAULT_CREDENTIALS, get_default_credentials
29+
from kasa.transports.klaptransport import KlapEncryptionSession, KlapTransportV2
3030

3131

3232
def _get_seq_from_query(packet):

docs/source/reference.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,35 +107,35 @@
107107
```
108108

109109
```{eval-rst}
110-
.. autoclass:: kasa.protocol.BaseTransport
110+
.. autoclass:: kasa.transports.BaseTransport
111111
:members:
112112
:inherited-members:
113113
:undoc-members:
114114
```
115115

116116
```{eval-rst}
117-
.. autoclass:: kasa.xortransport.XorTransport
117+
.. autoclass:: kasa.transports.XorTransport
118118
:members:
119119
:inherited-members:
120120
:undoc-members:
121121
```
122122

123123
``` F438 {eval-rst}
124-
.. autoclass:: kasa.klaptransport.KlapTransport
124+
.. autoclass:: kasa.transports.KlapTransport
125125
:members:
126126
:inherited-members:
127127
:undoc-members:
128128
```
129129

130130
```{eval-rst}
131-
.. autoclass:: kasa.klaptransport.KlapTransportV2
131+
.. autoclass:: kasa.transports.KlapTransportV2
132132
:members:
133133
:inherited-members:
134134
:undoc-members:
135135
```
136136

137137
```{eval-rst}
138-
.. autoclass:: kasa.aestransport.AesTransport
138+
.. autoclass:: kasa.transports.AesTransport
139139
:members:
140140
:inherited-members:
141141
:undoc-members:

kasa/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
_deprecated_TPLinkSmartHomeProtocol, # noqa: F401
4242
)
4343
from kasa.module import Module
44-
from kasa.protocol import BaseProtocol, BaseTransport
44+
from kasa.protocol import BaseProtocol
4545
from kasa.smartprotocol import SmartProtocol
46+
from kasa.transports import BaseTransport
4647

4748
__version__ = version("python-kasa")
4849

kasa/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
from .iotprotocol import IotProtocol
129129
from .module import Module
130130
from .protocol import BaseProtocol
131-
from .xortransport import XorTransport
131+
from .transports import XorTransport
132132

133133
if TYPE_CHECKING:
134134
from .modulemapping import ModuleMapping, ModuleName

kasa/device_factory.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import time
77
from typing import Any
88

9-
from .aestransport import AesTransport
109
from .device import Device
1110
from .device_type import DeviceType
1211
from .deviceconfig import DeviceConfig
@@ -24,14 +23,18 @@
2423
IotWallSwitch,
2524
)
2625
from .iotprotocol import IotProtocol
27-
from .klaptransport import KlapTransport, KlapTransportV2
2826
from .protocol import (
2927
BaseProtocol,
30-
BaseTransport,
3128
)
3229
from .smart import SmartDevice
3330
from .smartprotocol import SmartProtocol
34-
from .xortransport import XorTransport
31+
from .transports import (
32+
AesTransport,
33+
BaseTransport,
34+
KlapTransport,
35+
KlapTransportV2,
36+
XorTransport,
37+
)
3538

3639
_LOGGER = logging.getLogger(__name__)
3740

kasa/discover.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
from pydantic.v1 import BaseModel, ValidationError
112112

113113
from kasa import Device
114-
from kasa.aestransport import AesEncyptionSession, KeyPair
115114
from kasa.credentials import Credentials
116115
from kasa.device_factory import (
117116
get_device_class_from_family,
@@ -134,12 +133,14 @@
134133
from kasa.json import dumps as json_dumps
135134
from kasa.json import loads as json_loads
136135
from kasa.protocol import mask_mac, redact_data
137-
from kasa.xortransport import XorEncryption
136+
from kasa.transports.aestransport import AesEncyptionSession, KeyPair
137+
from kasa.transports.xortransport import XorEncryption
138138

139139
_LOGGER = logging.getLogger(__name__)
140140

141141
if TYPE_CHECKING:
142-
from kasa import BaseProtocol, BaseTransport
142+
from kasa import BaseProtocol
143+
from kasa.transports import BaseTransport
143144

144145

145146
class ConnectAttempt(NamedTuple):

kasa/experimental/sslaestransport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from yarl import URL
1515

16-
from ..aestransport import AesEncyptionSession
1716
from ..credentials import Credentials
1817
from ..deviceconfig import DeviceConfig
1918
from ..exceptions import (
@@ -28,7 +27,8 @@
2827
from ..httpclient import HttpClient
2928
from ..json import dumps as json_dumps
3029
from ..json import loads as json_loads
31-
from ..protocol import DEFAULT_CREDENTIALS, BaseTransport, get_default_credentials
30+
from ..protocol import DEFAULT_CREDENTIALS, get_default_credentials
31+
from ..transports import AesEncyptionSession, BaseTransport
3232

3333
_LOGGER = logging.getLogger(__name__)
3434

kasa/iotprotocol.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import asyncio
66
import logging
77
from pprint import pformat as pf
8-
from typing import Any, Callable
8+
from typing import TYPE_CHECKING, Any, Callable
99

1010
from .deviceconfig import DeviceConfig
1111
from .exceptions import (
@@ -16,8 +16,11 @@
1616
_RetryableError,
1717
)
1818
from .json import dumps as json_dumps
19-
from .protocol import BaseProtocol, BaseTransport, mask_mac, redact_data
20-
from .xortransport import XorEncryption, XorTransport
19+
from .protocol import BaseProtocol, mask_mac, redact_data
20+
from .transports import XorEncryption, XorTransport
21+
22+
if TYPE_CHECKING:
23+
from .transports import BaseTransport
2124

2225
_LOGGER = logging.getLogger(__name__)
2326

kasa/protocol.py

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import logging
1919
import struct
2020
from abc import ABC, abstractmethod
21-
from typing import Any, Callable, TypeVar, cast
21+
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast
2222

2323
# When support for cpython older than 3.11 is dropped
2424
# async_timeout can be replaced with asyncio.timeout
@@ -32,6 +32,10 @@
3232
_T = TypeVar("_T")
3333

3434

35+
if TYPE_CHECKING:
36+
from .transports import BaseTransport
37+
38+
3539
def redact_data(data: _T, redactors: dict[str, Callable[[Any], Any] | None]) -> _T:
3640
"""Redact sensitive data for logging."""
3741
if not isinstance(data, (dict, list)):
@@ -75,49 +79,6 @@ def md5(payload: bytes) -> bytes:
7579
return hashlib.md5(payload).digest() # noqa: S324
7680

7781

78-
class BaseTransport(ABC):
79-
"""Base class for all TP-Link protocol transports."""
80-
81-
DEFAULT_TIMEOUT = 5
82-
83-
def __init__(
84-
self,
85-
*,
86-
config: DeviceConfig,
87-
) -> None:
88-
"""Create a protocol object."""
89-
self._config = config
90-
self._host = config.host
91-
self._port = config.port_override or self.default_port
92-
self._credentials = config.credentials
93-
self._credentials_hash = config.credentials_hash
94-
if not config.timeout:
95-
config.timeout = self.DEFAULT_TIMEOUT
96-
self._timeout = config.timeout
97-
98-
@property
99-
@abstractmethod
100-
def default_port(self) -> int:
101-
"""The default port for the transport."""
102-
103-
@property
104-
@abstractmethod
105-
def credentials_hash(self) -> str | None:
106-
"""The hashed credentials used by the transport."""
107-
108-
@abstractmethod
109-
async def send(self, request: str) -> dict:
110-
"""Send a message to the device and return a response."""
111-
112-
@abstractmethod
113-
async def close(self) -> None:
114-
"""Close the transport. Abstract method to be overriden."""
115-
116-
@abstractmethod
117-
async def reset(self) -> None:
118-
"""Reset internal state."""
119-
120-
12182
class BaseProtocol(ABC):
12283
"""Base class for all TP-Link Smart Home communication."""
12384

kasa/smart/smartdevice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from datetime import datetime, timedelta, timezone, tzinfo
1010
from typing import TYPE_CHECKING, Any, cast
1111

12-
from ..aestransport import AesTransport
1312
from ..device import Device, WifiNetwork
1413
from ..device_type import DeviceType
1514
from ..deviceconfig import DeviceConfig
@@ -18,6 +17,7 @@
1817
from ..module import Module
1918
from ..modulemapping import ModuleMapping, ModuleName
2019
from ..smartprotocol import SmartProtocol
20+
from ..transports import AesTransport
2121
from .modules import (
2222
ChildDevice,
2323
Cloud,

kasa/smartprotocol.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import time
1313
import uuid
1414
from pprint import pformat as pf
15-
from typing import Any, Callable
15+
from typing import TYPE_CHECKING, Any, Callable
1616

1717
from .exceptions import (
1818
SMART_AUTHENTICATION_ERRORS,
@@ -26,7 +26,11 @@
2626
_RetryableError,
2727
)
2828
from .json import dumps as json_dumps
29-
from .protocol import BaseProtocol, BaseTransport, mask_mac, md5, redact_data
29+
from .protocol import BaseProtocol, mask_mac, md5, redact_data
30+
31+
if TYPE_CHECKING:
32+
from .transports import BaseTransport
33+
3034

3135
_LOGGER = logging.getLogger(__name__)
3236

kasa/transports/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Package containing all supported transports."""
2+
3+
from .aestransport import AesEncyptionSession, AesTransport
4+
from .basetransport import BaseTransport
5+
from .klaptransport import KlapTransport, KlapTransportV2
6+
from .xortransport import XorEncryption, XorTransport
7+
8+
__all__ = [
9+
"AesTransport",
10+
"AesEncyptionSession",
11+
"BaseTransport",
12+
"KlapTransport",
13+
"KlapTransportV2",
14+
"XorTransport",
15+
"XorEncryption",
16+
]

kasa/aestransport.py renamed to kasa/transports/aestransport.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
2121
from yarl import URL
2222

23-
from .credentials import Credentials
24-
from .deviceconfig import DeviceConfig
25-
from .exceptions import (
23+
from kasa.credentials import Credentials
24+
from kasa.deviceconfig import DeviceConfig
25+
from kasa.exceptions import (
2626
SMART_AUTHENTICATION_ERRORS,
2727
SMART_RETRYABLE_ERRORS,
2828
AuthenticationError,
@@ -33,10 +33,12 @@
3333
_ConnectionError,
3434
_RetryableError,
3535
)
36-
from .httpclient import HttpClient
37-
from .json import dumps as json_dumps
38-
from .json import loads as json_loads
39-
from .protocol import DEFAULT_CREDENTIALS, BaseTransport, get_default_credentials
36+
from kasa.httpclient import HttpClient
37+
from kasa.json import dumps as json_dumps
38+
from kasa.json import loads as json_loads
39+
from kasa.protocol import DEFAULT_CREDENTIALS, get_default_credentials
40+
41+
from .basetransport import BaseTransport
4042

4143
_LOGGER = logging.getLogger(__name__)
4244

kasa/transports/basetransport.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Base class for all transport implementations.
2+
3+
All transport classes must derive from this to implement t 48DA he common interface.
4+
"""
5+
6+
from __future__ import annotations
7+
8+
from abc import ABC, abstractmethod
9+
from typing import TYPE_CHECKING
10+
11+
if TYPE_CHECKING:
12+
from kasa import DeviceConfig
13+
14+
15+
class BaseTransport(ABC):
16+
"""Base class for all TP-Link protocol transports."""
17+
18+
DEFAULT_TIMEOUT = 5
19+
20+
def __init__(
21+
self,
22+
*,
23+
config: DeviceConfig,
24+
) -> None:
25+
"""Create a protocol object."""
26+
self._config = config
27+
self._host = config.host
28+
self._port = config.port_override or self.default_port
29+
self._credentials = config.credentials
30+
self._credentials_hash = config.credentials_hash
31+
if not config.timeout:
32+
config.timeout = self.DEFAULT_TIMEOUT
33+
self._timeout = config.timeout
34+
35+
@property
36+
@abstractmethod
37+
def default_port(self) -> int:
38+
"""The default port for the transport."""
39+
40+
@property
41+
@abstractmethod
42+
def credentials_hash(self) -> str | None:
43+
"""The hashed credentials used by the transport."""
44+
45+
@abstractmethod
46+
async def send(self, request: str) -> dict:
47+
"""Send a message to the device and return a response."""
48+
49+
@abstractmethod
50+
async def close(self) -> None:
51+
"""Close the transport. Abstract method to be overriden."""
52+
53+
@abstractmethod
54+
async def reset(self) -> None:
55+
"""Reset internal state."""

0 commit comments

Comments
 (0)
0