Skip to content

logic: add forecast grid charge target strategy#361

Open
filiplajszczak wants to merge 1 commit intoMaStr:mainfrom
filiplajszczak:logic-grid-charge-target-strategy
Open

logic: add forecast grid charge target strategy#361
filiplajszczak wants to merge 1 commit intoMaStr:mainfrom
filiplajszczak:logic-grid-charge-target-strategy

Conversation

@filiplajszczak
Copy link
Copy Markdown
Contributor

@filiplajszczak filiplajszczak commented May 5, 2026

Adds an opt-in forecast grid-charge target strategy while keeping the default fixed behavior unchanged.

With forecast, min_grid_charge_soc remains a floor, and batcontrol raises the effective target from forecasted expensive-slot net demand, capped by max_charging_from_grid_limit.

Also publishes the runtime effective target via MQTT/Home Assistant discovery.

I’ll add the corresponding wiki documentation after merge.

Tests:

  • uv run pytest tests/ → 638 passed
  • pylint → 9.41/10

@MaStr MaStr requested a review from Copilot May 5, 2026 07:31
@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from 58656a4 to 21c2e72 Compare May 5, 2026 07:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an opt-in forecast strategy for determining the effective minimum grid-charge SoC target at runtime, while keeping the default fixed behavior unchanged. The effective target is computed from forecasted net demand during upcoming expensive price slots (with optional PV forecast discounting) and is then passed into the logic layer and exposed via MQTT.

Changes:

  • Introduces batcontrol.logic.grid_charge_target.calculate_effective_grid_charge_soc() with fixed and forecast strategies.
  • Adds core config parsing and per-run computation of effective_min_grid_charge_soc, passing it into CalculationParameters and publishing it via MQTT.
  • Extends MQTT API + discovery and adds unit/integration-style tests for the new behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/batcontrol/logic/grid_charge_target.py New module implementing effective grid-charge SoC target calculation strategies.
src/batcontrol/core.py Parses new config, computes effective target each run, publishes it to MQTT, and feeds it into logic parameters.
src/batcontrol/mqtt_api.py Publishes the effective target and exposes it via Home Assistant MQTT discovery.
tests/batcontrol/logic/test_grid_charge_target.py Unit tests for fixed/forecast target computation and capping behavior.
tests/batcontrol/test_core.py Validates config parsing and verifies core passes/publishes the effective target.
tests/batcontrol/test_mqtt_api.py Tests MQTT publishing + discovery for the new effective target sensor.
config/batcontrol_config_dummy.yaml Documents the new optional config keys in the dummy config.

if isinstance(values, dict):
if not values:
return []
return [values[index] for index in range(max(values.keys()) + 1)]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f5261de: sparse/non-consecutive forecast dict indices now raise a clear ValueError, covered by a regression test.

Comment thread src/batcontrol/core.py
Comment on lines +98 to +106
def _parse_grid_charge_target_strategy(value) -> str:
"""Parse the grid-charge target strategy config value."""
strategy = str(value).lower()
if strategy not in GRID_CHARGE_TARGET_STRATEGIES:
raise ValueError(
f"battery_control.grid_charge_target_strategy must be one of "
f"{GRID_CHARGE_TARGET_STRATEGIES}, got {value!r}"
)
return strategy
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f5261de.

@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from 21c2e72 to f5261de Compare May 5, 2026 07:47
@filiplajszczak
Copy link
Copy Markdown
Contributor Author

Addressed the two Copilot comments in f5261de: forecast dict indices now fail with a clear ValueError/regression test, and grid_charge_target_strategy parsing now strips whitespace before lowercasing.

@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from f5261de to e054693 Compare May 5, 2026 08:06
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.

2 participants