feat(devices): add inverter profiles

An inverter profile describes the technical data of an inverter model: its
nominal AC power, the DC→AC and AC→DC efficiency curves over the load
fraction (AC power relative to the nominal power), the AC charge power
limit of the model, whether that limit also limits PV charging, and an
optional power derating over the ambient temperature. Curves are read
linearly and clamped at their ends, so every lookup yields a value. The
packaged profile generic-hybrid has typical hybrid inverter efficiencies,
no nominal power (the inverter device has to provide it) and full power up
to 45 degC.

Inverter settings name their profile (inverter_profile, default
generic-hybrid) and hold the differences of the unit in profile_adjustment:
nominal_power_w (replaces the profile value, required for a generic
profile), max_power_w and max_ac_charge_power_w (installation limits that
can only lower; an AC charge limit of 0 disables AC charging). The
effective parameters are resolved at the ambient temperature of the
inverter, given by the new measurement key <device_id>-temperature-degc
(45 degC without a fresh value); the power derating lowers the maximum
power and the AC charge limit.

Optimization parameters are always valid: the GENETIC and GENETIC0
parameter preparation resolve the inverter with
resolve_current_operating_parameters_or_defaults, which never raises. An
unusable profile gives the generic-hybrid profile, a missing nominal power
the fallback of 10000 W, an inverter derated to zero power the parameters
at 45 degC, and no usable profile at all the profile adjustment with
constant efficiency; each is logged as error. The GENETIC inverter takes
the AC→DC efficiency curve of the profile, solved exactly on each linear
piece of the curve (about 2 % longer runs); GENETIC0 keeps constant
efficiencies and no longer replaces the configured inverters with the demo
inverter; a battery, electric vehicle or inverter that cannot be prepared
is left out of the GENETIC0 run, never replaced by a demo device.

Configurations migrate: max_power_w becomes
profile_adjustment.nominal_power_w, max_ac_charge_power_w moves into the
adjustment, an ac_to_dc_efficiency of 0 keeps AC charging disabled, and
the efficiency fields are dropped with an error log, as for batteries, so
the profile efficiencies apply. Home Assistant config paths follow.

Additional changes
- Move the curve algorithms of the battery profile into profilecurve, so
  battery and inverter profiles share them; the battery profile keeps its
  document format and classes.
- Move ProfileBaseModel, the strict base of profile documents, into
  profilestore.
- Serve inverter profiles through the profile REST API. Refuse to delete a
  profile that a configured device uses and that has no packaged profile
  to revert to (409).
- Share the measurement access of the device settings in
  devicemeasurement.
- Rename check_battery_profiles to check_device_profiles; the device
  profile check at configuration setup covers inverters, too.
- Document inverter profiles, their configuration, defaults and sharing;
  regenerate the configuration and OpenAPI documentation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
Bobby Noelte
2026-11-11 22:08:18 +01:00
co-authored by Claude Opus 5.5
parent e255f987bf
commit faf37abfe7
45 changed files with 3235 additions and 784 deletions
+103 -56
View File
@@ -1,9 +1,10 @@
"""Tests for the load-dependent DC-to-AC efficiency curve of the GENETIC inverter.
"""Tests for the load-dependent efficiency curves of the GENETIC inverter.
``dc_to_ac_efficiency_curve`` replaces the constant ``dc_to_ac_efficiency`` by a
curve over the load fraction (AC power / rated power). Real inverters are much
less efficient at low load, which matters for night-time discharge into a small
base load. Without a curve the behaviour must stay exactly the legacy one.
base load. ``ac_to_dc_efficiency_curve`` does the same for charging the battery
from the grid. Without a curve the behaviour must stay exactly the legacy one.
"""
from types import SimpleNamespace
@@ -15,12 +16,16 @@ from pydantic import ValidationError
from akkudoktoreos.devices.devicesabc import (
interpolate_efficiency_curve,
solve_ac_for_charge_dc_energy,
solve_ac_for_dc_energy,
validate_efficiency_curve,
)
from akkudoktoreos.devices.genetic.battery import Battery, SolarPanelBatteryParameters
from akkudoktoreos.devices.genetic.inverter import Inverter, InverterParameters
from akkudoktoreos.devices.settings.invertersettings import InverterCommonSettings
from akkudoktoreos.devices.settings.invertersettings import (
InverterCommonSettings,
InverterProfileAdjustment,
)
from akkudoktoreos.optimization.genetic.genetic import GeneticSimulation
from akkudoktoreos.optimization.genetic.geneticparams import (
GeneticEnergyManagementParameters,
@@ -113,10 +118,6 @@ class TestCurveValidation:
InverterParameters(
device_id="inverter1", max_power_wh=1000, dc_to_ac_efficiency_curve=curve
)
with pytest.raises(ValidationError):
InverterCommonSettings(
device_id="inverter1", max_power_w=1000, dc_to_ac_efficiency_curve=curve
)
@pytest.mark.parametrize("fraction", [-0.1, 1.1])
def test_reference_load_fraction_range(self, fraction):
@@ -416,62 +417,36 @@ def test_low_load_curve_increases_losses(config_eos):
class TestSettings:
def test_settings_pass_curve_to_genetic_parameters(self):
settings = InverterCommonSettings.model_validate(
{
"device_id": "inverter1",
"max_power_w": 5000,
"dc_to_ac_efficiency_curve": [[0.02, 0.8], [0.07, 0.93], [1.0, 0.95]],
"dc_to_ac_efficiency_reference_load_fraction": 0.1,
}
)
params = settings.to_genetic_param()
assert params.dc_to_ac_efficiency_curve == [(0.02, 0.8), (0.07, 0.93), (1.0, 0.95)]
assert params.dc_to_ac_efficiency_reference_load_fraction == 0.1
"""The DC→AC curve of the GENETIC parameters comes from the inverter profile."""
def test_defaults_keep_constant_efficiency(self):
params = InverterCommonSettings(device_id="inverter1", max_power_w=5000).to_genetic_param()
assert params.dc_to_ac_efficiency_curve is None
assert params.dc_to_ac_efficiency_reference_load_fraction == pytest.approx(0.06)
def test_genetic0_is_unaffected(self):
def test_settings_pass_profile_curve_to_genetic_parameters(self, config_eos):
settings = InverterCommonSettings(
device_id="inverter1",
max_power_w=5000,
dc_to_ac_efficiency_curve=[(0.0, 0.8), (1.0, 0.95)],
profile_adjustment=InverterProfileAdjustment(nominal_power_w=5000),
dc_to_ac_efficiency_reference_load_fraction=0.1,
)
profile = settings.get_inverter_profile()
params = settings.to_genetic_param()
assert params.dc_to_ac_efficiency_curve is not None
# Not derated at 45 degC: the curve is the profile curve over [0, 1].
expected = list(zip(profile.dc_to_ac_efficiency.x, profile.dc_to_ac_efficiency.y))
assert [x for x, _ in params.dc_to_ac_efficiency_curve] == pytest.approx(
[x for x, _ in expected]
)
assert [y for _, y in params.dc_to_ac_efficiency_curve] == pytest.approx(
[y for _, y in expected]
)
assert params.dc_to_ac_efficiency_reference_load_fraction == 0.1
assert params.dc_to_ac_efficiency == pytest.approx(profile.dc_to_ac_efficiency_at(0.1))
def test_genetic0_has_no_curve(self, config_eos):
settings = InverterCommonSettings(
device_id="inverter1",
profile_adjustment=InverterProfileAdjustment(nominal_power_w=5000),
)
params = settings.to_genetic0_param()
assert not hasattr(params, "dc_to_ac_efficiency_curve")
def test_config_accepts_curve(self, config_eos):
config_eos.merge_settings_from_dict(
{
"devices": {
"inverters": {
"inverter1": {
"max_power_w": 5000,
"dc_to_ac_efficiency_curve": [[0.02, 0.8], [0.07, 0.93], [1.0, 0.95]],
}
}
}
}
)
inverters = config_eos.devices.inverters
assert inverters is not None
assert inverters["inverter1"].dc_to_ac_efficiency_curve == [
(0.02, 0.8),
(0.07, 0.93),
(1.0, 0.95),
]
config_eos.set_nested_value(
"devices/inverters/inverter1/dc_to_ac_efficiency_curve", [[0.0, 0.85], [1.0, 0.95]]
)
assert inverters["inverter1"].dc_to_ac_efficiency_curve == [(0.0, 0.85), (1.0, 0.95)]
with pytest.raises(ValueError, match="at least two points"):
config_eos.set_nested_value(
"devices/inverters/inverter1/dc_to_ac_efficiency_curve", [[0.5, 0.9]]
)
# ---------------------------------------------------------------------------
# Discharge limited by the battery (low state of charge, power limit)
@@ -664,3 +639,75 @@ class TestBatteryLimitedDischarge:
)
ac, losses = inverter._discharge_battery_to_ac(500.0 * slot_duration_h, 0)
assert ac == pytest.approx(200.0 * slot_duration_h * efficiency)
# ---------------------------------------------------------------------------
# AC→DC curve for charging the battery from the grid
# ---------------------------------------------------------------------------
CHARGE_CURVE = [(0.05, 0.86), (0.3, 0.96), (1.0, 0.95)]
class TestSolveAcForChargeDcEnergy:
@pytest.mark.parametrize("dc_wh", [1.0, 30.0, 100.0, 250.0, 500.0, 900.0, 2000.0])
def test_solution_is_consistent(self, dc_wh):
ac_wh = solve_ac_for_charge_dc_energy(CHARGE_CURVE, dc_wh, 1000.0)
efficiency = interpolate_efficiency_curve(CHARGE_CURVE, ac_wh / 1000.0)
assert ac_wh * efficiency == pytest.approx(dc_wh, rel=1e-9)
def test_clamped_ends(self):
# Below the first point the first efficiency applies, above the last the last.
assert solve_ac_for_charge_dc_energy(CHARGE_CURVE, 8.6, 1000.0) == pytest.approx(10.0)
assert solve_ac_for_charge_dc_energy(CHARGE_CURVE, 1900.0, 1000.0) == pytest.approx(2000.0)
def test_flat_curve_equals_constant_efficiency(self):
flat = [(0.0, 0.9), (1.0, 0.9)]
assert solve_ac_for_charge_dc_energy(flat, 450.0, 1000.0) == pytest.approx(500.0)
def test_no_energy(self):
assert solve_ac_for_charge_dc_energy(CHARGE_CURVE, 0.0, 1000.0) == 0.0
class TestChargeFromGridWithCurve:
def test_curve_is_validated(self):
with pytest.raises(ValidationError):
InverterParameters(
device_id="inverter1", max_power_wh=1000, ac_to_dc_efficiency_curve=[(0.5, 0.9)]
)
@staticmethod
def _empty_battery() -> Battery:
battery = _real_battery(soc_percentage=0)
battery.charge_array = np.ones(1)
return battery
def test_low_load_charge_draws_more_ac(self):
flat = _real_inverter(self._empty_battery(), ac_to_dc_efficiency=0.96)
curved = _real_inverter(
self._empty_battery(),
ac_to_dc_efficiency=0.96,
ac_to_dc_efficiency_curve=CHARGE_CURVE,
)
# 5 % of the 1 kW battery charge power: a low AC load, efficiency ~0.87.
flat_ac, flat_losses = flat.charge_battery_from_grid(0, 0.05)
curved_ac, curved_losses = curved.charge_battery_from_grid(0, 0.05)
assert flat_ac == pytest.approx(50.0 / 0.96)
assert curved_ac > flat_ac
assert curved_ac * interpolate_efficiency_curve(
CHARGE_CURVE, curved_ac / 1000.0
) == pytest.approx(50.0)
assert curved_losses == pytest.approx(curved_ac - 50.0)
def test_settings_pass_the_profile_curve(self, config_eos):
settings = InverterCommonSettings(
device_id="inverter1",
profile_adjustment=InverterProfileAdjustment(nominal_power_w=5000),
)
profile = settings.get_inverter_profile()
params = settings.to_genetic_param()
assert params.ac_to_dc_efficiency_curve is not None
assert [y for _, y in params.ac_to_dc_efficiency_curve] == pytest.approx(
list(profile.ac_to_dc_efficiency.y)
)
# The constant is the efficiency at the AC charge limit, here the nominal power.
assert params.ac_to_dc_efficiency == pytest.approx(profile.ac_to_dc_efficiency_at(1.0))