Loadpoint: fix fast charging phase scaling#27972
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
TestFastChargingCircuitPhaseScaling, thefor _, tc := range tcloop closes over the loop variable int.Run, which can cause all subtests to use the last table entry; add atc := tcinside the loop beforet.Runto capture the value per iteration. - The test mutates the global
Voltagevariable without restoring it, which can lead to test ordering issues; consider saving the previous value and deferring its restoration inside the test. - The
4140constant used for the 3‑phase minimum power edge cases in the test seems derived fromVoltage * minCurrent * 3; computing it dynamically instead of hardcoding would make the test more robust to future changes inVoltageorminCurrent.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `TestFastChargingCircuitPhaseScaling`, the `for _, tc := range tc` loop closes over the loop variable in `t.Run`, which can cause all subtests to use the last table entry; add a `tc := tc` inside the loop before `t.Run` to capture the value per iteration.
- The test mutates the global `Voltage` variable without restoring it, which can lead to test ordering issues; consider saving the previous value and deferring its restoration inside the test.
- The `4140` constant used for the 3‑phase minimum power edge cases in the test seems derived from `Voltage * minCurrent * 3`; computing it dynamically instead of hardcoding would make the test more robust to future changes in `Voltage` or `minCurrent`.
## Individual Comments
### Comment 1
<location path="core/loadpoint_phases_test.go" line_range="484" />
<code_context>
}
+
+func TestFastChargingCircuitPhaseScaling(t *testing.T) {
+ Voltage = 230
+
+ tc := []struct {
</code_context>
<issue_to_address>
**issue (testing):** Reset global `Voltage` after the test to avoid leaking state into other tests
This test mutates the package-level `Voltage` and never restores it, which can cause order-dependent test failures elsewhere. Capture the original value and defer restoring it, e.g. `oldVoltage := Voltage; Voltage = 230; defer func() { Voltage = oldVoltage }()` at the start of the test.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
6db4166 to
34534f8
Compare
|
I didn't check the tests, but the implementation looks perfectly sound. It would be great if we could get a test confirmation before merging. Maybe @eliaslecomte? |
34534f8 to
49782c2
Compare
49782c2 to
b4da4fb
Compare
|
Accidentally pushed an invalid local test. Now it's ready. |
|
Yeah, I can't test it this week unfortunately. @eliaslecomte would be great if you could build and run evcc with the changes from this PR. |
|
Nice!
I'm looking at how I can best run this. For now I'm thinking to run it locally on my PC. I would need to copy my config (from home assistant) and do an export/restore backup through the UI. And then await some sun on this cloudy day. |
|
Let‘s merge this for testing. The current implementation on master is clearly wrong and the associated bug is already much too old. Please let us know if this works as expected. |
|
Thank you @mfuchs1984 great PR! |
|
Now that it got merged: I'll install the (home assistant) nightly and test tomorrow. You can expect my test results in the evening. |
|
I think you have to wait for the nightly tomorrow. |
|
New nightly in 30min |
|
Can you do the same later without surplus to verify it switches down? |
|
After charging for half an hour, I noticed that it halted charging, while I believe that it could have lowered phases from 3 to 1, so that it could continue charging. Not sure if I have captured the full relevant log but this might help: |
|
The log starts to late to understand what's going on. |
|
I've retested the scenario of switching to 1 phase while it was already charging with 3 phases by limiting solar panels downwards. |
|
@andig while the change itself seems fine, this might need a phase switching guard timer, like in PV mode, at least before switching up again. |
|
Created #28174 as follow-up, preventing excessive repeated phase switching. |


This should fix the the fastCharging phase-switching logic to take the actual circuit load into account. This makes sure to take available surplus into account as well as circuit nesting.
lp.fastChargingnow used circuit.ValidatePower to determine if the minimum power for 3 phase charging is available. Instead of using the heuristic1.1*maxPower1p, I changed it to explicitly use minPower3p. Everything below will lead to scaling down.Fixes #24160
Replaces #27907
/cc @eliaslecomte
/cc @McFSR
/cc @joachimbaten