Add UnsupportedEncryptionError for unsupported encryption schemes (#1866)
Some checks are pending
CI / Perform Lint Checks (3.15) (push) Waiting to run
CI / Python 3.11 on macos-latest (push) Blocked by required conditions
CI / Python 3.12 on macos-latest (push) Blocked by required conditions
CI / Python 3.13 on macos-latest (push) Blocked by required conditions
CI / Python 3.14 on macos-latest (push) Blocked by required conditions
CI / Python 3.15 on macos-latest (push) Blocked by required conditions
CI / Python 3.11 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.12 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.13 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.14 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.15 on ubuntu-latest (push) Blocked by required conditions
CI / Python 3.11 on windows-latest (push) Blocked by required conditions
CI / Python 3.12 on windows-latest (push) Blocked by required conditions
CI / Python 3.13 on windows-latest (push) Blocked by required conditions
CI / Python 3.14 on windows-latest (push) Blocked by required conditions
CI / Python 3.15 on windows-latest (push) Blocked by required conditions
CodeQL Checks / Analyze (python) (push) Waiting to run

This commit is contained in:
Teemu R.
2026-11-28 01:13:53 +01:00
committed by GitHub
parent 422194e5eb
commit 6a35bb6e53
8 changed files with 53 additions and 13 deletions

View File

@@ -134,6 +134,12 @@
:undoc-members:
```
```{eval-rst}
.. autoclass:: kasa.exceptions.UnsupportedEncryptionError
:members:
:undoc-members:
```
```{eval-rst}
.. autoclass:: kasa.exceptions.TimeoutError
:members:

View File

@@ -139,6 +139,8 @@ The base exception for all library errors is {class}`KasaException <kasa.excepti
from {class}`DeviceError <kasa.exceptions.DeviceError>` and could contain an ``error_code`` depending on the type of failure.
- If a command is sent to a hub child that is not available (see {attr}`Device.is_available <kasa.Device.is_available>`) the library raises a {class}`DeviceUnavailableError <kasa.exceptions.DeviceUnavailableError>`,
derived from {class}`DeviceError <kasa.exceptions.DeviceError>`, without contacting the device.
- If the library encounters and unsupported deviceit raises an {class}`UnsupportedDeviceError <kasa.exceptions.UnsupportedDeviceError>`.
- If the library encounters an unsupported device it raises an {class}`UnsupportedDeviceError <kasa.exceptions.UnsupportedDeviceError>`.
When encountering an unsupported encryption scheme, it raises the derived
{class}`UnsupportedEncryptionError <kasa.exceptions.UnsupportedEncryptionError>` naming the scheme in ``encryption_type``.
- If the device fails to respond within a timeout the library raises a {class}`TimeoutError <kasa.exceptions.TimeoutError>`.
- All other failures will raise the base {class}`KasaException <kasa.exceptions.KasaException>` class.

View File

@@ -34,6 +34,7 @@ from kasa.exceptions import (
KasaException,
TimeoutError,
UnsupportedDeviceError,
UnsupportedEncryptionError,
)
from kasa.feature import Feature
from kasa.interfaces.light import HSV, ColorTempRange, Light, LightState
@@ -71,6 +72,7 @@ __all__ = [
"DeviceError",
"DeviceUnavailableError",
"UnsupportedDeviceError",
"UnsupportedEncryptionError",
"TimeoutError",
"Credentials",
"DeviceConfig",

View File

@@ -14,6 +14,7 @@ from kasa import (
Device,
Discover,
UnsupportedDeviceError,
UnsupportedEncryptionError,
)
from kasa.discover import (
NEW_DISCOVERY_REDACTORS,
@@ -170,7 +171,11 @@ async def list(ctx: click.Context) -> DeviceDict:
async def print_unsupported(unsupported_exception: UnsupportedDeviceError):
if host := unsupported_exception.host:
echo(f"{host:<15} UNSUPPORTED DEVICE")
if isinstance(unsupported_exception, UnsupportedEncryptionError):
reason = f"ENCRYPTION {unsupported_exception.encryption_type}"
echo(f"{host:<15} UNSUPPORTED {reason}")
else:
echo(f"{host:<15} UNSUPPORTED DEVICE")
echo(
f"{'HOST':<15} {'MODEL':<9} {'DEVICE FAMILY':<20} {'ENCRYPT':<7} "

View File

@@ -124,6 +124,7 @@ from kasa.exceptions import (
KasaException,
TimeoutError,
UnsupportedDeviceError,
UnsupportedEncryptionError,
)
from kasa.iot.iotdevice import IotDevice, _extract_sys_info
from kasa.json import DataClassJSONMixin
@@ -847,11 +848,13 @@ class Discover:
)
if encrypt_type not in {e.value for e in DeviceEncryptionType}:
raise UnsupportedDeviceError(
raise UnsupportedEncryptionError(
f"Device {discovery_result.ip} ({type_}) uses unsupported "
f"encryption type {encrypt_type}",
f"encryption type {encrypt_type}, enabling third-party "
"compatibility in the official app may help",
discovery_result=discovery_result.to_dict(),
host=discovery_result.ip,
encryption_type=encrypt_type,
)
return DeviceConnectionParameters.from_values(
type_,

View File

@@ -35,6 +35,18 @@ class UnsupportedDeviceError(KasaException):
super().__init__(*args)
class UnsupportedEncryptionError(UnsupportedDeviceError):
"""Exception for devices using an encryption scheme not implemented here.
The device type may still be supported on firmware using another scheme.
``encryption_type`` is the raw scheme name the device reported, e.g. ``TPAP``.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.encryption_type: str = kwargs["encryption_type"]
super().__init__(*args, **kwargs)
class DeviceError(KasaException):
"""Base exception for device errors."""

View File

@@ -209,10 +209,17 @@ async def test_list_update_failed(
assert row in res.output.replace("\n", "")
# Unsupported device fixtures failing on their encryption, by fixture id
UNSUPPORTED_ENCRYPTION_ROWS = {
"unknown_encryption": "ENCRYPTION FOO",
"wrong_encryption_smart": "ENCRYPTION IOT",
}
async def test_list_unsupported(
unsupported_device_info: dict, runner: CliRunner
unsupported_device_info: dict, runner: CliRunner, request: pytest.FixtureRequest
) -> None:
"""Test that device update is called on main."""
"""Test that unsupported devices are listed with the reason, if known."""
res = await runner.invoke(
cli,
["--username", "foo", "--password", "bar", "discover", "list"],
@@ -223,7 +230,8 @@ async def test_list_unsupported(
f"{'HOST':<15} {'MODEL':<9} {'DEVICE FAMILY':<20} {'ENCRYPT':<7} "
f"{'HTTPS':<5} {'LV':<3} {'ALIAS'}"
)
row = f"{'127.0.0.1':<15} UNSUPPORTED DEVICE"
reason = UNSUPPORTED_ENCRYPTION_ROWS.get(request.node.callspec.id, "DEVICE")
row = f"{'127.0.0.1':<15} UNSUPPORTED {reason}\n"
assert header in res.output
assert row in res.output

View File

@@ -41,7 +41,11 @@ from kasa.discover import (
_DiscoverProtocol,
json_dumps,
)
from kasa.exceptions import AuthenticationError, UnsupportedDeviceError
from kasa.exceptions import (
AuthenticationError,
UnsupportedDeviceError,
UnsupportedEncryptionError,
)
from kasa.iot import IotDevice, IotPlug
from kasa.transports.aestransport import AesEncyptionSession
from kasa.transports.xortransport import XorEncryption, XorTransport
@@ -816,12 +820,10 @@ async def test_connection_parameters_unsupported_encrypt_type():
},
}
with pytest.raises(
UnsupportedDeviceError,
match=(
r"^Device 127\.0\.0\.123 \(SMART\.TAPOROBOVAC\) uses unsupported "
r"encryption type TPAP$"
),
UnsupportedEncryptionError,
match=r"encryption type TPAP, enabling third-party compatibility",
) as exc_info:
Discover._get_connection_parameters(DiscoveryResult.from_dict(result))
assert exc_info.value.host == "127.0.0.123"
assert exc_info.value.discovery_result
assert exc_info.value.encryption_type == "TPAP"