Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion tesla_fleet_api/router/energysite.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ def merge_local_into_cloud(
ownership test — a fixed owned-key set lets a caller overlay local
readings without clobbering cloud values, and lets a local outage fall
back to the cloud value instead of an unavailable one.

A key overlays only when it is owned, present in ``local``, and
``local[key] is not None``; ``None`` means "not served this tick" and the
cloud value is kept, while any other falsy value (``0``, ``False``, ``""``)
still overlays.
"""
merged = dict(cloud)
if local is None:
return merged
for key in owned_keys:
if key in local:
if key in local and local[key] is not None:
merged[key] = local[key]
return merged

Expand Down
13 changes: 13 additions & 0 deletions tests/test_energysite_merge_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def test_falsy_but_present_local_value_is_overlaid(self) -> None:
result = merge_local_into_cloud(cloud, local, LOCAL_LIVE_STATUS_KEYS)
self.assertEqual(result["grid_power"], 0)

def test_falsy_bool_local_value_is_overlaid(self) -> None:
cloud = {"island_status": "on_grid"}
local = {"island_status": False}
result = merge_local_into_cloud(cloud, local, LOCAL_LIVE_STATUS_KEYS)
self.assertEqual(result["island_status"], False)

def test_owned_key_present_with_none_keeps_cloud_value(self) -> None:
cloud = {"solar_power": 100, "grid_power": 50}
local = {"solar_power": None, "grid_power": 75}
result = merge_local_into_cloud(cloud, local, LOCAL_LIVE_STATUS_KEYS)
self.assertEqual(result["solar_power"], 100)
self.assertEqual(result["grid_power"], 75)

def test_local_keys_outside_owned_set_are_ignored(self) -> None:
cloud = {"solar_power": 100}
local = {"solar_power": 200, "some_unowned_field": "surprise"}
Expand Down
Loading