Fix dump_devinfo for unauthenticated (#593)

This commit is contained in:
sdb9696 2023-12-29 19:42:02 +00:00 committed by GitHub
parent f6fd898faf
commit 1b7914277d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 9 deletions

View File

@ -13,6 +13,8 @@ Usage: dump_devinfo.py [OPTIONS] HOST
Options: Options:
-d, --debug -d, --debug
--help Show this message and exit. --help Show this message and exit.
--username For authenticating devices.
--password
``` ```
## create_module_fixtures ## create_module_fixtures

View File

@ -17,7 +17,7 @@ from pprint import pprint
import asyncclick as click import asyncclick as click
from kasa import Credentials, Discover, SmartDevice from kasa import AuthenticationException, Credentials, Discover, SmartDevice
from kasa.discover import DiscoveryResult from kasa.discover import DiscoveryResult
from kasa.tapo.tapodevice import TapoDevice from kasa.tapo.tapodevice import TapoDevice
@ -85,14 +85,14 @@ def default_to_regular(d):
@click.argument("host") @click.argument("host")
@click.option( @click.option(
"--username", "--username",
default=None, default="",
required=False, required=False,
envvar="TPLINK_CLOUD_USERNAME", envvar="TPLINK_CLOUD_USERNAME",
help="Username/email address to authenticate to device.", help="Username/email address to authenticate to device.",
) )
@click.option( @click.option(
"--password", "--password",
default=None, default="",
required=False, required=False,
envvar="TPLINK_CLOUD_PASSWORD", envvar="TPLINK_CLOUD_PASSWORD",
help="Password to use to authenticate to device.", help="Password to use to authenticate to device.",
@ -227,6 +227,15 @@ async def get_smart_fixture(device: SmartDevice):
try: try:
click.echo(f"Testing {test_call}..", nl=False) click.echo(f"Testing {test_call}..", nl=False)
response = await device.protocol.query(test_call.method) 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: except Exception as ex:
click.echo(click.style(f"FAIL {ex}", fg="red")) click.echo(click.style(f"FAIL {ex}", fg="red"))
else: else:
@ -244,15 +253,25 @@ async def get_smart_fixture(device: SmartDevice):
try: try:
responses = await device.protocol.query(final_query) 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: except Exception as ex:
click.echo( click.echo(
click.style( click.style(
f"Unable to query all successes at once: {ex}", bold=True, fg="red" f"Unable to query all successes at once: {ex}", bold=True, fg="red"
) )
) )
exit(1)
final = {} final = {}
for response in responses["responses"]: for method, result in responses.items():
final[response["method"]] = response["result"] final[method] = result
# Need to recreate a DiscoverResult here because we don't want the aliases # 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. # in the fixture, we want the actual field names as returned by the device.

View File

@ -182,10 +182,9 @@ class AesTransport(BaseTransport):
"request_time_milis": round(time.time() * 1000), "request_time_milis": round(time.time() * 1000),
} }
request = json_dumps(login_request) request = json_dumps(login_request)
try:
resp_dict = await self.send_secure_passthrough(request) resp_dict = await self.send_secure_passthrough(request)
except SmartDeviceException as ex: self._handle_response_error_code(resp_dict, "Error logging in")
raise AuthenticationException(ex) from ex
self._login_token = resp_dict["result"]["token"] self._login_token = resp_dict["result"]["token"]
async def perform_login(self) -> None: async def perform_login(self) -> None: