Skip to content

Fix dump_devinfo for unauthenticated #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions devtools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions devtools/dump_devinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions kasa/aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down