From 1db78052621ae8508dac5aa087a1a4e71f921e04 Mon Sep 17 00:00:00 2001 From: sdb9696 Date: Wed, 24 Jan 2024 07:27:44 +0000 Subject: [PATCH 1/2] Add --batch-size hint to timeout errors in dump_devinfo --- devtools/dump_devinfo.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/devtools/dump_devinfo.py b/devtools/dump_devinfo.py index 6a8240ef6..e6353e23f 100644 --- a/devtools/dump_devinfo.py +++ b/devtools/dump_devinfo.py @@ -20,7 +20,14 @@ import asyncclick as click from devtools.helpers.smartrequests import COMPONENT_REQUESTS, SmartRequest -from kasa import AuthenticationException, Credentials, Discover, SmartDevice +from kasa import ( + AuthenticationException, + Credentials, + Discover, + SmartDevice, + SmartDeviceException, + TimeoutException, +) from kasa.discover import DiscoveryResult from kasa.exceptions import SmartErrorCode from kasa.tapo.tapodevice import TapoDevice @@ -285,10 +292,30 @@ async def _make_requests_or_exit( ) ) exit(1) - except Exception as ex: + except SmartDeviceException as ex: click.echo( click.style(f"Unable to query {name} at once: {ex}", bold=True, fg="red") ) + if ( + isinstance(ex, TimeoutException) + or ex.error_code == SmartErrorCode.SESSION_TIMEOUT_ERROR + ): + click.echo( + click.style( + "Timeout, try reducing the batch size via --batch-size option.", + bold=True, + fg="red", + ) + ) + exit(1) + except Exception as ex: + click.echo( + click.style( + f"Unexpected exception querying {name} at once: {ex}", + bold=True, + fg="red", + ) + ) exit(1) From 6df277bda022c92e4395f37e59df26e16c137035 Mon Sep 17 00:00:00 2001 From: sdb9696 Date: Wed, 24 Jan 2024 09:32:05 +0000 Subject: [PATCH 2/2] Add _echo_error function for displaying critical errors --- devtools/dump_devinfo.py | 52 ++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/devtools/dump_devinfo.py b/devtools/dump_devinfo.py index e6353e23f..e9ec56b7b 100644 --- a/devtools/dump_devinfo.py +++ b/devtools/dump_devinfo.py @@ -234,11 +234,7 @@ async def get_legacy_fixture(device): try: final = await device.protocol.query(final_query) except Exception as ex: - click.echo( - click.style( - f"Unable to query all successes at once: {ex}", bold=True, fg="red" - ) - ) + _echo_error(f"Unable to query all successes at once: {ex}", bold=True, fg="red") if device._discovery_info and not device._discovery_info.get("system"): # Need to recreate a DiscoverResult here because we don't want the aliases @@ -261,6 +257,16 @@ async def get_legacy_fixture(device): return save_filename, copy_folder, final +def _echo_error(msg: str): + click.echo( + click.style( + msg, + bold=True, + fg="red", + ) + ) + + async def _make_requests_or_exit( device: SmartDevice, requests: List[SmartRequest], @@ -284,37 +290,25 @@ async def _make_requests_or_exit( final[method] = result return final except AuthenticationException as ex: - click.echo( - click.style( - f"Unable to query the device due to an authentication error: {ex}", - bold=True, - fg="red", - ) + _echo_error( + f"Unable to query the device due to an authentication error: {ex}", ) exit(1) except SmartDeviceException as ex: - click.echo( - click.style(f"Unable to query {name} at once: {ex}", bold=True, fg="red") + _echo_error( + f"Unable to query {name} at once: {ex}", ) if ( isinstance(ex, TimeoutException) or ex.error_code == SmartErrorCode.SESSION_TIMEOUT_ERROR ): - click.echo( - click.style( - "Timeout, try reducing the batch size via --batch-size option.", - bold=True, - fg="red", - ) + _echo_error( + "Timeout, try reducing the batch size via --batch-size option.", ) exit(1) except Exception as ex: - click.echo( - click.style( - f"Unexpected exception querying {name} at once: {ex}", - bold=True, - fg="red", - ) + _echo_error( + f"Unexpected exception querying {name} at once: {ex}", ) exit(1) @@ -388,12 +382,8 @@ async def get_smart_fixture(device: TapoDevice, batch_size: int): SmartRequest._create_request_dict(test_call.request) ) except AuthenticationException as ex: - click.echo( - click.style( - f"Unable to query the device due to an authentication error: {ex}", - bold=True, - fg="red", - ) + _echo_error( + f"Unable to query the device due to an authentication error: {ex}", ) exit(1) except Exception as ex: