logic: add forecast grid charge target strategy#361
logic: add forecast grid charge target strategy#361filiplajszczak wants to merge 1 commit intoMaStr:mainfrom
Conversation
58656a4 to
21c2e72
Compare
There was a problem hiding this comment.
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()withfixedandforecaststrategies. - Adds core config parsing and per-run computation of
effective_min_grid_charge_soc, passing it intoCalculationParametersand 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)] |
There was a problem hiding this comment.
Addressed in f5261de: sparse/non-consecutive forecast dict indices now raise a clear ValueError, covered by a regression test.
| 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 |
21c2e72 to
f5261de
Compare
|
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. |
f5261de to
e054693
Compare
Adds an opt-in
forecastgrid-charge target strategy while keeping the defaultfixedbehavior unchanged.With
forecast,min_grid_charge_socremains a floor, and batcontrol raises the effective target from forecasted expensive-slot net demand, capped bymax_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