FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add UNKNOWN_USERNAME (-60502) camera error code (#1761) · python-kasa/python-kasa@454e269 · GitHub

Repository navigation

Commit 454e269

Browse files
authored
Add UNKNOWN_USERNAME (-60502) camera error code (#1761)
1 parent 4c2ef50 commit 454e269

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎kasa/exceptions.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def from_int(value: int) -> SmartErrorCode:
135135
# Camera error codes
136136
SESSION_EXPIRED = -40401
137137
BAD_USERNAME = -40411 # determined from testing
138+
# determined from testing, returned by secure-login devices (C200, H200)
139+
# for an unaccepted username
140+
UNKNOWN_USERNAME = -60502
138141
HOMEKIT_LOGIN_FAIL = -40412
139142
DEVICE_BLOCKED = -40404
140143
DEVICE_FACTORY = -40405

‎tests/transports/test_sslaestransport.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,53 @@ async def test_unencrypted_passthrough_errors(
345345
await transport.send(json_dumps(request))
346346

347347

348+
@pytest.mark.xdist_group(name="caplog")
349+
async def test_handshake_unaccepted_username_no_unknown_error_warning(
350+
mocker: MockerFixture, caplog: pytest.LogCaptureFixture
351+
) -> None:
352+
"""Secure-login devices answer -60502 for an unaccepted username."""
353+
host = "127.0.0.1"
354+
mock_ssl_aes_device = MockSslAesDevice(host, want_default_username=True)
355+
mocker.patch.object(
356+
aiohttp.ClientSession, "post", side_effect=mock_ssl_aes_device.post
357+
)
358+
transport = SslAesTransport(
359+
config=DeviceConfig(host, credentials=Credentials(MOCK_USER, MOCK_PWD))
360+
)
361+
362+
caplog.set_level(logging.WARNING, logger="kasa.transports.sslaestransport")
363+
await transport.perform_handshake()
364+
365+
assert transport._state is TransportState.ESTABLISHED
366+
assert "received unknown error code" not in caplog.text
367+
assert SmartErrorCode.from_int(-60502) is SmartErrorCode.UNKNOWN_USERNAME
368+
369+
370+
@pytest.mark.xdist_group(name="caplog")
371+
async def test_handshake_unknown_inner_error_code(
372+
mocker: MockerFixture, caplog: pytest.LogCaptureFixture
373+
) -> None:
374+
host = "127.0.0.1"
375+
mock_ssl_aes_device = MockSslAesDevice(host)
376+
mocker.patch.object(
377+
aiohttp.ClientSession, "post", side_effect=mock_ssl_aes_device.post
378+
)
379+
mocker.patch.object(
380+
MockSslAesDevice,
381+
"BAD_USER_RESP",
382+
{**MockSslAesDevice.BAD_USER_RESP, "result": {"data": {"code": -99999}}},
383+
)
384+
transport = SslAesTransport(
385+
config=DeviceConfig(host, credentials=Credentials("foobar", MOCK_PWD))
386+
)
387+
388+
caplog.set_level(logging.WARNING, logger="kasa.transports.sslaestransport")
389+
with pytest.raises(AuthenticationError):
390+
await transport.perform_handshake()
391+
392+
assert f"Device {host} received unknown error code: -99999" in caplog.text
393+
394+
348395
async def test_device_blocked_response(mocker: MockerFixture) -> None:
349396
host = "127.0.0.1"
350397
mock_ssl_aes_device = MockSslAesDevice(host, device_blocked=True)
@@ -465,11 +512,16 @@ async def test_login_version_default_credentials(
465512

466513

467514
class MockSslAesDevice:
515+
# Response observed on secure-login devices (C200, H200) when the username
516+
# is not the one accepted for secure login.
468517
BAD_USER_RESP = {
469518
"error_code": SmartErrorCode.SESSION_EXPIRED.value,
470519
"result": {
471520
"data": {
472521
"code": -60502,
522+
"encrypt_type": ["1", "2"],
523+
"key": "Someb64keyWithUnknownPurpose",
524+
"nonce": "MixedCaseAlphaNumericWithUnknownPurpose",
473525
}
474526
},
475527
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL