Skip to content

Fix P100 error getting conn closed when trying default login after login failure #690

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 2 commits into from
Jan 23, 2024
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
33 changes: 21 additions & 12 deletions kasa/aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,28 @@ async def perform_login(self):
"""Login to the device."""
try:
await self.try_login(self._login_params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does fix the issue but we do handshake 2x now on discovery

DEBUG:kasa.aestransport:Generating keypair
DEBUG:kasa.aestransport:Handshake params: {'key': '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZbufuct/b1qSTmUpaLVApGIEMaspNgu1cwTJF4LN0XVXtIoPk+xx0rruNDOTOpVkLpT2Rw/UVyxt63fMktC03Y/1x+BfVfLbDXYTmlFE5+180KP0lwAN8uipqAaHHkEPR0mw1xoiyndZTu9O2CSexdvQeQh8/uNiTYiJZxJ9rTwIDAQAB\n-----END PUBLIC KEY-----\n'}
DEBUG:kasa.aestransport:Request {'method': 'handshake', 'params': {'key': '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZbufuct/b1qSTmUpaLVApGIEMaspNgu1cwTJF4LN0XVXtIoPk+xx0rruNDOTOpVkLpT2Rw/UVyxt63fMktC03Y/1x+BfVfLbDXYTmlFE5+180KP0lwAN8uipqAaHHkEPR0mw1xoiyndZTu9O2CSexdvQeQh8/uNiTYiJZxJ9rTwIDAQAB\n-----END PUBLIC KEY-----\n'}}
DEBUG:kasa.aestransport:Device responded with: {'error_code': 0, 'result': {'key': 'cVxMCtYukLw31nzEr5L/ytLSFjR33CED9KAkwAh6fJFfiAu3LvyifLFx3dWCQUQfLEDrZSjvH53ru0MUtyvWiSqn8JNuliRJ8MHxfe477ksarKWyLkdhxFoCYZ91AMhIpGQ2WofrGxDOvgpXWIbTOJLLirK0JP6CqacZP4cKdAo='}}
DEBUG:kasa.aestransport:Handshake with 192.168.107.47 complete
DEBUG:kasa.aestransport:Will perform handshaking...
DEBUG:kasa.aestransport:Generating keypair
DEBUG:kasa.aestransport:Handshake params: {'key': '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDal8slelSWpAjM+TXxqVVul2TWk11rty5eb+qtsRuesvD492CA1AUA5jRw5zz2OSGPJSt1vv5qnbBbDVFHX10k7Xq+vKnq9Ytnd7FOUtlVKSVdBq5Ynkx1TZjv8/SwB5N9t/j+ZqZoJVIEeKP+YUccAPOikHutRRNu91t2AgqsxwIDAQAB\n-----END PUBLIC KEY-----\n'}
DEBUG:kasa.aestransport:Request {'method': 'handshake', 'params': {'key': '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDal8slelSWpAjM+TXxqVVul2TWk11rty5eb+qtsRuesvD492CA1AUA5jRw5zz2OSGPJSt1vv5qnbBbDVFHX10k7Xq+vKnq9Ytnd7FOUtlVKSVdBq5Ynkx1TZjv8/SwB5N9t/j+ZqZoJVIEeKP+YUccAPOikHutRRNu91t2AgqsxwIDAQAB\n-----END PUBLIC KEY-----\n'}}

except AuthenticationException as ex:
if ex.error_code != SmartErrorCode.LOGIN_ERROR:
raise ex
if self._default_credentials is None:
self._default_credentials = get_default_credentials(
DEFAULT_CREDENTIALS["TAPO"]
except AuthenticationException as aex:
try:
if aex.error_code != SmartErrorCode.LOGIN_ERROR:
raise aex
if self._default_credentials is None:
self._default_credentials = get_default_credentials(
DEFAULT_CREDENTIALS["TAPO"]
)
await self.perform_handshake()
await self.try_login(self._get_login_params(self._default_credentials))
_LOGGER.debug(
"%s: logged in with default credentials",
self._host,
)
await self.perform_handshake()
await self.try_login(self._get_login_params(self._default_credentials))
_LOGGER.debug(
"%s: logged in with default credentials",
self._host,
)
except AuthenticationException:
raise
except Exception as ex:
raise AuthenticationException(
"Unable to login and trying default "
+ "login raised another exception: %s",
ex,
) from ex

async def try_login(self, login_params):
"""Try to login with supplied login_params."""
Expand Down
12 changes: 11 additions & 1 deletion kasa/tests/test_aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@ async def test_login(mocker, status_code, error_code, inner_error_code, expectat
pytest.raises(AuthenticationException),
1,
),
(
[SmartErrorCode.LOGIN_ERROR, SmartErrorCode.SESSION_TIMEOUT_ERROR],
pytest.raises(SmartDeviceException),
3,
),
],
ids=("LOGIN_ERROR-success", "LOGIN_ERROR-LOGIN_ERROR", "LOGIN_FAILED_ERROR"),
ids=(
"LOGIN_ERROR-success",
"LOGIN_ERROR-LOGIN_ERROR",
"LOGIN_FAILED_ERROR",
"LOGIN_ERROR-SESSION_TIMEOUT_ERROR",
),
)
async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
host = "127.0.0.1"
Expand Down