Skip to content

Add DeviceConfig to allow specifying configuration parameters #569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/source/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ This will return you a list of device instances based on the discovery replies.
If the device's host is already known, you can use to construct a device instance with
:meth:`~kasa.SmartDevice.connect()`.

When connecting a device with the :meth:`~kasa.SmartDevice.connect()` method, it is recommended to
pass the device type as well as this allows the library to use the correct device class for the
device without having to query the device.
The :meth:`~kasa.SmartDevice.connect()` also enables support for connecting to new
KASA SMART protocol and TAPO devices directly using the parameter :class:`~kasa.DeviceConfig`.
Simply serialize the :attr:`~kasa.SmartDevice.config` property via :meth:`~kasa.DeviceConfig.to_dict()`
and then deserialize it later with :func:`~kasa.DeviceConfig.from_dict()`
and then pass it into :meth:`~kasa.SmartDevice.connect()`.


.. _update_cycle:

Expand Down
18 changes: 18 additions & 0 deletions docs/source/deviceconfig.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DeviceConfig
============

.. contents:: Contents
:local:

.. note::

Feel free to open a pull request to improve the documentation!


API documentation
*****************

.. autoclass:: kasa.DeviceConfig
:members:
:inherited-members:
:undoc-members:
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
smartdimmer
smartstrip
smartlightstrip
deviceconfig
10 changes: 10 additions & 0 deletions kasa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
from importlib.metadata import version

from kasa.credentials import Credentials
from kasa.deviceconfig import (
ConnectionType,
DeviceConfig,
DeviceFamilyType,
EncryptType,
)
from kasa.discover import Discover
from kasa.emeterstatus import EmeterStatus
from kasa.exceptions import (
Expand Down Expand Up @@ -55,4 +61,8 @@
"AuthenticationException",
"UnsupportedDeviceException",
"Credentials",
"DeviceConfig",
"ConnectionType",
"EncryptType",
"DeviceFamilyType",
]
40 changes: 21 additions & 19 deletions kasa/aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

from .credentials import Credentials
from .deviceconfig import DeviceConfig
from .exceptions import (
SMART_AUTHENTICATION_ERRORS,
SMART_RETRYABLE_ERRORS,
Expand Down Expand Up @@ -47,8 +47,7 @@ class AesTransport(BaseTransport):
protocol, sometimes used by newer firmware versions on kasa devices.
"""

DEFAULT_PORT = 80
DEFAULT_TIMEOUT = 5
DEFAULT_PORT: int = 80
SESSION_COOKIE_NAME = "TP_SESSIONID"
COMMON_HEADERS = {
"Content-Type": "application/json",
Expand All @@ -58,32 +57,37 @@ class AesTransport(BaseTransport):

def __init__(
self,
host: str,
*,
port: Optional[int] = None,
credentials: Optional[Credentials] = None,
timeout: Optional[int] = None,
config: DeviceConfig,
) -> None:
super().__init__(
host,
port=port or self.DEFAULT_PORT,
credentials=credentials,
timeout=timeout,
)
super().__init__(config=config)

self._default_http_client: Optional[httpx.AsyncClient] = None

self._handshake_done = False

self._encryption_session: Optional[AesEncyptionSession] = None
self._session_expire_at: Optional[float] = None

self._timeout = timeout if timeout else self.DEFAULT_TIMEOUT
self._session_cookie = None

self._http_client: httpx.AsyncClient = httpx.AsyncClient()
self._login_token = None

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

@property
def default_port(self):
"""Default port for the transport."""
return self.DEFAULT_PORT

@property
def _http_client(self) -> httpx.AsyncClient:
if self._config.http_client:
return self._config.http_client
if not self._default_http_client:
self._default_http_client = httpx.AsyncClient()
return self._default_http_client

def hash_credentials(self, login_v2):
"""Hash the credentials."""
if login_v2:
Expand All @@ -102,8 +106,6 @@ def hash_credentials(self, login_v2):

async def client_post(self, url, params=None, data=None, json=None, headers=None):
"""Send an http post request to the device."""
if not self._http_client:
self._http_client = httpx.AsyncClient()
response_data = None
cookies = None
if self._session_cookie:
Expand Down Expand Up @@ -268,8 +270,8 @@ async def send(self, request: str):

async def close(self) -> None:
"""Close the protocol."""
client = self._http_client
self._http_client = None
client = self._default_http_client
self._default_http_client = None
self._handshake_done = False
self._login_token = None
if client:
Expand Down
67 changes: 53 additions & 14 deletions kasa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@

from kasa import (
AuthenticationException,
ConnectionType,
Credentials,
DeviceType,
DeviceConfig,
DeviceFamilyType,
Discover,
EncryptType,
SmartBulb,
SmartDevice,
SmartDimmer,
SmartLightStrip,
SmartPlug,
SmartStrip,
UnsupportedDeviceException,
)
from kasa.device_factory import DEVICE_TYPE_TO_CLASS
from kasa.discover import DiscoveryResult

try:
Expand Down Expand Up @@ -49,10 +54,19 @@ def wrapper(message=None, *args, **kwargs):
# --json has set it to _nop_echo
echo = _do_echo

DEVICE_TYPES = [
device_type.value
for device_type in DeviceType
if device_type in DEVICE_TYPE_TO_CLASS

TYPE_TO_CLASS = {
"plug": SmartPlug,
"bulb": SmartBulb,
"dimmer": SmartDimmer,
"strip": SmartStrip,
"lightstrip": SmartLightStrip,
}

ENCRYPT_TYPES = [encrypt_type.value for encrypt_type in EncryptType]

DEVICE_FAMILY_TYPES = [
device_family_type.value for device_family_type in DeviceFamilyType
]

click.anyio_backend = "asyncio"
Expand Down Expand Up @@ -149,7 +163,7 @@ def _device_to_serializable(val: SmartDevice):
"--type",
envvar="KASA_TYPE",
default=None,
type=click.Choice(DEVICE_TYPES, case_sensitive=False),
type=click.Choice(list(TYPE_TO_CLASS), case_sensitive=False),
)
@click.option(
"--json/--no-json",
Expand All @@ -158,6 +172,18 @@ def _device_to_serializable(val: SmartDevice):
is_flag=True,
help="Output raw device response as JSON.",
)
@click.option(
"--encrypt-type",
envvar="KASA_ENCRYPT_TYPE",
default=None,
type=click.Choice(ENCRYPT_TYPES, case_sensitive=False),
)
@click.option(
"--device-family",
envvar="KASA_DEVICE_FAMILY",
default=None,
type=click.Choice(DEVICE_FAMILY_TYPES, case_sensitive=False),
)
@click.option(
"--timeout",
envvar="KASA_TIMEOUT",
Expand Down Expand Up @@ -199,6 +225,8 @@ async def cli(
verbose,
debug,
type,
encrypt_type,
device_family,
json,
timeout,
discovery_timeout,
Expand Down Expand Up @@ -270,12 +298,19 @@ def _nop_echo(*args, **kwargs):
return await ctx.invoke(discover)

if type is not None:
device_type = DeviceType.from_value(type)
dev = await SmartDevice.connect(
host, credentials=credentials, device_type=device_type, timeout=timeout
dev = TYPE_TO_CLASS[type](host)
await dev.update()
elif device_family or encrypt_type:
ctype = ConnectionType(
DeviceFamilyType(device_family),
EncryptType(encrypt_type),
)
config = DeviceConfig(
host=host, credentials=credentials, timeout=timeout, connection_type=ctype
)
dev = await SmartDevice.connect(config=config)
else:
echo("No --type defined, discovering..")
echo("No --type or --device-family and --encrypt-type defined, discovering..")
dev = await Discover.discover_single(
host,
port=port,
Expand Down Expand Up @@ -332,8 +367,10 @@ async def discover(ctx):
target = ctx.parent.params["target"]
username = ctx.parent.params["username"]
password = ctx.parent.params["password"]
timeout = ctx.parent.params["discovery_timeout"]
verbose = ctx.parent.params["verbose"]
discovery_timeout = ctx.parent.params["discovery_timeout"]
timeout = ctx.parent.params["timeout"]
port = ctx.parent.params["port"]

credentials = Credentials(username, password)

Expand All @@ -354,7 +391,7 @@ async def print_unsupported(unsupported_exception: UnsupportedDeviceException):
echo(f"\t{unsupported_exception}")
echo()

echo(f"Discovering devices on {target} for {timeout} seconds")
echo(f"Discovering devices on {target} for {discovery_timeout} seconds")

async def print_discovered(dev: SmartDevice):
async with sem:
Expand All @@ -376,9 +413,11 @@ async def print_discovered(dev: SmartDevice):

await Discover.discover(
target=target,
timeout=timeout,
discovery_timeout=discovery_timeout,
on_discovered=print_discovered,
on_unsupported=print_unsupported,
port=port,
timeout=timeout,
credentials=credentials,
)

Expand Down
4 changes: 2 additions & 2 deletions kasa/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
class Credentials:
"""Credentials for authentication."""

username: Optional[str] = field(default=None, repr=False)
password: Optional[str] = field(default=None, repr=False)
username: Optional[str] = field(default="", repr=False)
password: Optional[str] = field(default="", repr=False)
Loading