fix: prediction import with pydantic-typed bodies (#1152)
Some checks failed
Bump Version / Bump Version Workflow (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
docker-build / platform-excludes (push) Has been cancelled
docker-build / build (push) Has been cancelled
docker-build / merge (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled
Run Pytest on Pull Request / test (push) Has been cancelled
Close stale pull requests/issues / Find Stale issues and PRs (push) Has been cancelled

Respect pydantic model in serializing json data for import.

Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
This commit is contained in:
Bobby Noelte
2026-07-16 17:15:10 +02:00
committed by GitHub
parent 4848280f9c
commit 0306e7e4ed
9 changed files with 73 additions and 14 deletions

View File

@@ -9,7 +9,7 @@ from akkudoktoreos.prediction.feedintarifffixed import FeedInTariffFixedCommonSe
from akkudoktoreos.prediction.feedintariffimport import FeedInTariffImportCommonSettings
def elecprice_provider_ids() -> list[str]:
def feedintariff_provider_ids() -> list[str]:
"""Valid feedintariff provider ids."""
try:
prediction_eos = get_prediction()
@@ -67,14 +67,14 @@ class FeedInTariffCommonSettings(SettingsBaseModel):
@property
def providers(self) -> list[str]:
"""Available feed in tariff provider ids."""
return elecprice_provider_ids()
return feedintariff_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():
if value is None or value in feedintariff_provider_ids():
return value
raise ValueError(
f"Provider '{value}' is not a valid feed in tariff provider: {elecprice_provider_ids()}."
f"Provider '{value}' is not a valid feed in tariff provider: {feedintariff_provider_ids()}."
)

View File

@@ -40,6 +40,7 @@ from akkudoktoreos.core.ems import ems_manage_energy
from akkudoktoreos.core.emsettings import EnergyManagementMode
from akkudoktoreos.core.logging import logging_track_config, read_file_log
from akkudoktoreos.core.pydantic import (
BaseModel,
PydanticBaseModel,
PydanticDateTimeData,
PydanticDateTimeDataFrame,
@@ -1036,7 +1037,7 @@ async def fastapi_prediction_list_get(
@app.put("/v1/prediction/import/{provider_id}", tags=["prediction"])
def fastapi_prediction_import_provider(
async def fastapi_prediction_import_provider(
provider_id: str = FastapiPath(..., description="Provider ID."),
data: Optional[Union[PydanticDateTimeDataFrame, PydanticDateTimeData, dict]] = None,
force_enable: Optional[bool] = None,
@@ -1056,7 +1057,11 @@ def fastapi_prediction_import_provider(
if not provider.enabled() and not force_enable:
raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not enabled.")
try:
provider.import_from_json(json_str=json.dumps(data))
if isinstance(data, BaseModel):
json_str = data.model_dump_json()
else:
json_str = json.dumps(data)
await provider.import_from_json(json_str=json_str)
provider.update_datetime = to_datetime(in_timezone=get_config().general.timezone)
except Exception as e:
raise HTTPException(