Files
EOS/src/akkudoktoreos/prediction/elecprice.py
Bobby Noelte 3bb0e02aed fix: provider settings in top level field of configuration (#1161)
Configuration for some providers was given in the sub-field `provider_settings`
combining the settings of several providers. Pydantic does not understand
this very well and the configuration became cumbersome, especially in EOSdash.

All provider settings from the `provider-settings` sub-field are now lifted
to the top level field of the configuration.

The changes are automatically migrated in the configuration.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
2026-07-20 14:37:03 +02:00

101 lines
3.4 KiB
Python

from typing import Optional
from pydantic import Field, computed_field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.coreabc import get_prediction
from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider
from akkudoktoreos.prediction.elecpriceenergycharts import (
ElecPriceEnergyChartsCommonSettings,
)
from akkudoktoreos.prediction.elecpricefixed import ElecPriceFixedCommonSettings
from akkudoktoreos.prediction.elecpriceimport import ElecPriceImportCommonSettings
from akkudoktoreos.prediction.elecpricetibber import ElecPriceTibberCommonSettings
def elecprice_provider_ids() -> list[str]:
"""Valid elecprice provider ids."""
try:
prediction_eos = get_prediction()
except Exception:
# Prediction may not be initialized. Return static built-in provider ids.
return [
"ElecPriceAkkudoktor",
"ElecPriceEnergyCharts",
"ElecPriceFixed",
"ElecPriceImport",
"ElecPriceTibber",
]
return [
provider.provider_id()
for provider in prediction_eos.providers
if isinstance(provider, ElecPriceProvider)
]
class ElecPriceCommonSettings(SettingsBaseModel):
"""Electricity Price Prediction Configuration."""
provider: Optional[str] = Field(
default=None,
json_schema_extra={
"description": "Electricity price provider id of provider to be used.",
"examples": ["ElecPriceAkkudoktor"],
},
)
charges_kwh: Optional[float] = Field(
default=None,
ge=0,
json_schema_extra={
"description": "Electricity price charges [amount/kWh]. Will be added to variable market price.",
"examples": [0.21],
},
)
vat_rate: Optional[float] = Field(
default=1.19,
ge=0,
json_schema_extra={
"description": "VAT rate factor applied to electricity price when charges are used.",
"examples": [1.19],
},
)
elecpricefixed: ElecPriceFixedCommonSettings = Field(
default_factory=ElecPriceFixedCommonSettings,
json_schema_extra={"description": "Fixed electricity price provider settings."},
)
elecpriceimport: ElecPriceImportCommonSettings = Field(
default_factory=ElecPriceImportCommonSettings,
json_schema_extra={"description": "Electricity price import provider settings."},
)
energycharts: ElecPriceEnergyChartsCommonSettings = Field(
default_factory=ElecPriceEnergyChartsCommonSettings,
json_schema_extra={"description": "Energy Charts provider settings."},
)
tibber: ElecPriceTibberCommonSettings = Field(
default_factory=ElecPriceTibberCommonSettings,
json_schema_extra={"description": "Tibber electricity price provider settings."},
)
@computed_field # type: ignore[prop-decorator]
@property
def providers(self) -> list[str]:
"""Available electricity price provider ids."""
return elecprice_provider_ids()
# Validators
@field_validator("provider", mode="after")
@classmethod
def validate_provider(cls, value: Optional[str]) -> Optional[str]:
if value is None or value in elecprice_provider_ids():
return value
raise ValueError(
f"Provider '{value}' is not a valid electricity price provider: {elecprice_provider_ids()}."
)