2024-12-15 14:40:03 +01:00
|
|
|
"""Retrieves load forecast data from an import file.
|
|
|
|
|
|
|
|
This module provides classes and mappings to manage load data obtained from
|
|
|
|
an import file, including support for various load attributes such as temperature,
|
|
|
|
humidity, cloud cover, and solar irradiance. The data is mapped to the `LoadDataRecord`
|
|
|
|
format, enabling consistent access to forecasted and historical load attributes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Optional, Union
|
|
|
|
|
|
|
|
from pydantic import Field, field_validator
|
|
|
|
|
|
|
|
from akkudoktoreos.config.configabc import SettingsBaseModel
|
2025-01-05 14:41:07 +01:00
|
|
|
from akkudoktoreos.core.logging import get_logger
|
2024-12-15 14:40:03 +01:00
|
|
|
from akkudoktoreos.prediction.loadabc import LoadProvider
|
|
|
|
from akkudoktoreos.prediction.predictionabc import PredictionImportProvider
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class LoadImportCommonSettings(SettingsBaseModel):
|
2024-12-16 20:26:08 +01:00
|
|
|
"""Common settings for load data import from file or JSON string."""
|
2024-12-15 14:40:03 +01:00
|
|
|
|
2025-01-18 14:26:34 +01:00
|
|
|
import_file_path: Optional[Union[str, Path]] = Field(
|
2025-01-15 00:54:45 +01:00
|
|
|
default=None,
|
|
|
|
description="Path to the file to import load data from.",
|
|
|
|
examples=[None, "/path/to/yearly_load.json"],
|
2024-12-15 14:40:03 +01:00
|
|
|
)
|
2025-01-18 14:26:34 +01:00
|
|
|
import_json: Optional[str] = Field(
|
2025-01-15 00:54:45 +01:00
|
|
|
default=None,
|
|
|
|
description="JSON string, dictionary of load forecast value lists.",
|
|
|
|
examples=['{"load0_mean": [676.71, 876.19, 527.13]}'],
|
2024-12-15 14:40:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Validators
|
2025-01-18 14:26:34 +01:00
|
|
|
@field_validator("import_file_path", mode="after")
|
2024-12-15 14:40:03 +01:00
|
|
|
@classmethod
|
|
|
|
def validate_loadimport_file_path(cls, value: Optional[Union[str, Path]]) -> Optional[Path]:
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
if isinstance(value, str):
|
|
|
|
value = Path(value)
|
|
|
|
"""Ensure file is available."""
|
|
|
|
value.resolve()
|
|
|
|
if not value.is_file():
|
|
|
|
raise ValueError(f"Import file path '{value}' is not a file.")
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
class LoadImport(LoadProvider, PredictionImportProvider):
|
|
|
|
"""Fetch Load data from import file or JSON string.
|
|
|
|
|
|
|
|
LoadImport is a singleton-based class that retrieves load forecast data
|
|
|
|
from a file or JSON string and maps it to `LoadDataRecord` fields. It manages the forecast
|
|
|
|
over a range of hours into the future and retains historical data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def provider_id(cls) -> str:
|
|
|
|
"""Return the unique identifier for the LoadImport provider."""
|
|
|
|
return "LoadImport"
|
|
|
|
|
|
|
|
def _update_data(self, force_update: Optional[bool] = False) -> None:
|
2025-01-22 23:47:28 +01:00
|
|
|
if self.config.load.provider_settings is None:
|
|
|
|
logger.debug(f"{self.provider_id()} data update without provider settings.")
|
|
|
|
return
|
2025-01-19 14:54:15 +01:00
|
|
|
if self.config.load.provider_settings.import_file_path:
|
2025-01-18 14:26:34 +01:00
|
|
|
self.import_from_file(self.config.provider_settings.import_file_path, key_prefix="load")
|
2025-01-19 14:54:15 +01:00
|
|
|
if self.config.load.provider_settings.import_json:
|
2025-01-18 14:26:34 +01:00
|
|
|
self.import_from_json(self.config.load.provider_settings.import_json, key_prefix="load")
|