From 8b021e08582d56fb6497116dff0cce3ec3f910ba Mon Sep 17 00:00:00 2001 From: sdb9696 Date: Fri, 29 Dec 2023 10:25:25 +0000 Subject: [PATCH] Fix dump_devinfo for unauthenticated --- devtools/README.md | 2 ++ devtools/dump_devinfo.py | 29 ++++++++++++++++++++++++----- kasa/aestransport.py | 7 +++---- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/devtools/README.md b/devtools/README.md index c7da859f8..50425c254 100644 --- a/devtools/README.md +++ b/devtools/README.md @@ -13,6 +13,8 @@ Usage: dump_devinfo.py [OPTIONS] HOST Options: -d, --debug --help Show this message and exit. + --username For authenticating devices. + --password ``` ## create_module_fixtures diff --git a/devtools/dump_devinfo.py b/devtools/dump_devinfo.py index c9f34fcb1..c03e97d55 100644 --- a/devtools/dump_devinfo.py +++ b/devtools/dump_devinfo.py @@ -17,7 +17,7 @@ import asyncclick as click -from kasa import Credentials, Discover, SmartDevice +from kasa import AuthenticationException, Credentials, Discover, SmartDevice from kasa.discover import DiscoveryResult from kasa.tapo.tapodevice import TapoDevice @@ -85,14 +85,14 @@ def default_to_regular(d): @click.argument("host") @click.option( "--username", - default=None, + default="", required=False, envvar="TPLINK_CLOUD_USERNAME", help="Username/email address to authenticate to device.", ) @click.option( "--password", - default=None, + default="", required=False, envvar="TPLINK_CLOUD_PASSWORD", help="Password to use to authenticate to device.", @@ -227,6 +227,15 @@ async def get_smart_fixture(device: SmartDevice): try: click.echo(f"Testing {test_call}..", nl=False) response = await device.protocol.query(test_call.method) + except AuthenticationException as ex: + click.echo( + click.style( + f"Unable to query the device due to an authentication error: {ex}", + bold=True, + fg="red", + ) + ) + exit(1) except Exception as ex: click.echo(click.style(f"FAIL {ex}", fg="red")) else: @@ -244,15 +253,25 @@ async def get_smart_fixture(device: SmartDevice): try: responses = await device.protocol.query(final_query) + except AuthenticationException as ex: + click.echo( + click.style( + f"Unable to query the device due to an authentication error: {ex}", + bold=True, + fg="red", + ) + ) + exit(1) except Exception as ex: click.echo( click.style( f"Unable to query all successes at once: {ex}", bold=True, fg="red" ) ) + exit(1) final = {} - for response in responses["responses"]: - final[response["method"]] = response["result"] + for method, result in responses.items(): + final[method] = result # Need to recreate a DiscoverResult here because we don't want the aliases # in the fixture, we want the actual field names as returned by the device. diff --git a/kasa/aestransport.py b/kasa/aestransport.py index b6fa34723..df26c4c49 100644 --- a/kasa/aestransport.py +++ b/kasa/aestransport.py @@ -182,10 +182,9 @@ async def _perform_login_for_version(self, *, login_version: int = 1): "request_time_milis": round(time.time() * 1000), } request = json_dumps(login_request) - try: - resp_dict = await self.send_secure_passthrough(request) - except SmartDeviceException as ex: - raise AuthenticationException(ex) from ex + + resp_dict = await self.send_secure_passthrough(request) + self._handle_response_error_code(resp_dict, "Error logging in") self._login_token = resp_dict["result"]["token"] async def perform_login(self) -> None: