Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions .changeset/energy-island-modes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tesla-fleet-api": minor
---

`set_island_mode` now accepts only `EnergyIslandMode.ON_GRID` (1) and `OFF_GRID` (6), and throws `TypeError` before signing on anything else. It does not coerce `"6"`, and a missing mode has no default. `force` must be a boolean when given.
5 changes: 5 additions & 0 deletions .changeset/energy-island-rejection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tesla-fleet-api": minor
---

`set_island_mode`, `go_off_grid` and `reconnect_grid` now raise `DeviceUnexpectedResponse` when the gateway refuses, for example `REJECTED_LOW_ENERGY`, instead of returning the refusal as a normal reply. `data` holds `{code, message}`, with `message` the result's name.
5 changes: 5 additions & 0 deletions .changeset/log-routable-message.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tesla-fleet-api": patch
---

Debug logs now redact `routable_message`, a signed command that would execute again if replayed within 30 seconds.
14 changes: 13 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,22 @@ A funnel, not a selector: do not add source health, availability, grace windows,
- TLS goes through `node:https` with certificate checks off on `V1rTransport`'s own `https.Agent` only: global `fetch` cannot take a self-signed certificate, and `NODE_TLS_REJECT_UNAUTHORIZED` would also disable the cloud's checks. `node:https` decompresses nothing, so every body is gunzipped by magic bytes.
- A v1r POST is sent once, carries no bearer token, and is never retried at any level: the same bytes execute again before `expires_at`. Only REST GETs retry, once, after a 401/403 re-login.
- LAN envelopes are separate from the cloud's: commands set `DELIVERY_CHANNEL_HERMES_COMMAND`, and GraphQL and system-info reads use `LOCAL_HTTPS` with the `INSTALLER` sender. `EnergyDeviceSigner.envelope` is cloud-only and leaves the channel unset.
- `Powerwall*` errors extend `Error`, not `TeslaFleetError`. Only `failover_safe = true` on connection, authentication and rate-limit errors lets a router fail over. A tolerated reply (`cancel_max_backup`, `remove_authorized_client`) swallows only fault and protocol errors, unlike Python, so a dead link never reads as success.
- **Rules that differ from Python on purpose. Do not "fix" these back:**
- `cancel_max_backup` and `remove_authorized_client` tolerate only an empty reply, plus code 5 on a cancel. Python swallows every error, and document 07 section 8 is overridden here, so a BUSY fault never reads as a revoked key.
- An island or `trigger_islanding` reply that does not decode raises `PowerwallProtocolError`. Python returns.
- `set_island_mode` takes only modes 1 and 6, on LAN and cloud, with no coercion or default. `src/energyisland.ts` holds that check and the `REJECTED_*` codes for both paths, and `EnergyDeviceSigner.envelope` repeats the check for a raw `_signed_command`.
- Cloud `set_island_mode` raises `DeviceUnexpectedResponse` on `REJECTED_*`, from the signed envelope or Teslemetry's `{ response: { result } }`. Python returns the reply.
- `connect_gateway` sends nothing to a reported address outside the private and link-local IPv4 ranges (`is_lan_address`), and `expectedDin`, or the cloud site's `din`, refuses any other gateway with a failover-safe error. Python has no such function.
- `write_config` rejects non-finite numbers and runs one at a time per client. A second `curtail` keeps the first's saved values.
- Doc 08 section 6 lists the rest, such as `priority` as a string and no stubs.
- **Same as Python, and easy to mistake for a bug:** `set_island_mode(6)` sends `force: true` on LAN, where it defaults from the mode, and omits `force` on cloud. Only `go_off_grid` forces there.
- `Powerwall*` errors extend `Error`, not `TeslaFleetError`. Only `failover_safe = true` on connection, authentication and rate-limit errors lets a router fail over.
- `PowerwallEnergySite` omits what has no local mapping instead of stubbing it, so a router skips it. `priority` is a decimal string, because `2^64 - 1` does not fit a number.
- Golden envelopes (`test/fixtures/powerwall-envelopes.json`, from the real Python client; never regenerate them here) differ from Node in two expected ways, both asserted in `test/powerwall/golden.test.ts`: `reconnect_grid` omits `force = false`, and a `config.json` write has `JSON.stringify` spacing. `src/powerwall/queries.ts` is pinned by SHA-256: a client cannot sign a new query.
- An empty `MessageEnvelope` encodes to zero bytes, which `post_v1r` rejects, so a fake reply meaning "no response field" must still carry a category, as `{ envelope: { teg: {} } }` does.
- `LiveStatusMerge` and `SiteInfoMerge` own no timer, and `test/powerwall/merge.test.ts` fails if they start one: the caller drives `poll()`. A `SiteInfoMerge` read that a command landed in is discarded by `generation`, because it predates the command's value, but it still updates `healthy`.
- `connect_gateway` persists nothing: `moved` hands the caller a `host` to save. Every outcome returns a client, so a stale one fails over to the cloud rather than breaking the site. Registration and discovery use the cloud backend directly, never the router.
- `scripts/powerwall-readonly-check.ts` runs on a real home's gateway, so it must stay unable to write and must print only key names, types and pass or fail. `test/powerwall/readonly-check.test.ts` checks this on its source, at run time and on the wire, from an allowlist: a new client or site method counts as a write until added to `READS` there, and a new write must join the table in `docs/powerwall.md`. `tsconfig.test.json` also compiles `scripts/*.ts`, so the script's `Pick` types make a write a compile error.

## Errors (`src/exceptions.ts`)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class MyApi extends Teslemetry {
}
```

## Local Powerwall

A client for a Powerwall gateway on your LAN, with routing to the cloud and merge helpers, is built but not published: `tesla-fleet-api/powerwall` is not importable yet. It ships once a read-only check has passed on a real gateway. Its writes are not yet verified from Node on hardware. See [docs/powerwall.md](docs/powerwall.md).

## OAuth

```ts
Expand Down
141 changes: 141 additions & 0 deletions docs/powerwall.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Local Powerwall

Control a Powerwall gateway over your LAN, with the cloud as a fallback.

**Status: not published.** `tesla-fleet-api/powerwall` is not in the package's `exports` map yet, so it cannot be imported from npm. It ships after the read-only check below has passed on a real gateway. The examples show the import it will have.

**No write has run from Node on real hardware.** Each write below has only run against a fake gateway in this repository's tests. Python has run the same commands on a Powerwall 3, but that does not prove Node's bytes reach a gateway the same way. Treat every write as not yet verified, and read the state back after using one.

## Before you start

You need:

- The gateway's LAN address. A Teslemetry site can look it up with `find_gateway_address()`. `192.168.91.1` is the gateway's own Wi-Fi access point, and is only useful to a device joined to it.
- The gateway password. The client sends its last 5 characters. Pass the full password either way, because an early Gateway 1 unit needs all of it (use `PowerwallClient.legacy_gateway`).
- An RSA private key that is already paired with the gateway. Pairing goes through the cloud and needs a physical breaker toggle: see `add_authorized_client` and `wait_until_paired` on `TeslemetryEnergySite`.

The gateway's certificate is self-signed, so the client accepts any certificate, on its own connection only. Replies are not signed either. A device that can intercept your LAN traffic can read the last 5 password characters and forge readings. It cannot forge a signed command.

Keep the key and password out of anything readable. On Homey, do not store them in `/userdata`, which is publicly served.

## Reading

```ts
import { PowerwallClient, PowerwallEnergySite, verify_local_gateway } from "tesla-fleet-api/powerwall";

const client = new PowerwallClient({ host, gatewayPassword, rsaPrivateKeyPem });
await verify_local_gateway(client);

const site = new PowerwallEnergySite(client);
const { response } = await site.live_status();
```

`verify_local_gateway` proves the password and the key separately. A plain `PowerwallAuthenticationError` means the password is wrong. `PowerwallKeyRejectedError`, a subclass, means the password worked and the key did not.

`PowerwallEnergySite` has the cloud site's method names and reply shapes. Keys `live_status` has no local source for are `null`, never guessed. Methods with no local mapping, such as `site_info` and `storm_mode`, are absent.

## With the cloud as a fallback

```ts
import { connect_gateway } from "tesla-fleet-api/powerwall";
import { EnergySiteRouter } from "tesla-fleet-api/router";

const { client, host, outcome } = await connect_gateway({
client: { host: storedHost, gatewayPassword, rsaPrivateKeyPem },
cloud: teslemetrySite,
});
if (outcome === "moved") {
await saveHost(host); // `connect_gateway` persists nothing.
}

const router = new EnergySiteRouter([new PowerwallEnergySite(client), teslemetrySite]);
await router.live_status();
```

The cloud's answer is not trusted. An address outside the private and link-local IPv4 ranges gets no request, so a bad reply cannot collect the password. When the DIN is known, from `expectedDin` or the cloud site's `din`, a gateway with another DIN is refused. It has seen the login by then, because reading the DIN needs a token.

Build the router whatever the outcome. A client that cannot connect fails over to the cloud on each call, so the site degrades to cloud control.

| Outcome | Meaning | What to do |
| -- | -- | -- |
| `connected` | Connected at the stored host | Nothing |
| `rate_limited` | The gateway answered 429 or 503 | Nothing: it is there |
| `moved` | Connected at a new host | Persist `host` |
| `refused` | The cloud confirms the address and the gateway refused. A wrong password lands here. | Ask for the password again |
| `not_found` | No usable address | Offer a repair |

Only some errors fail over:

| Local error | Fails over? |
| -- | -- |
| `PowerwallConnectionError`, `PowerwallRateLimitError`, `PowerwallAuthenticationError` | Yes |
| `PowerwallCommandError`, `PowerwallNotSupportedError`, `PowerwallProtocolError`, `PowerwallFaultError` | No. The gateway answered, and the cloud reaches the same gateway. |

- A method the local site lacks goes straight to the cloud.
- A failed-over write can run twice. To avoid that, call `router.primary.powerwall` directly.
- `operation`, `backup` and `grid_import_export` all write `config.json`. Do not split them across backends, or one write can stomp another.
- `add_authorized_client` and `find_gateway_address` are registration and discovery. Call them on `router.secondary`.

## Polling and merging

`LiveStatusMerge` and `SiteInfoMerge` keep the latest cloud document and local reading, and overlay the keys local owns. They own no timer: call `poll()` on your own schedule, and attach a rejection handler.

```ts
const live = new LiveStatusMerge(site, cloudLiveStatus);
setInterval(() => {
live.poll().then(publish).catch(reportBug);
}, ENERGY_LIVE_INTERVAL_MS);

live.set_cloud(nextCloudLiveStatus);
live.healthy; // null before the first poll, then true or false.
live.last_error;
```

- `poll()` resolves to the merged view, or `null` for a skipped tick. It rejects only on a bug. A failed read resolves, and falls back to cloud values.
- `LiveStatusMerge` backs off after failures: it tries every 2, 4, 8, then 12 ticks, and resets on a success.
- A zero reading overlays. A `null` does not.
- Pass `owned_keys` to leave a key at its cloud value.
- After a successful command, call `SiteInfoMerge.hold_command_value(key, value)`. It keeps the value through a poll that started before the command, and through a failed poll, until its source next reports the key.

## Writes

**Every method in this table is not yet verified from Node on hardware.**

| `PowerwallEnergySite` | `PowerwallClient` | Notes |
| -- | -- | -- |
| `operation` | `set_operation_mode` | Writes `config.json` |
| `backup` | `set_backup_reserve`, `set_backup_reserve_raw` | Writes `config.json`. `backup` takes the app's scale. |
| `grid_import_export` | `set_grid_import_export`, `set_export_rule`, `set_grid_charging` | Writes `config.json`. Both settings in one call is one write. |
| `set_island_mode`, `go_off_grid`, `reconnect_grid` | `set_island_mode`, `go_off_grid`, `reconnect_grid` | Moves the grid contactor. Takes mode 1 or 6 only. A success-shaped reply does not prove it moved: read `control.islanding.contactorClosed` afterwards. |
| `schedule_backup_event`, `cancel_backup_event` | `schedule_max_backup`, `cancel_max_backup` | Always starts now at maximum priority |
| `dispatch_battery_power`, `resume_battery_operation` | `dispatch_battery_power`, `resume_battery_operation` | Use short durations and renew them. The gateway refuses above its limit. |
| `remove_authorized_client` | `remove_authorized_client` | Removing the signing key locks this client out |
| | `write_config` | Any `config.json` key, by dotted path |
| | `set_tou_mode` | The gateway does not validate this value |
| | `set_on_grid_solar_curtailment`, `set_import_limit`, `set_export_limit` | Writes `config.json` |
| | `curtail`, `restore_from_curtailment` | Takes about 90 seconds to apply. The saved state does not survive a restart. |
| | `delay_battery_calibration`, `configure_ocpp` | Firmware may answer `PowerwallNotSupportedError` |
| | `trigger_islanding` | Black start. No hardware result is recorded, even from Python. |

A signed request executes again if it is re-sent before it expires, so a write is sent once and never retried.

Storm mode and the tariff schedule are not local. Use the cloud for those.

## The read-only check

`scripts/powerwall-readonly-check.ts` runs steps R1 to R12 of the live plan against a real gateway. It imports from `src/`, so it runs from a checkout:

```sh
POWERWALL_HOST=<address> \
POWERWALL_PASSWORD_PATH=<file holding the password> \
POWERWALL_KEY_PATH=<file holding the RSA key PEM> \
pnpm powerwall:check
```

- It has no code path to a write. `test/powerwall/readonly-check.test.ts` enforces that on its source, at run time, and on the wire.
- It prints key names, types and pass or fail, never a value, so its output is safe to share.
- It waits 1 second between steps and stops at the first unexpected error.
- R12 reads `live_status` every 5 seconds for 10 minutes, and prints how many `ECONNRESET` and rate-limit errors it saw. Set `POWERWALL_SOAK_TICKS` to shorten it.
- It exits 0 on a pass, 1 on a failed step and 2 on bad setup.

R13 (the same reads from Python) and R14 (`connect_gateway` with a real cloud lookup) are not in the script. R14 needs cloud credentials, which this script does not take.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"eslint": "^10.8.0",
"publint": "^0.3.23",
"tsdown": "^0.23.4",
"tsx": "^4.23.15",
"typescript": "^5.9.2",
"typescript-eslint": "^8.66.0",
"vitest": "^4.1.10"
Expand All @@ -32,7 +33,8 @@
"pack:check": "publint && attw --pack . --profile node16",
"changeset": "changeset",
"ci:publish": "changeset publish",
"build": "tsdown"
"build": "tsdown",
"powerwall:check": "tsx scripts/powerwall-readonly-check.ts"
},
"publishConfig": {
"access": "public",
Expand Down
Loading
Loading