Skip to content

Add energy site dispatch_battery_power and resume_battery_operation - #193

Merged
Bre77 merged 1 commit into
mainfrom
fm/tfa-energy-dispatch
Oct 28, 2026
Merged

Bre77 merged 1 commit into
mainfrom
fm/tfa-energy-dispatch

Conversation

@Bre77

@Bre77 Bre77 commented Oct 28, 2026

Copy link
Copy Markdown
Member

Summary

Adds dispatch_battery_power(power_watts, duration_seconds) and resume_battery_operation() to the energy site classes.

  • EnergySite (Tesla Fleet API): sends POST /api/1/energy_sites/{id}/command with the grpc_command envelope, identifier_type 1 (gateway DIN). The message is teg.dispatch_battery_power_request with a real_power leaf (power_watts, duration_seconds) or a resume_device_mode leaf. Needs the energy_cmds scope.
  • TeslemetryEnergySite: calls the Teslemetry routes POST .../command/dispatch_battery_power (body {"power_watts", "duration_seconds"}) and POST .../command/resume_battery_operation, added in Teslemetry/api#886.
  • Router compatibility: both methods use aiopowerwall's local names, argument names, units (watts, seconds), sign convention (positive discharges, negative charges) and duration limit (1 to 86400). validate_dispatch_battery_power copies aiopowerwall's checks, so each backend raises the same ValueError before any request. EnergySiteRouter already routes any method that both backends have through Router.__getattr__, the same way it routes operation() and backup(). So router/energysite.py needs no code change: it tries the local primary first and fails over to the cloud with the same arguments.

A failover that repeats dispatch_battery_power is safe. A new call replaces the active setpoint (power and timer), and resume is idempotent.

Follow-up outside this repo

aiopowerwall's PowerwallEnergySite adapter (the router's local primary) does not expose these two methods yet; only PowerwallClient has them. Until the adapter gets them, EnergySiteRouter sends both commands to the cloud backend.

Tests

tests/test_energysite_battery_dispatch.py: the exact request bodies for both classes, 12 invalid argument pairs that raise without a request, signature parity with an aiopowerwall-shaped local site, and router routing (local first, cloud failover to Teslemetry and to Fleet gRPC).

  • testgate uv run pytest tests/test_energysite_battery_dispatch.py tests/test_router.py tests/test_energysite_island_mode.py: 50 passed
  • uv run pyright tesla_fleet_api: 0 errors
  • uv run ruff check: clean

No live command was sent from this change. The captain has confirmation that the cloud command works.

Fleet API EnergySite sends teg.dispatch_battery_power_request (real_power or
resume_device_mode leaf) through the grpc_command envelope. TeslemetryEnergySite
calls the new Teslemetry custom routes. Both match aiopowerwall's local method
names, arguments, units, sign convention and 1..86400 s duration limit, so
EnergySiteRouter routes them local-first with cloud failover.
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between d3e393d and 4d13d96.

Files selected for processing: 5
  • docs/fleet_api_energy_sites.md
  • docs/teslemetry.md
  • tesla_fleet_api/tesla/energysite.py
  • tesla_fleet_api/teslemetry/energysite.py
  • tests/test_energysite_battery_dispatch.py

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Walkthrough

Energy-site APIs now provide timed battery dispatch and setpoint resume commands. Dispatch arguments are validated before requests. Fleet and Teslemetry implementations send provider-specific commands, and tests cover request payloads and router fallback.

Changes: Battery Dispatch Commands

Layer / File(s) Summary
Provider commands and validation
tesla_fleet_api/tesla/energysite.py, tesla_fleet_api/teslemetry/energysite.py, tests/test_energysite_battery_dispatch.py, docs/fleet_api_energy_sites.md, docs/teslemetry.md
Fleet and Teslemetry energy sites add dispatch and resume methods. Shared validation rejects invalid power values and durations before dispatch requests. Tests check provider requests and invalid arguments. The documentation describes the commands, limits, and examples.
Router dispatch and fallback
tests/test_energysite_battery_dispatch.py
Tests verify local-first dispatch and resume calls, and cloud fallback when local calls raise ConnectionError.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant EnergySiteRouter
  participant LocalEnergySite
  participant TeslemetryEnergySite
  participant FleetEnergySite
  EnergySiteRouter->>LocalEnergySite: dispatch or resume
  alt Local call succeeds
    LocalEnergySite-->>EnergySiteRouter: result
  else Local call raises ConnectionError
    alt Teslemetry fallback
      EnergySiteRouter->>TeslemetryEnergySite: dispatch or resume
      TeslemetryEnergySite-->>EnergySiteRouter: result
    else Fleet fallback
      EnergySiteRouter->>FleetEnergySite: dispatch or resume
      FleetEnergySite-->>EnergySiteRouter: result
    end
  end
Loading
Merge Risk: Minimal · up to 4d13d

No actionable merge-blocking risk was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the two new public methods, dispatch_battery_power and resume_battery_operation, on the energy site classes. The raw summary and PR objectives confirm these methods are the main change…
Description check Passed The description states that the PR adds dispatch_battery_power and resume_battery_operation to the energy site classes. It also covers the Fleet API endpoint, Teslemetry routes, router compatibility w…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two new public methods, dispatch_battery_power and resume_battery_operation, on the energy site classes. The raw summary and PR objectives confirm these methods are the main change, so the title matches the changeset. It is specific and concise.

Full details: Description check

Explanation

The description states that the PR adds dispatch_battery_power and resume_battery_operation to the energy site classes. It also covers the Fleet API endpoint, Teslemetry routes, router compatibility with aiopowerwall, tests, and the follow-up work outside this repo. It is closely related to the changeset.

Full details: Docstring Coverage

Explanation

Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 3 files. (2 skipped: 2 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@Bre77
Bre77 enabled auto-merge (squash) October 28, 2026 02:20
@Bre77
Bre77 merged commit c0e29bc into main Oct 28, 2026
7 checks passed
@Bre77
Bre77 deleted the fm/tfa-energy-dispatch branch October 28, 2026 02:27
@Bre77 Bre77 mentioned this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant