mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2025-04-17 07:55:15 +00:00
measurement: - Add new measurement class to hold real world measurements. - Handles load meter readings, grid import and export meter readings. - Aggregates load meter readings aka. measurements to total load. - Can import measurements from files, pandas datetime series, pandas datetime dataframes, simple daetime arrays and programmatically. - Maybe expanded to other measurement values. - Should be used for load prediction adaptions by real world measurements. core/coreabc: - Add mixin class to access measurements core/pydantic: - Add pydantic models for pandas datetime series and dataframes. - Add pydantic models for simple datetime array core/dataabc: - Provide DataImport mixin class for generic import handling. Imports from JSON string and files. Imports from pandas datetime dataframes and simple datetime arrays. Signature of import method changed to allow import datetimes to be given programmatically and by data content. - Use pydantic models for datetime series, dataframes, arrays - Validate generic imports by pydantic models - Provide new attributes min_datetime and max_datetime for DataSequence. - Add parameter dropna to drop NAN/ None values when creating lists, pandas series or numpy array from DataSequence. config/config: - Add common settings for the measurement module. predictions/elecpriceakkudoktor: - Use mean values of last 7 days to fill prediction values not provided by akkudoktor.net (only provides 24 values). prediction/loadabc: - Extend the generic prediction keys by 'load_total_adjusted' for load predictions that adjust the predicted total load by measured load values. prediction/loadakkudoktor: - Extend the Akkudoktor load prediction by load adjustment using measured load values. prediction/load_aggregator: - Module removed. Load aggregation is now handled by the measurement module. prediction/load_corrector: - Module removed. Load correction (aka. adjustment of load prediction by measured load energy) is handled by the LoadAkkudoktor prediction and the generic 'load_mean_adjusted' prediction key. prediction/load_forecast: - Module removed. Functionality now completely handled by the LoadAkkudoktor prediction. utils/cacheutil: - Use pydantic. - Fix potential bug in ttl (time to live) duration handling. utils/datetimeutil: - Added missing handling of pendulum.DateTime and pendulum.Duration instances as input. Handled before as datetime.datetime and datetime.timedelta. utils/visualize: - Move main to generate_example_report() for better testing support. server/server: - Added new configuration option server_fastapi_startup_server_fasthtml to make startup of FastHTML server by FastAPI server conditional. server/fastapi_server: - Add APIs for measurements - Improve APIs to provide or take pandas datetime series and datetime dataframes controlled by pydantic model. - Improve APIs to provide or take simple datetime data arrays controlled by pydantic model. - Move fastAPI server API to v1 for new APIs. - Update pre v1 endpoints to use new prediction and measurement capabilities. - Only start FastHTML server if 'server_fastapi_startup_server_fasthtml' config option is set. tests: - Adapt import tests to changed import method signature - Adapt server test to use the v1 API - Extend the dataabc test to test for array generation from data with several data interval scenarios. - Extend the datetimeutil test to also test for correct handling of to_datetime() providing now(). - Adapt LoadAkkudoktor test for new adjustment calculation. - Adapt visualization test to use example report function instead of visualize.py run as process. - Removed test_load_aggregator. Functionality is now tested in test_measurement. - Added tests for measurement module docs: - Remove sphinxcontrib-openapi as it prevents build of documentation. "site-packages/sphinxcontrib/openapi/openapi31.py", line 305, in _get_type_from_schema for t in schema["anyOf"]: KeyError: 'anyOf'" Signed-off-by: Bobby Noelte <b0661n0e17e@gmail.com>
111 lines
4.1 KiB
Python
111 lines
4.1 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from akkudoktoreos.config.config import get_config
|
|
from akkudoktoreos.core.ems import get_ems
|
|
from akkudoktoreos.prediction.weatherimport import WeatherImport
|
|
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime
|
|
|
|
DIR_TESTDATA = Path(__file__).absolute().parent.joinpath("testdata")
|
|
|
|
FILE_TESTDATA_WEATHERIMPORT_1_JSON = DIR_TESTDATA.joinpath("import_input_1.json")
|
|
|
|
config_eos = get_config()
|
|
ems_eos = get_ems()
|
|
|
|
|
|
@pytest.fixture
|
|
def weather_provider(reset_config, sample_import_1_json):
|
|
"""Fixture to create a WeatherProvider instance."""
|
|
settings = {
|
|
"weather_provider": "WeatherImport",
|
|
"weatherimport_file_path": str(FILE_TESTDATA_WEATHERIMPORT_1_JSON),
|
|
"weatherimport_json": json.dumps(sample_import_1_json),
|
|
}
|
|
config_eos.merge_settings_from_dict(settings)
|
|
provider = WeatherImport()
|
|
assert provider.enabled() == True
|
|
return provider
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_import_1_json():
|
|
"""Fixture that returns sample forecast data report."""
|
|
with open(FILE_TESTDATA_WEATHERIMPORT_1_JSON, "r") as f_res:
|
|
input_data = json.load(f_res)
|
|
return input_data
|
|
|
|
|
|
# ------------------------------------------------
|
|
# General forecast
|
|
# ------------------------------------------------
|
|
|
|
|
|
def test_singleton_instance(weather_provider):
|
|
"""Test that WeatherForecast behaves as a singleton."""
|
|
another_instance = WeatherImport()
|
|
assert weather_provider is another_instance
|
|
|
|
|
|
def test_invalid_provider(weather_provider, monkeypatch):
|
|
"""Test requesting an unsupported weather_provider."""
|
|
settings = {
|
|
"weather_provider": "<invalid>",
|
|
"weatherimport_file_path": str(FILE_TESTDATA_WEATHERIMPORT_1_JSON),
|
|
}
|
|
config_eos.merge_settings_from_dict(settings)
|
|
assert weather_provider.enabled() == False
|
|
|
|
|
|
# ------------------------------------------------
|
|
# Import
|
|
# ------------------------------------------------
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"start_datetime, from_file",
|
|
[
|
|
("2024-11-10 00:00:00", True), # No DST in Germany
|
|
("2024-08-10 00:00:00", True), # DST in Germany
|
|
("2024-03-31 00:00:00", True), # DST change in Germany (23 hours/ day)
|
|
("2024-10-27 00:00:00", True), # DST change in Germany (25 hours/ day)
|
|
("2024-11-10 00:00:00", False), # No DST in Germany
|
|
("2024-08-10 00:00:00", False), # DST in Germany
|
|
("2024-03-31 00:00:00", False), # DST change in Germany (23 hours/ day)
|
|
("2024-10-27 00:00:00", False), # DST change in Germany (25 hours/ day)
|
|
],
|
|
)
|
|
def test_import(weather_provider, sample_import_1_json, start_datetime, from_file):
|
|
"""Test fetching forecast from Import."""
|
|
ems_eos.set_start_datetime(to_datetime(start_datetime, in_timezone="Europe/Berlin"))
|
|
if from_file:
|
|
config_eos.weatherimport_json = None
|
|
assert config_eos.weatherimport_json is None
|
|
else:
|
|
config_eos.weatherimport_file_path = None
|
|
assert config_eos.weatherimport_file_path is None
|
|
weather_provider.clear()
|
|
|
|
# Call the method
|
|
weather_provider.update_data()
|
|
|
|
# Assert: Verify the result is as expected
|
|
assert weather_provider.start_datetime is not None
|
|
assert weather_provider.total_hours is not None
|
|
assert compare_datetimes(weather_provider.start_datetime, ems_eos.start_datetime).equal
|
|
values = sample_import_1_json["weather_temp_air"]
|
|
value_datetime_mapping = weather_provider.import_datetimes(ems_eos.start_datetime, len(values))
|
|
for i, mapping in enumerate(value_datetime_mapping):
|
|
assert i < len(weather_provider.records)
|
|
expected_datetime, expected_value_index = mapping
|
|
expected_value = values[expected_value_index]
|
|
result_datetime = weather_provider.records[i].date_time
|
|
result_value = weather_provider.records[i]["weather_temp_air"]
|
|
|
|
# print(f"{i}: Expected: {expected_datetime}:{expected_value}")
|
|
# print(f"{i}: Result: {result_datetime}:{result_value}")
|
|
assert compare_datetimes(result_datetime, expected_datetime).equal
|
|
assert result_value == expected_value
|