feat(tesla): raise PrivateKeyError for unusable existing key files - #146
Merged
Conversation
Home Assistant has to catch OSError/ValueError/TypeError/AssertionError at multiple sites to handle an unreadable, malformed, encrypted, or wrong-type key file. Give get_private_key/get_rsa_private_key one typed exception with a reason instead. Claude-Session: https://claude.ai/code/session_01D7girHWFLWBu78by9NL3qE
Bre77
commented
Sep 5, 2026
| status = 540 | ||
|
|
||
|
|
||
| class PrivateKeyError(TeslaFleetError): |
Member
Author
There was a problem hiding this comment.
Shouldn't this be a LibraryError since its not actually a Tesla Fleet API upstream error?
A key-file failure is local to this library, not an upstream Fleet API error, so it belongs with SigningDisabled under LibraryError. Claude-Session: https://claude.ai/code/session_01D7girHWFLWBu78by9NL3qE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
Review-comment fix on PR 146: PrivateKeyError must subclass LibraryError, not TeslaFleetError, since a key-file failure is local to this library, not an upstream Fleet API error. Moved the class next to SigningDisabled under LibraryError in exceptions.py, kept the reason/message attributes and docstring, updated the docstring's rationale and the README line that described it as a TeslaFleetError subclass. LibraryError subclasses Exception (not BaseException); PrivateKeyError(...) from err raise sites in tesla.py are unaffected by this base-class change. Tests still assert PrivateKeyError itself and needed no changes.
What Changed
PrivateKeyError(aLibraryErrorsubclass, notTeslaFleetError, since a key-file failure is local to this library rather than an upstream Fleet API error) with areasonof"unreadable","malformed","encrypted", or"wrong_type", plus amessageattribute.Tesla.get_private_key/get_rsa_private_keynow route a known-existing key file's read/parse failure through a shared_load_existing_private_keyhelper that raisesPrivateKeyErrorfor I/O errors, invalid PEM, password-encrypted PEM, and a loaded key of the wrong type, replacing the previous ad-hocFileNotFoundError/PermissionError/AssertionErrorhandling; key generation and the O_EXCL create-race fallback are unaffected.docs/bluetooth_vehicles.md/docs/fleet_api_energy_sites.mdto document the newPrivateKeyErrorreasons and itsLibraryErrorbase, consolidating the create/load semantics description in the README with cross-references from the docs.PrivateKeyErrorTestscovering unreadable, malformed, encrypted, and wrong-type key files for both the EC and RSA loaders.Risk Assessment
✅ Low: Small, well-scoped change: moves PrivateKeyError from TeslaFleetError to LibraryError, correctly threads the message through Exception.init (required since LibraryError has no custom init unlike TeslaFleetError), updates docs consistently, and has no internal call sites that catch TeslaFleetError/BaseException expecting to intercept PrivateKeyError, so no behavior regression.
Testing
Ran the existing targeted unit tests in tests/test_tesla_private_key.py (all 38 pass unchanged, confirming the intent's claim that no test changes were needed) and additionally wrote a manual end-to-end script exercising the actual exception-hierarchy behavior change: it proves a caller's generic
except TeslaFleetErrorhandler no longer swallows PrivateKeyError post-fix, whileexcept LibraryErrordoes, and thatreason/message/str()remain intact. No issues found; working tree left clean.Evidence: End-to-end demonstration: PrivateKeyError escapes `except TeslaFleetError` but is caught by `except LibraryError`
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
✅ **Test** - passed
✅ No issues found.
uv run python -m pytest tests/test_tesla_private_key.py -v(38 passed, including the 8 PrivateKeyErrorTests asserting reason/message/cause unchanged)Manual script: constructed a malformed PEM file, calledTesla().get_private_key(path), and confirmedexcept TeslaFleetErrorno longer catches the raised error whileexcept LibraryErrordoes, withreason,message, andstr(e)all intactissubclass(PrivateKeyError, LibraryError)is True,issubclass(PrivateKeyError, TeslaFleetError)is False,issubclass(LibraryError, Exception)is True✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.