Some Kasa IoT devices accept the account e-mail with `kasaSetup`
instead of the account password over KLAP. python-kasa only tries that
password with the default Kasa account, so these devices fail to
authenticate even with the correct account credentials.
This PR checks the account e-mail with the setup password locally
against handshake1, after the supplied credentials, without an extra
request.
In details:
- Try the e-mail as given, then trimmed and lowercased as the Kasa app
sends it.
- Keep the fallbacks in one ordered list, with an explicit `save_hash`
flag controlling hash persistence.
- Save only a matching account/setup hash as `credentials_hash`, so
stored configs can reconnect without the e-mail.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This centralizes the response budgets in
`IotDevice.max_device_response_size` and removes the bulb override:
- KLAP: 2048 for every IoT device.
- Other transports: 4096 for bulbs and light strips, 16 KiB for other
IoT devices.
Some tests leave the `kasa` logger at a different level or rename devices
in shared fixture data. Later log assertions and device dump comparisons
then pass or fail depending on the test order.
This PR restores logger levels through `caplog` and renames devices on
copies, so the affected tests pass independently of their order.
In details:
- Use `caplog.set_level` for tests that change the `kasa` logger, and
remove an unused logger override.
- Add an autouse fixture that restores any changed logger levels and
fails the test with a pointer to `caplog.set_level`.
- Rename README devices on deep copies of the fixture data.
- Cover KLAP handshake debug logging explicitly, instead of relying on
an earlier test leaving DEBUG enabled.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Some IoT devices respond over KLAP with a plain `200 OK` html page
instead of a proper response when the response would be too large to
construct, causing a "not a multiple of the block length" decryption
error (e.g., KS200M in #1828).
This PR lowers the maximum response size to 2048 bytes for IoT devices
using KLAP, so that the update is split into multiple smaller requests
like it is already done for bulbs.
In details:
- `IotDevice.max_device_response_size` returns 2048 over KLAP. Bulbs
keep 4096 and XOR devices keep 16 KiB.
- `KlapTransport` checks the response length and signature before
decrypting, and raises a `KasaException` telling what was wrong
instead of the decryption error.
Fixes#1828
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
- Add `kasa/transports/sslcontext.py` with one shared SSL context for
the https transports to avoid copies across transports.
- Use it in `SslTransport`, and convert KLAP and Linkie also to use it.
- Extend Python's default ciphers with the legacy suites instead of
replacing accepted ciphers for future compatibility.
A `credentials_hash` is specific to the transport that produced it, but
nothing checked that the hash a transport was handed was its own.
This matters because a device can change its encryption type without the
credentials changing. For example, toggling **Third-Party Compatibility** in the Tapo
app moves a plug or strip between TPAP and KLAP, and firmware updates are known to introduce new transports.
This introduces a per-transport check that the hash has the shape that transport produces, and treats a foreign hash as absent rather than as a bad password. This makes the failure honest but still ends in a reauth.
This also changes how credentials are read back out when the hash is one of the plaintext forms and lets the transport derive its own, so the change of encryption type needs no reauth in all cases.
Some cameras (C220 and C100 on the 1.4.4 line in those reports) drop the
session about every ten minutes and answer the next `securePassthrough`
with HTTP 401 and `-40421` in the body.
With this change a 401 on passthrough marks the transport as needing a
handshake and raises `_RetryableError`, so the protocol logs in again
and retries the same request.
---------
Co-authored-by: freeKC <7538438+freeKC@users.noreply.github.com>
Add ECDHE-RSA-AES256-GCM-SHA384 alongside the existing ECDHE-RSA-AES128-GCM-SHA256 suite for newer Tapo firmware, while keeping all legacy suites for backward compatibility.
Tested with Tapo TC70, C530WS, and C210 cameras after firmware updates produced TLS handshake failures.
type annotations and parameter type annotations to
all test functions and helper functions across the 5 transport test
files. This enables mypy to check function bodies, catching type errors
that were previously hidden.
Updates to get_default_credentials and DEFAULT_CREDENTIALS for handling
a new default password for encryption_type 3 in TAPOCAMERA devices that
use encryption_type 3.
This adds support for devices like TC40.
Ruff 0.9 contains a number of formatter changes for the 2025 style guide.
Update to `ruff>=0.9.0` and apply the formatter fixes.
https://astral.sh/blog/ruff-v0.9.0
Older firmware versions do not encrypt the payload.
Tested to work with C110 hw 2.0 fw 1.3.7 Build 230823 Rel.57279n(5553)
---------
Co-authored-by: Teemu R. <tpr@iki.fi>
Devices that have failed authentication multiple times due to bad credentials go into a blocked state for 30 mins.
Handle that as a different error type instead of treating it as a normal `AuthenticationError`.
Add LinkieTransportV2 transport used by kasa cameras and a basic
implementation for IOT.IPCAMERA (kasacam) devices.
---------
Co-authored-by: Zach Price <pricezt@ornl.gov>
Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
Co-authored-by: Teemu Rytilahti <tpr@iki.fi>
This PR implements a clear-text, token-based transport protocol seen on
RV30 Plus (#937).
- Client sends `{"username": "email@example.com", "password":
md5(password)}` and gets back a token in the response
- Rest of the communications are done with POST at `/app?token=<token>`
---------
Co-authored-by: Steven B. <51370195+sdb9696@users.noreply.github.com>
* Transport tests under tests/transports/
* Protocol tests under tests/protocols/
* IOT tests under iot/
* Plus some minor cleanups, most code changes are related to splitting
up smart & iot tests