diff --git a/docs/source/reference.md b/docs/source/reference.md index b4d098f7..50602224 100644 --- a/docs/source/reference.md +++ b/docs/source/reference.md @@ -134,6 +134,12 @@ :undoc-members: ``` +```{eval-rst} +.. autoclass:: kasa.exceptions.UnsupportedEncryptionError + :members: + :undoc-members: +``` + ```{eval-rst} .. autoclass:: kasa.exceptions.TimeoutError :members: diff --git a/docs/source/topics.md b/docs/source/topics.md index 34660a0d..436b7c3e 100644 --- a/docs/source/topics.md +++ b/docs/source/topics.md @@ -139,6 +139,8 @@ The base exception for all library errors is {class}`KasaException ` 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 `) the library raises a {class}`DeviceUnavailableError `, derived from {class}`DeviceError `, without contacting the device. -- If the library encounters and unsupported deviceit raises an {class}`UnsupportedDeviceError `. +- If the library encounters an unsupported device it raises an {class}`UnsupportedDeviceError `. + When encountering an unsupported encryption scheme, it raises the derived + {class}`UnsupportedEncryptionError ` naming the scheme in ``encryption_type``. - If the device fails to respond within a timeout the library raises a {class}`TimeoutError `. - All other failures will raise the base {class}`KasaException ` class. diff --git a/kasa/__init__.py b/kasa/__init__.py index 4950cbc1..0949f809 100755 --- a/kasa/__init__.py +++ b/kasa/__init__.py @@ -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", diff --git a/kasa/cli/discover.py b/kasa/cli/discover.py index af367e32..8efba523 100644 --- a/kasa/cli/discover.py +++ b/kasa/cli/discover.py @@ -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} " diff --git a/kasa/discover.py b/kasa/discover.py index b958e76f..b7fb6751 100755 --- a/kasa/discover.py +++ b/kasa/discover.py @@ -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_, diff --git a/kasa/exceptions.py b/kasa/exceptions.py index 7499eef4..cc44a181 100644 --- a/kasa/exceptions.py +++ b/kasa/exceptions.py @@ -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.""" diff --git a/tests/test_cli.py b/tests/test_cli.py index 029ae1fb..43fa5189 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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 diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 7f30a0a4..932c8f33 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -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"