Files
EOS/src/akkudoktoreos/prediction/weather.py
T

52 lines
1.7 KiB
Python
Raw Normal View History

"""Weather forecast module for weather predictions."""
from typing import Optional
from pydantic import Field, computed_field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.prediction.weatherabc import WeatherProvider
2025-01-12 05:19:37 +01:00
from akkudoktoreos.prediction.weatherimport import WeatherImportCommonSettings
def weather_provider_ids() -> list[str]:
"""Valid weather provider ids."""
# From the provider classes: valid also while the configuration is created,
# before the prediction instance exists.
from akkudoktoreos.prediction.prediction import prediction_provider_ids
return prediction_provider_ids(WeatherProvider)
class WeatherCommonSettings(SettingsBaseModel):
2025-01-15 00:54:45 +01:00
"""Weather Forecast Configuration."""
2025-01-18 14:26:34 +01:00
provider: Optional[str] = Field(
2025-01-15 00:54:45 +01:00
default=None,
json_schema_extra={
"description": "Weather provider id of provider to be used.",
"examples": ["WeatherImport"],
},
)
2025-01-12 05:19:37 +01:00
weatherimport: WeatherImportCommonSettings = Field(
default_factory=WeatherImportCommonSettings,
json_schema_extra={"description": "Weather import provider settings"},
2025-01-15 00:54:45 +01:00
)
@computed_field # type: ignore[prop-decorator]
@property
def providers(self) -> list[str]:
"""Available weather provider ids."""
return weather_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 weather_provider_ids():
return value
raise ValueError(
f"Provider '{value}' is not a valid weather provider: {weather_provider_ids()}."
)