Repository navigation
Read the Polestar Data Portal's M2M API - #81
Merged
Merged
Conversation
A Polestar is read through the Polestar Data Portal (EU Data Act), with a credential its owner creates there. internal/polestar is its client, on the model of internal/volvo, used by nothing yet. The client asks a token with the credential's client ID and secret (a JSON body, without grant_type), lists the VINs, and fetches the five domains a logger needs (battery, odometer, location, availability, target-soc), with the account ID in x-client-id. It returns the raw bodies: one request per call, no retry, the body capped and checked as JSON. Its errors are kinds the collector will act on: a refused credential (a 401 on /token, or an account ID that is not the credential's), an expired token (any 401 on /v1: the specification describes none), a VIN no longer shared, a domain not granted (a 403 of the API's own body on a domain), a domain the car does not upload (DATA_NOT_AVAILABLE), and a 429 with Retry-After when it has one. Normalizer reads the domains into snapshots for the source polestar, each value dated by the car's timestamp. The API writes protobuf's JSON, which leaves zeros out: a missing field stays absent, but the charging power out of an active charge, which is 0 W. The enums lose their prefix; usageMode stands for the engine, chargingStatusV2 for the charging status. The model comes from the VIN alone: its year at the tenth position, battery electric for every model the portal serves; the family is left unknown, no real VIN telling the Polestar 2 from the 3, 4 or 5. The fingerprint leaves out timestamp, metaReceivedAt and metaEventId, so that a parked car uploading the same values again makes no new snapshot. Tested against the real responses of testdata/polestar-data-portal and against the simulator's Polestar of demonstration. The package joins archtest's allowlist, with its depguard rule. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closed
8 tasks done
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.
Part of #52 (step 2: the client). It does not close the issue: the schema, the collector by provider, the API and the Connection page come next.
What
internal/polestar, the client of the Polestar Data Portal's M2M API, on the model ofinternal/volvo. Nothing uses it yet.Token(POST /token,clientId/clientSecretin JSON, withoutgrant_type),Vehicles(VINs only),Fetchof a domain (battery, odometer, location, availability, target-soc), withAuthorization: Bearerandx-client-id. One request per call, no retry, body capped, JSON checked.APIError,Kind,RetryIn): credential refused (401 on /token, orAUTHZ_CLIENT_ID_MISMATCH), token expired (401 on /v1), VIN no longer shared, domain not granted,DATA_NOT_AVAILABLE, 429 withRetry-After(seconds or a date).polestar.Normalizer(satisfiesderive.Normalizer, sourcepolestar): values dated by the car's timestamp, odometer in metres → km,usageMode→ engine,chargingStatusV2→ charging status, protobuf's omitted zeros (a missing power is 0 W only outside an active charge), model year from the VIN's 10th character, battery electric.timestamp,metaReceivedAtandmetaEventId.archtestallowlist and a depguard rulepolestar-is-a-client;coreandapimay not import it.Assumptions and what is left open
/v1: any 401 there is taken for an expired token.SMART_CHARGING_WILL_NOT_FINISHis counted as charging;CHARGING_TYPE_WIRELESSis left absent.Tests
testdata/polestar-data-portal/(exact snapshots, the real 404 body).polestarapi, scenariopolestar2): token, vehicles, every domain; parked, driving, on a DC charge; each error the simulator gives; a parked car's identical re-upload keeps its fingerprint.task unit-gopasses. Mutation check: an import ofvolvoinpolestarfails both depguard andTestDependencies.🤖 Generated with Claude Code