feat: integrate device and runtime configuration foundation on main (#1328)

* feat: adapt configuration for multi optimization algorithms

Decouple configuration from optimization algorithm parameters. Add to_[algorithm]_param() methods
to the configuration that derive optimization algorithm specific parameters from the configuration.
Add x-scope tags to the configuration options that describe for which specific algorithms the
configuration option is for.

The whole device settings are restructured. There are now general settings for the device classes
with the afore mentioned to_[algorithm]_param() methods. The general device settings got their own
directory `devices/settings`. By this the parameter class also does not have to be a pydantic model
which can be used for future optimization/ simulations speed up.

Also the parameter class for a device is now part of the device module. This better decouples and
also is the natural place for parameters of a device.

Besides this feature there are also fixes and improvements:

* feat: extend home appliance time window settings and simulation

  Home appliance can now be configured for multiple runs with per-cycle allowed time windows. The
  number of remaining cycles to plan is determined at runtime by reading the
  ``cycles_completed_measurement_key`` from the measurement store.

* feat: specialiced CycleTimeWindowSequence for time window sequences

  Sequence of time windows associated to cycles.

  This model specializes ``ValueTimeWindowSequence`` so that the ``value``
  field of each ``ValueTimeWindow`` encodes the **cycle index** (0-based
  integer) the window belongs to.

  Typical use: an appliance that must run ``n`` times per day, each run
  constrained to a distinct time window.  Assign ``value=0`` to windows
  for the first cycle, ``value=1`` for the second, and so on.  Multiple
  windows may share the same cycle index (their allowed regions are unioned).
  Windows with ``value=None`` are silently ignored by all cycle-aware methods.

* fix: Make test_configmigrate also regard the _ANY_SENTENIEL in key values

* chore: Make devices configurations a map instead of a list

  This makes config paths stable regardless of declaration order and lets each device settings
  class build its own config path from ``self.device_id`` without needing an external index.
  Tests are adapted likewise.

  Devices configurations are automatically migrated from lists to maps.

* chore: rename levelized_cost_of_storage_kwh to levelized_cost_of_storage_amt kwh

  This better fits in the naming scheme and also makes clear the costs are money.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>

* fix: runtime config update ignored by config file

Runtime settings were handed back to pydantic-settings as init settings,
which rank below the config file and the environment. Any key already
present in EOS.config.json or in the environment silently discarded the
update, so a bulk PUT /v1/config returned 200 without applying anything,
while the granular PUT /v1/config/{path} endpoint kept working.

Add a dedicated runtime settings source ranked directly below the command
line arguments and record granular updates there as well, so both
endpoints share one store that survives re-evaluation of the settings
sources. Environment variables keep precedence over the config file for
all keys that were not set at runtime.

Also repairs revert_settings() and update(), which passed their data
through the same init settings.

Closes #1303

* fix: env vars ignored on first config build

ConfigEOS.__init__ passed self as first positional argument to _setup,
which forwards it to pydantic_settings.BaseSettings.__init__. Its first
positional parameter is _case_sensitive, so the environment source
matched the upper case variable names against the lower case field names
and returned nothing. Environment settings only took effect after the
next configuration setup.

* docs: changelog for config priority fixes

* fix(config): preserve device identities and storage costs during migration

* fix(devices): preserve charge-rate typing and public import compatibility

* ruff format fix

* fix(config): satisfy typed device conversion and migration contracts

* docs(config): refresh validated configuration prerequisite schemas

---------

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
Co-authored-by: Bobby Noelte <b0661n0e17e@gmail.com>
Co-authored-by: r0b2g1t <r0b2g1t@users.noreply.github.com>
This commit is contained in:
Andreas
2026-09-17 17:51:40 +02:00
committed by GitHub
co-authored by Bobby Noelte r0b2g1t
parent 431d7d57e5
commit 8224c64654
55 changed files with 5214 additions and 2170 deletions
@@ -0,0 +1,258 @@
"""Controllable home appliance device settings.
Note: Used for the GENETIC and GENETIC0 algorithm.
"""
from typing import TYPE_CHECKING, Optional
from pydantic import Field, computed_field, model_validator
from akkudoktoreos.config.configabc import ConfigScope, CycleTimeWindowSequence
from akkudoktoreos.devices.settings.devicebasesettings import DevicesBaseSettings
if TYPE_CHECKING:
from akkudoktoreos.devices.genetic0.genetic0homeappliance import (
Genetic0HomeApplianceParameters,
)
from akkudoktoreos.devices.genetic.homeappliance import HomeApplianceParameters
class HomeApplianceCommonSettings(DevicesBaseSettings):
"""Controllable home appliance device settings.
Represents a shiftable load whose start time — and optionally the
start times for multiple sequential runs — can be deferred by the
optimiser within per-cycle allowed time windows.
The number of remaining cycles to plan is determined at runtime by
reading ``cycles_completed_measurement_key`` from the measurement
store inside ``HomeApplianceDevice.setup_run``.
``num_cycles`` is required when ``cycle_time_windows`` is ``None``
(unconstrained); when windows are provided it is derived from
``cycle_time_windows.num_cycles()``.
Single-cycle, unconstrained (start any time)
---------------------------------------------
::
device_id: dishwasher
consumption_wh: 1500
duration_h: 2
num_cycles: 1
ports:
- port_id: p_ac
bus_id: bus_ac
direction: sink
Single-cycle, constrained to one window
----------------------------------------
Each window's ``value`` field carries the **cycle index** (0-based).
Windows without a ``value`` are ignored by the optimizer::
device_id: dishwasher
consumption_wh: 1500
duration_h: 2
ports:
- port_id: p_ac
bus_id: bus_ac
direction: sink
cycle_time_windows:
windows:
- start_time: "10:00"
duration: "12 hours"
value: 0
Multi-cycle, per-cycle windows
--------------------------------
Two cycles, each with its own window. Cycle 0 runs in the morning,
cycle 1 in the evening::
device_id: washing_machine
consumption_wh: 2000
duration_h: 2
min_cycle_gap_h: 1
ports:
- port_id: p_ac
bus_id: bus_ac
direction: sink
cycle_time_windows:
windows:
- start_time: "07:00"
duration: "5 hours"
value: 0
- start_time: "17:00"
duration: "5 hours"
value: 1
Multi-cycle, shared window (both cycles may run any time 10:00-20:00)
-----------------------------------------------------------------------
Assign the same-shaped windows to distinct cycle indices so the
optimizer can place them independently::
cycle_time_windows:
windows:
- start_time: "10:00"
duration: "10 hours"
value: 0
- start_time: "10:00"
duration: "10 hours"
value: 1
"""
consumption_wh: int = Field(
default=3000,
gt=0,
json_schema_extra={
"description": "Energy consumption per run cycle [Wh].",
"examples": [2000],
"x-scope": [str(ConfigScope.GENETIC), str(ConfigScope.GENETIC0)],
},
)
duration_h: int = Field(
default=3,
gt=0,
le=24,
json_schema_extra={
"description": "Run duration per cycle [h] (1-24).",
"examples": [2],
"x-scope": [str(ConfigScope.GENETIC), str(ConfigScope.GENETIC0)],
},
)
num_cycles: int = Field(
default=1,
ge=1,
json_schema_extra={
"description": (
"Number of times the appliance must run within the horizon. "
"Required when cycle_time_windows is null (unconstrained). "
"Ignored when cycle_time_windows is provided -- the number "
"of distinct cycle indices in the windows defines num_cycles. "
"Defaults to 1."
),
"examples": [1, 2],
"x-scope": [str(ConfigScope.GENETIC), str(ConfigScope.GENETIC0)],
},
)
cycle_time_windows: Optional[CycleTimeWindowSequence] = Field(
default=None,
json_schema_extra={
"description": (
"Per-cycle allowed scheduling time windows. "
"Each window's value field specifies the cycle index (0-based). "
"When null, the appliance may start at any step and num_cycles "
"must be set explicitly."
),
"examples": [
None,
{
"windows": [
{"start_time": "07:00", "duration": "5 hours", "value": 0},
{"start_time": "17:00", "duration": "5 hours", "value": 1},
]
},
],
"x-scope": [
str(ConfigScope.GENETIC),
],
},
)
min_cycle_gap_h: int = Field(
default=0,
ge=0,
json_schema_extra={
"description": (
"Minimum idle time between the end of one cycle and the start "
"of the next [h]. Applies uniformly between all consecutive cycles. "
"0 means back-to-back runs are permitted."
),
"examples": [0, 1, 4],
"x-scope": [
str(ConfigScope.GENETIC),
],
},
)
cycles_completed_measurement_key: Optional[str] = Field(
default=None,
json_schema_extra={
"description": (
"Measurement store key holding the number of cycles already "
"completed in the current planning day. Read by "
"HomeApplianceDevice.setup_run via context.resolve_measurement. "
"Defaults to '{device_id}.cycles_completed' when null."
),
"examples": ["dishwasher.cycles_completed", None],
"x-scope": [
str(ConfigScope.GENETIC),
],
},
)
@model_validator(mode="after")
def _validate_num_cycles_specified(self) -> "HomeApplianceCommonSettings":
"""Require num_cycles when windows are not provided."""
if self.cycle_time_windows is None and self.num_cycles is None:
raise ValueError(
"num_cycles must be set when cycle_time_windows is null. "
"Provide either cycle_time_windows (windows define cycle count) "
"or set num_cycles explicitly."
)
return self
@computed_field # type: ignore[prop-decorator]
@property
def effective_num_cycles(self) -> int:
"""Number of cycles as seen by the optimizer.
Derived from cycle_time_windows.num_cycles() when windows are
provided; falls back to the explicit num_cycles field otherwise.
"""
if self.cycle_time_windows is not None:
return self.cycle_time_windows.num_cycles()
return self.num_cycles
# ------------------------------------------------------------------
# GENETIC domain conversion
# ------------------------------------------------------------------
def to_genetic_param(self) -> "HomeApplianceParameters":
"""Return HomeApplianceParameters for the GENETIC optimizer."""
from akkudoktoreos.devices.genetic.homeappliance import HomeApplianceParameters
return HomeApplianceParameters(
device_id=self.device_id,
consumption_wh=self.consumption_wh,
duration_h=self.duration_h,
num_cycles=self.effective_num_cycles,
min_cycle_gap_h=self.min_cycle_gap_h,
time_windows=self.cycle_time_windows,
)
# ------------------------------------------------------------------
# GENETIC0 domain conversion
# ------------------------------------------------------------------
def to_genetic0_param(self) -> "Genetic0HomeApplianceParameters":
"""Return Genetic0HomeApplianceParameters for the GENETIC0 optimizer."""
from akkudoktoreos.devices.genetic0.genetic0homeappliance import (
Genetic0HomeApplianceParameters,
)
return Genetic0HomeApplianceParameters(
device_id=self.device_id,
consumption_wh=self.consumption_wh,
duration_h=self.duration_h,
time_windows=self.cycle_time_windows,
)
@computed_field # type: ignore[prop-decorator]
@property
def measurement_keys(self) -> Optional[list[str]]:
"""Measurement keys for the home appliance stati that are measurements."""
keys: list[str] = []
return keys