feat(router): add local-Powerwall key-ownership merge contract - #145
Merged
Conversation
Add merge_local_into_cloud plus LOCAL_LIVE_STATUS_KEYS/LOCAL_SITE_INFO_KEYS to router/energysite.py so a consumer can overlay local Powerwall readings onto a cloud response without clobbering fields the local gateway can't serve. PowerwallEnergySite.live_status() returns all cloud keys with None for unservable ones, so presence-in-response can't be the ownership test - a fixed owned-key set is what makes the overlay safe and lets a local outage fall back to the cloud value. Router dispatch is unchanged; this is a plain function next to EnergySiteRouter, not new Router/health logic. Claude-Session: https://claude.ai/code/session_01B18NQ2RAaJAX2rsiU31Qn6
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
Add the local-Powerwall key-ownership contract to tesla-fleet-api, in tesla_fleet_api/router/energysite.py (energy-specific; router/base.py stays entity-agnostic). Define LOCAL_LIVE_STATUS_KEYS (solar_power, energy_left, total_pack_energy, percentage_charged, battery_power, load_power, grid_power, generator_power, grid_status, island_status) and LOCAL_SITE_INFO_KEYS (backup_reserve_percent, default_real_mode) as frozensets, plus merge_local_into_cloud(cloud, local, owned_keys) -> dict that overlays owned_keys present in local onto a copy of cloud; every other key keeps its cloud value. Exact semantics: returns a NEW dict (never mutates inputs); when local is None the result equals cloud; only keys in owned_keys AND present in local are overlaid; a key in owned_keys absent from local keeps the cloud value; a falsy-but-present local value (e.g. grid_power: 0) IS overlaid; keys in local outside owned_keys are ignored. Export all three from the package's public surface the same way EnergySiteRouter is exported (router/init.py and tesla/init.py).
Added a short WHY note in the function docstring: PowerwallEnergySite.live_status() returns all 16 cloud keys with None for the 6 it cannot serve, so presence-in-response cannot be the ownership test - a fixed owned-key set lets a consumer overlay local readings without clobbering cloud values, and lets a local outage fall back to cloud values rather than unavailable.
Added a 'Merge contract' subsection to docs/energy_local_control.md next to the EnergySiteRouter walkthrough (section 5), covering the two key sets, the one function, the fallback-to-cloud rule, and that per-command router dispatch is NOT the merge (calling router.live_status() returns one whole source, not a blend).
Added unit tests (tests/test_energysite_merge_contract.py) covering the five semantics above (local=None -> cloud values, owned+present overlaid, owned+absent keeps cloud, falsy-but-present overlaid, unowned local keys ignored) plus no-mutation and new-dict checks.
Deliberately did NOT touch Router dispatch, did NOT add health logic (stays with the consumer), did NOT add new classes. Deliberately did NOT bump the package version: this repo's release history shows version bumps happen as their own dedicated commits/PRs, not bundled into feature PRs, so pyproject.toml/init.py/uv.lock are left unchanged and the PR body should say so explicitly.
What Changed
LOCAL_LIVE_STATUS_KEYSandLOCAL_SITE_INFO_KEYSfrozensets and amerge_local_into_cloud(cloud, local, owned_keys) -> dictfunction intesla_fleet_api/router/energysite.py, which overlays only the owned keys present inlocalonto a copy ofcloud(returns a new dict, never mutates inputs, falls back tocloudfor absent/owned-missing keys and whenlocal is None, and ignores unowned local keys).LOCAL_LIVE_STATUS_KEYS,LOCAL_SITE_INFO_KEYS, andmerge_local_into_cloudfromtesla_fleet_api/router/__init__.pyandtesla_fleet_api/tesla/__init__.py, following the same public-surface pattern asEnergySiteRouter.docs/energy_local_control.mdalongside theEnergySiteRouterwalkthrough, covering the two key sets, the merge function's fallback-to-cloud behavior, and the distinction fromRouterper-command dispatch (which returns one whole source, not a blend).tests/test_energysite_merge_contract.pycovering:local=Noneyields cloud values, owned+present keys are overlaid, owned+absent keys keep cloud values, falsy-but-present local values are overlaid, unowned local keys are ignored, and no mutation of inputs occurs with a new dict returned.No package version bump is included; per this repo's release process, version bumps ship as their own dedicated commits/PRs, so
pyproject.toml,tesla_fleet_api/__init__.py, anduv.lockare unchanged.Risk Assessment
✅ Low: Small, additive, well-isolated change (new pure function + two frozensets + exports + docs + tests) matching the stated intent exactly, with no modification to existing Router/dispatch logic or call sites.
Testing
Ran the new focused unit test suite (8/8 pass) and manually exercised the exported public API end-to-end (import from both tesla_fleet_api.tesla and tesla_fleet_api.router, merge behavior with falsy overlay, unowned-key exclusion, None-fallback, and non-mutation all verified live); confirmed no version-bump files were touched. All acceptance criteria in the user intent are satisfied with no findings.
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 pytest tests/test_energysite_merge_contract.py -v— all 8 tests pass, covering local=None fallback, owned+present overlay, owned+absent keeps cloud, falsy-but-present overlay, unowned-key ignore, no-mutation, new-dict identity, and site_info keysManual end-to-end script importing merge_local_into_cloud/LOCAL_LIVE_STATUS_KEYS from bothtesla_fleet_api.teslaandtesla_fleet_api.routerpublic surfaces, confirming identical function object, correct merge output, cloud-fallback on local=None, and cloud dict left unmutatedgit diff f4c773c..24f73c7 --stat -- pyproject.toml uv.lock tesla_fleet_api/__init__.py— confirmed empty, verifying no version-bump files were touched as the intent required✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.