Files
EOS/tests/test_devicesprofileinverter.py
T
Bobby NoelteandClaude Opus 5.5 faf37abfe7 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)
2026-11-11 22:08:18 +01:00

166 lines
6.4 KiB
Python

"""Tests for inverter profile documents and their runtime form."""
import copy
from typing import Any, Iterator
import pytest
from pydantic import ValidationError
from akkudoktoreos.devices.profile.inverterprofile import (
InverterProfile,
InverterProfileSettings,
)
from akkudoktoreos.devices.profile.profilestore import (
ProfileStore,
get_profile_store,
profile_type,
)
DOCUMENT: dict[str, Any] = {
"inverter_model": "Test inverter",
"nominal_power_w": 10000.0,
"max_ac_charge_power_w": 6000.0,
"dc_to_ac_efficiency": {
"load_fraction": [0.1, 0.5, 1.0],
"efficiency": [0.90, 0.97, 0.96],
},
"ac_to_dc_efficiency": {
"load_fraction": [0.1, 1.0],
"efficiency": [0.88, 0.95],
},
"power_derating": {
"temperature_degC": [40.0, 60.0],
"power_fraction": [1.0, 0.5],
},
}
def make_document(**changes: Any) -> dict[str, Any]:
document = copy.deepcopy(DOCUMENT)
document.update(changes)
return document
def make_profile(**changes: Any) -> InverterProfile:
return InverterProfileSettings.model_validate(make_document(**changes)).compile_profile()
class TestDocument:
def test_registered_as_inverter_kind(self):
assert profile_type("inverter") is InverterProfileSettings
def test_minimal_generic_document(self):
document = make_document(nominal_power_w=None, max_ac_charge_power_w=None)
del document["power_derating"]
settings = InverterProfileSettings.model_validate(document)
assert settings.nominal_power_w is None
assert settings.power_derating is None
assert settings.ac_charge_limits_total_charge is False
@pytest.mark.parametrize("field", ["dc_to_ac_efficiency", "ac_to_dc_efficiency"])
def test_efficiency_curves_are_required(self, field):
document = make_document()
del document[field]
with pytest.raises(ValidationError, match=field):
InverterProfileSettings.model_validate(document)
def test_rejects_unknown_keys(self):
with pytest.raises(ValidationError, match="nominal_power_kw"):
InverterProfileSettings.model_validate(make_document(nominal_power_kw=10.0))
@pytest.mark.parametrize(
"curve",
[
{"load_fraction": [0.5, 0.1], "efficiency": [0.9, 0.9]},
{"load_fraction": [-0.1, 1.0], "efficiency": [0.9, 0.9]},
{"load_fraction": [0.1, 1.0], "efficiency": [0.0, 0.9]},
{"load_fraction": [0.1, 1.0], "efficiency": [0.9, 1.1]},
{"load_fraction": [0.1, 0.5, 1.0], "efficiency": [0.9, 0.9]},
{"load_fraction": [0.1], "efficiency": [0.9]},
],
)
def test_rejects_invalid_efficiency_curves(self, curve):
with pytest.raises(ValidationError):
InverterProfileSettings.model_validate(make_document(dc_to_ac_efficiency=curve))
@pytest.mark.parametrize(
"curve",
[
{"temperature_degC": [60.0, 40.0], "power_fraction": [1.0, 0.5]},
{"temperature_degC": [40.0, 60.0], "power_fraction": [1.0, 1.5]},
{"temperature_degC": [40.0, 60.0], "power_fraction": [1.0, -0.1]},
],
)
def test_rejects_invalid_derating(self, curve):
with pytest.raises(ValidationError):
InverterProfileSettings.model_validate(make_document(power_derating=curve))
@pytest.mark.parametrize("field", ["nominal_power_w", "max_ac_charge_power_w"])
@pytest.mark.parametrize("value", [0.0, -1.0, float("inf")])
def test_rejects_invalid_powers(self, field, value):
with pytest.raises(ValidationError):
InverterProfileSettings.model_validate(make_document(**{field: value}))
def test_compile_rejects_options(self):
with pytest.raises(TypeError):
InverterProfileSettings.model_validate(make_document()).compile_profile(t_step=1.0)
class TestRuntime:
def test_carries_the_model_data(self):
profile = make_profile(ac_charge_limits_total_charge=True)
assert profile.inverter_model == "Test inverter"
assert profile.nominal_power_w == pytest.approx(10000.0)
assert profile.max_ac_charge_power_w == pytest.approx(6000.0)
assert profile.ac_charge_limits_total_charge is True
def test_efficiencies_interpolate_linearly(self):
profile = make_profile()
assert profile.dc_to_ac_efficiency_at(0.3) == pytest.approx(0.935)
assert profile.ac_to_dc_efficiency_at(0.55) == pytest.approx(0.915)
def test_efficiencies_clamp_beyond_the_curve(self):
profile = make_profile()
assert profile.dc_to_ac_efficiency_at(0.0) == pytest.approx(0.90)
assert profile.dc_to_ac_efficiency_at(1.5) == pytest.approx(0.96)
def test_derating(self):
profile = make_profile()
assert profile.power_fraction_at(20.0) == pytest.approx(1.0)
assert profile.power_fraction_at(50.0) == pytest.approx(0.75)
assert profile.power_fraction_at(80.0) == pytest.approx(0.5)
def test_without_derating_full_power(self):
document = make_document()
del document["power_derating"]
profile = InverterProfileSettings.model_validate(document).compile_profile()
assert profile.power_fraction_at(70.0) == pytest.approx(1.0)
def test_is_immutable(self):
profile = make_profile()
with pytest.raises(AttributeError):
profile.nominal_power_w = 1.0 # type: ignore[misc]
class TestPackagedGenericHybrid:
@pytest.fixture
def profile(self, config_eos) -> Iterator[InverterProfile]:
ProfileStore.reset_instance()
yield get_profile_store().get(InverterProfileSettings, "generic-hybrid")
ProfileStore.reset_instance()
def test_is_generic(self, profile):
assert profile.nominal_power_w is None
assert profile.max_ac_charge_power_w is None
def test_full_power_up_to_45_degc(self, profile):
assert profile.power_fraction_at(-30.0) == pytest.approx(1.0)
assert profile.power_fraction_at(45.0) == pytest.approx(1.0)
assert profile.power_fraction_at(60.0) == pytest.approx(0.6)
assert profile.power_fraction_at(65.0) == pytest.approx(0.0)
def test_realistic_efficiencies(self, profile):
for load_fraction in (0.05, 0.1, 0.3, 0.5, 1.0):
assert 0.85 <= profile.dc_to_ac_efficiency_at(load_fraction) < 1.0
assert 0.85 <= profile.ac_to_dc_efficiency_at(load_fraction) < 1.0