6 Commits

Author SHA1 Message Date
Normann
ecaf1366ba fix 2025-02-16 23:01:39 +01:00
Normann
027ba7c07f [attr-defined,unused-ignore] usage 2025-02-16 22:58:00 +01:00
NormannK
f1d8728da4 . 2025-02-16 16:22:37 +01:00
NormannK
12df87d392 type: ignore changes 2025-02-16 16:17:44 +01:00
NormannK
1d6b31ceb4 pre-commit run changes 2025-02-16 15:35:17 +01:00
NormannK
e7143f6540 pre-commit autoupdate 2025-02-16 15:21:41 +01:00
125 changed files with 15175 additions and 14490 deletions

View File

@@ -195,7 +195,6 @@ jobs:
type=ref,event=pr type=ref,event=pr
type=semver,pattern={{version}} type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
labels: | labels: |
org.opencontainers.image.licenses=${{ env.EOS_LICENSE }} org.opencontainers.image.licenses=${{ env.EOS_LICENSE }}
annotations: | annotations: |

View File

@@ -1,35 +0,0 @@
name: "Close stale pull requests/issues"
on:
schedule:
- cron: "16 00 * * *"
permissions:
contents: read
jobs:
stale:
name: Find Stale issues and PRs
runs-on: ubuntu-22.04
if: github.repository == 'Akkudoktor-EOS/EOS'
permissions:
pull-requests: write # to comment on stale pull requests
issues: write # to comment on stale issues
steps:
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
with:
stale-pr-message: 'This pull request has been marked as stale because it has been open (more
than) 90 days with no activity. Remove the stale label or add a comment saying that you
would like to have the label removed otherwise this pull request will automatically be
closed in 30 days. Note, that you can always re-open a closed pull request at any time.'
stale-issue-message: 'This issue has been marked as stale because it has been open (more
than) 90 days with no activity. Remove the stale label or add a comment saying that you
would like to have the label removed otherwise this issue will automatically be closed in
30 days. Note, that you can always re-open a closed issue at any time.'
days-before-stale: 90
days-before-close: 30
stale-issue-label: 'stale'
stale-pr-label: 'stale'
exempt-pr-labels: 'in progress'
exempt-issue-labels: 'feature request, enhancement'
operations-per-run: 400

2
.gitignore vendored
View File

@@ -179,7 +179,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/ #.idea/
# General # General
.DS_Store .DS_Store

View File

@@ -1,35 +0,0 @@
[general]
# verbosity should be a value between 1 and 3, the commandline -v flags take precedence over this
verbosity = 3
regex-style-search=true
# Ignore rules, reference them by id or name (comma-separated)
ignore=title-trailing-punctuation, T3
# Enable specific community contributed rules
contrib=contrib-title-conventional-commits,CC1
# Set the extra-path where gitlint will search for user defined rules
extra-path=scripts/gitlint
[title-max-length]
line-length=80
[title-min-length]
min-length=5
[ignore-by-title]
# Match commit titles starting with "Release"
regex=^Release(.*)
ignore=title-max-length,body-min-length
[ignore-by-body]
# Match commits message bodies that have a line that contains 'release'
regex=(.*)release(.*)
ignore=all
[ignore-by-author-name]
# Match commits by author name (e.g. ignore dependabot commits)
regex=dependabot
ignore=all

View File

@@ -33,16 +33,3 @@ repos:
- "pandas-stubs==2.2.3.241009" - "pandas-stubs==2.2.3.241009"
- "numpy==2.1.3" - "numpy==2.1.3"
pass_filenames: false pass_filenames: false
- repo: https://github.com/jackdewinter/pymarkdown
rev: v0.9.29
hooks:
- id: pymarkdown
files: ^docs/
exclude: ^docs/_generated
args:
- --config=docs/pymarkdown.json
- scan
- repo: https://github.com/jorisroovers/gitlint
rev: v0.19.1
hooks:
- id: gitlint

View File

@@ -6,7 +6,7 @@ The `EOS` project is in early development, therefore we encourage contribution i
## Documentation ## Documentation
Latest development documentation can be found at [Akkudoktor-EOS](https://akkudoktor-eos.readthedocs.io/en/latest/). Latest development documentation can be found at [Akkudoktor-EOS](https://akkudoktor-eos.readthedocs.io/en/main/).
## Bug Reports ## Bug Reports
@@ -21,8 +21,8 @@ There are just too many possibilities and the project would drown in tickets oth
## Code Contributions ## Code Contributions
We welcome code contributions and bug fixes via [Pull Requests](https://github.com/Akkudoktor-EOS/EOS/pulls). We welcome code contributions and bug fixes via [Pull Requests](https://github.com/Akkudoktor-EOS/EOS/pulls).
To make collaboration easier, we require pull requests to pass code style, unit tests, and commit To make collaboration easier, we require pull requests to pass code style and unit tests.
message style checks.
### Setup development environment ### Setup development environment
@@ -60,7 +60,6 @@ To run formatting automatically before every commit:
```bash ```bash
pre-commit install pre-commit install
pre-commit install --hook-type commit-msg
``` ```
Or run them manually: Or run them manually:
@@ -76,8 +75,3 @@ Use `pytest` to run tests locally:
```bash ```bash
python -m pytest -vs --cov src --cov-report term-missing tests/ python -m pytest -vs --cov src --cov-report term-missing tests/
``` ```
### Commit message style
Our commit message checks use [`gitlint`](https://github.com/jorisroovers/gitlint). The checks
enforce the [`Conventional Commits`](https://www.conventionalcommits.org) commit message style.

View File

@@ -1,4 +1,3 @@
# syntax=docker/dockerfile:1.7
ARG PYTHON_VERSION=3.12.7 ARG PYTHON_VERSION=3.12.7
FROM python:${PYTHON_VERSION}-slim FROM python:${PYTHON_VERSION}-slim
@@ -10,9 +9,6 @@ ENV EOS_CACHE_DIR="${EOS_DIR}/cache"
ENV EOS_OUTPUT_DIR="${EOS_DIR}/output" ENV EOS_OUTPUT_DIR="${EOS_DIR}/output"
ENV EOS_CONFIG_DIR="${EOS_DIR}/config" ENV EOS_CONFIG_DIR="${EOS_DIR}/config"
# Overwrite when starting the container in a production environment
ENV EOS_SERVER__EOSDASH_SESSKEY=s3cr3t
WORKDIR ${EOS_DIR} WORKDIR ${EOS_DIR}
RUN adduser --system --group --no-create-home eos \ RUN adduser --system --group --no-create-home eos \
@@ -35,19 +31,6 @@ RUN mkdir -p src && pip install -e .
COPY src src COPY src src
# Create minimal default configuration for Docker to fix EOSDash accessibility (#629)
# This ensures EOSDash binds to 0.0.0.0 instead of 127.0.0.1 in containers
RUN echo '{\n\
"server": {\n\
"host": "0.0.0.0",\n\
"port": 8503,\n\
"startup_eosdash": true,\n\
"eosdash_host": "0.0.0.0",\n\
"eosdash_port": 8504\n\
}\n\
}' > "${EOS_CONFIG_DIR}/EOS.config.json" \
&& chown eos:eos "${EOS_CONFIG_DIR}/EOS.config.json"
USER eos USER eos
ENTRYPOINT [] ENTRYPOINT []

View File

@@ -1,5 +1,5 @@
# Define the targets # Define the targets
.PHONY: help venv pip install dist test test-full docker-run docker-build docs read-docs clean format gitlint mypy run run-dev .PHONY: help venv pip install dist test test-full docker-run docker-build docs read-docs clean format mypy run run-dev
# Default target # Default target
all: help all: help
@@ -11,22 +11,18 @@ help:
@echo " pip - Install dependencies from requirements.txt." @echo " pip - Install dependencies from requirements.txt."
@echo " pip-dev - Install dependencies from requirements-dev.txt." @echo " pip-dev - Install dependencies from requirements-dev.txt."
@echo " format - Format source code." @echo " format - Format source code."
@echo " gitlint - Lint last commit message."
@echo " mypy - Run mypy." @echo " mypy - Run mypy."
@echo " install - Install EOS in editable form (development mode) into virtual environment." @echo " install - Install EOS in editable form (development mode) into virtual environment."
@echo " docker-run - Run entire setup on docker" @echo " docker-run - Run entire setup on docker"
@echo " docker-build - Rebuild docker image" @echo " docker-build - Rebuild docker image"
@echo " docs - Generate HTML documentation (in build/docs/html/)." @echo " docs - Generate HTML documentation (in build/docs/html/)."
@echo " read-docs - Read HTML documentation in your browser." @echo " read-docs - Read HTML documentation in your browser."
@echo " gen-docs - Generate openapi.json and docs/_generated/*." @echo " gen-docs - Generate openapi.json and docs/_generated/*.""
@echo " clean-docs - Remove generated documentation." @echo " clean-docs - Remove generated documentation.""
@echo " run - Run EOS production server in virtual environment." @echo " run - Run EOS production server in virtual environment."
@echo " run-dev - Run EOS development server in virtual environment (automatically reloads)." @echo " run-dev - Run EOS development server in virtual environment (automatically reloads)."
@echo " run-dash - Run EOSdash production server in virtual environment." @echo " run-dash - Run EOSdash production server in virtual environment."
@echo " run-dash-dev - Run EOSdash development server in virtual environment (automatically reloads)." @echo " run-dash-dev - Run EOSdash development server in virtual environment (automatically reloads)."
@echo " test - Run tests."
@echo " test-full - Run tests with full optimization."
@echo " test-ci - Run tests as CI does. No user config file allowed."
@echo " dist - Create distribution (in dist/)." @echo " dist - Create distribution (in dist/)."
@echo " clean - Remove generated documentation, distribution and virtual environment." @echo " clean - Remove generated documentation, distribution and virtual environment."
@@ -76,11 +72,6 @@ read-docs: docs
@echo "Read the documentation in your browser" @echo "Read the documentation in your browser"
.venv/bin/python -m webbrowser build/docs/html/index.html .venv/bin/python -m webbrowser build/docs/html/index.html
# Clean Python bytecode
clean-bytecode:
find . -type d -name "__pycache__" -exec rm -r {} +
find . -type f -name "*.pyc" -delete
# Clean target to remove generated documentation and documentation artefacts # Clean target to remove generated documentation and documentation artefacts
clean-docs: clean-docs:
@echo "Searching and deleting all '_autosum' directories in docs..." @echo "Searching and deleting all '_autosum' directories in docs..."
@@ -119,11 +110,6 @@ test:
@echo "Running tests..." @echo "Running tests..."
.venv/bin/pytest -vs --cov src --cov-report term-missing .venv/bin/pytest -vs --cov src --cov-report term-missing
# Target to run tests as done by CI on Github.
test-ci:
@echo "Running tests as CI..."
.venv/bin/pytest --full-run --check-config-side-effect -vs --cov src --cov-report term-missing
# Target to run all tests. # Target to run all tests.
test-full: test-full:
@echo "Running all tests..." @echo "Running all tests..."
@@ -133,10 +119,6 @@ test-full:
format: format:
.venv/bin/pre-commit run --all-files .venv/bin/pre-commit run --all-files
# Target to trigger gitlint using pre-commit for the last commit message
gitlint:
.venv/bin/pre-commit run gitlint --hook-stage commit-msg --commit-msg-filename .git/COMMIT_EDITMSG
# Target to format code. # Target to format code.
mypy: mypy:
.venv/bin/mypy .venv/bin/mypy

View File

@@ -8,19 +8,9 @@ Documentation can be found at [Akkudoktor-EOS](https://akkudoktor-eos.readthedoc
See [CONTRIBUTING.md](CONTRIBUTING.md). See [CONTRIBUTING.md](CONTRIBUTING.md).
## System requirements
- Python >= 3.11, < 3.13
- Architecture: amd64, aarch64 (armv8)
- OS: Linux, Windows, macOS
Note: For Python 3.13 some dependencies (e.g. [Pendulum](https://github.com/python-pendulum/Pendulum)) are not yet available on https://pypi.org and have to be manually compiled (a recent [Rust](https://www.rust-lang.org/tools/install) installation is required).
Other architectures (e.g. armv6, armv7) are unsupported for now, because a multitude of dependencies are not available on https://piwheels.org and have to be built manually (a recent Rust installation and [GCC](https://gcc.gnu.org/) are required, Python 3.11 is recommended).
## Installation ## Installation
The project requires Python 3.11 or newer. Docker images (amd64/aarch64) can be found at [akkudoktor/eos](https://hub.docker.com/r/akkudoktor/eos). The project requires Python 3.10 or newer. Official docker images can be found at [akkudoktor/eos](https://hub.docker.com/r/akkudoktor/eos).
Following sections describe how to locally start the EOS server on `http://localhost:8503`. Following sections describe how to locally start the EOS server on `http://localhost:8503`.
@@ -40,12 +30,11 @@ Windows:
```cmd ```cmd
python -m venv .venv python -m venv .venv
.venv\Scripts\Activate
.venv\Scripts\pip install -r requirements.txt .venv\Scripts\pip install -r requirements.txt
.venv\Scripts\pip install -e . .venv\Scripts\pip install -e .
``` ```
Finally, start the EOS server to access it at `http://localhost:8503` (API docs at `http://localhost:8503/docs`): Finally, start the EOS server:
Linux: Linux:
@@ -61,8 +50,6 @@ Windows:
### Docker ### Docker
Start EOS with following command to access it at `http://localhost:8503` (API docs at `http://localhost:8503/docs`):
```bash ```bash
docker compose up docker compose up
``` ```

View File

@@ -21,25 +21,5 @@ services:
- EOS_ELECPRICE__PROVIDER=ElecPriceAkkudoktor - EOS_ELECPRICE__PROVIDER=ElecPriceAkkudoktor
- EOS_ELECPRICE__CHARGES_KWH=0.21 - EOS_ELECPRICE__CHARGES_KWH=0.21
ports: ports:
# Configure what ports to expose on host - "${EOS_SERVER__PORT}:${EOS_SERVER__PORT}"
- "${EOS_SERVER__PORT}:8503" - "${EOS_SERVER__EOSDASH_PORT}:${EOS_SERVER__EOSDASH_PORT}"
- "${EOS_SERVER__EOSDASH_PORT}:8504"
# Volume mount configuration (optional)
# IMPORTANT: When mounting local directories, the default config won't be available.
# You must create an EOS.config.json file in your local config directory with:
# {
# "server": {
# "host": "0.0.0.0", # Required for Docker container accessibility
# "port": 8503,
# "startup_eosdash": true,
# "eosdash_host": "0.0.0.0", # Required for Docker container accessibility
# "eosdash_port": 8504
# }
# }
#
# Example volume mounts (uncomment to use):
# volumes:
# - ./config:/opt/eos/config # Mount local config directory
# - ./cache:/opt/eos/cache # Mount local cache directory
# - ./output:/opt/eos/output # Mount local output directory

View File

@@ -15,6 +15,10 @@ Properties:
timezone (Optional[str]): Computed time zone string based on the specified latitude timezone (Optional[str]): Computed time zone string based on the specified latitude
and longitude. and longitude.
Validators:
validate_latitude (float): Ensures `latitude` is within the range -90 to 90.
validate_longitude (float): Ensures `longitude` is within the range -180 to 180.
:::{table} general :::{table} general
:widths: 10 20 10 5 5 30 :widths: 10 20 10 5 5 30
:align: left :align: left
@@ -123,10 +127,8 @@ Properties:
| Name | Environment Variable | Type | Read-Only | Default | Description | | Name | Environment Variable | Type | Read-Only | Default | Description |
| ---- | -------------------- | ---- | --------- | ------- | ----------- | | ---- | -------------------- | ---- | --------- | ------- | ----------- |
| level | `EOS_LOGGING__LEVEL` | `Optional[str]` | `rw` | `None` | This is deprecated. Use console_level and file_level instead. | | level | `EOS_LOGGING__LEVEL` | `Optional[str]` | `rw` | `None` | EOS default logging level. |
| console_level | `EOS_LOGGING__CONSOLE_LEVEL` | `Optional[str]` | `rw` | `None` | Logging level when logging to console. | | root_level | | `str` | `ro` | `N/A` | Root logger logging level. |
| file_level | `EOS_LOGGING__FILE_LEVEL` | `Optional[str]` | `rw` | `None` | Logging level when logging to file. |
| file_path | | `Optional[pathlib.Path]` | `ro` | `N/A` | Computed log file path based on data output path. |
::: :::
### Example Input ### Example Input
@@ -136,9 +138,7 @@ Properties:
{ {
"logging": { "logging": {
"level": null, "level": "INFO"
"console_level": "TRACE",
"file_level": "TRACE"
} }
} }
``` ```
@@ -150,10 +150,8 @@ Properties:
{ {
"logging": { "logging": {
"level": null, "level": "INFO",
"console_level": "TRACE", "root_level": "INFO"
"file_level": "TRACE",
"file_path": "/home/user/.local/share/net.akkudoktoreos.net/output/eos.log"
} }
} }
``` ```
@@ -424,7 +422,6 @@ Validators:
| ---- | -------------------- | ---- | --------- | ------- | ----------- | | ---- | -------------------- | ---- | --------- | ------- | ----------- |
| provider | `EOS_ELECPRICE__PROVIDER` | `Optional[str]` | `rw` | `None` | Electricity price provider id of provider to be used. | | provider | `EOS_ELECPRICE__PROVIDER` | `Optional[str]` | `rw` | `None` | Electricity price provider id of provider to be used. |
| charges_kwh | `EOS_ELECPRICE__CHARGES_KWH` | `Optional[float]` | `rw` | `None` | Electricity price charges (€/kWh). | | charges_kwh | `EOS_ELECPRICE__CHARGES_KWH` | `Optional[float]` | `rw` | `None` | Electricity price charges (€/kWh). |
| vat_rate | `EOS_ELECPRICE__VAT_RATE` | `Optional[float]` | `rw` | `1.19` | VAT rate factor applied to electricity price when charges are used. |
| provider_settings | `EOS_ELECPRICE__PROVIDER_SETTINGS` | `Optional[akkudoktoreos.prediction.elecpriceimport.ElecPriceImportCommonSettings]` | `rw` | `None` | Provider settings | | provider_settings | `EOS_ELECPRICE__PROVIDER_SETTINGS` | `Optional[akkudoktoreos.prediction.elecpriceimport.ElecPriceImportCommonSettings]` | `rw` | `None` | Provider settings |
::: :::
@@ -437,7 +434,6 @@ Validators:
"elecprice": { "elecprice": {
"provider": "ElecPriceAkkudoktor", "provider": "ElecPriceAkkudoktor",
"charges_kwh": 0.21, "charges_kwh": 0.21,
"vat_rate": 1.19,
"provider_settings": null "provider_settings": null
} }
} }
@@ -479,7 +475,7 @@ Validators:
| Name | Environment Variable | Type | Read-Only | Default | Description | | Name | Environment Variable | Type | Read-Only | Default | Description |
| ---- | -------------------- | ---- | --------- | ------- | ----------- | | ---- | -------------------- | ---- | --------- | ------- | ----------- |
| provider | `EOS_LOAD__PROVIDER` | `Optional[str]` | `rw` | `None` | Load provider id of provider to be used. | | provider | `EOS_LOAD__PROVIDER` | `Optional[str]` | `rw` | `None` | Load provider id of provider to be used. |
| provider_settings | `EOS_LOAD__PROVIDER_SETTINGS` | `Union[akkudoktoreos.prediction.loadakkudoktor.LoadAkkudoktorCommonSettings, akkudoktoreos.prediction.loadvrm.LoadVrmCommonSettings, akkudoktoreos.prediction.loadimport.LoadImportCommonSettings, NoneType]` | `rw` | `None` | Provider settings | | provider_settings | `EOS_LOAD__PROVIDER_SETTINGS` | `Union[akkudoktoreos.prediction.loadakkudoktor.LoadAkkudoktorCommonSettings, akkudoktoreos.prediction.loadimport.LoadImportCommonSettings, NoneType]` | `rw` | `None` | Provider settings |
::: :::
### Example Input/Output ### Example Input/Output
@@ -522,33 +518,6 @@ Validators:
} }
``` ```
### Common settings for VRM API
:::{table} load::provider_settings
:widths: 10 10 5 5 30
:align: left
| Name | Type | Read-Only | Default | Description |
| ---- | ---- | --------- | ------- | ----------- |
| load_vrm_token | `str` | `rw` | `your-token` | Token for Connecting VRM API |
| load_vrm_idsite | `int` | `rw` | `12345` | VRM-Installation-ID |
:::
#### Example Input/Output
```{eval-rst}
.. code-block:: json
{
"load": {
"provider_settings": {
"load_vrm_token": "your-token",
"load_vrm_idsite": 12345
}
}
}
```
### Common settings for load data import from file ### Common settings for load data import from file
:::{table} load::provider_settings :::{table} load::provider_settings
@@ -583,9 +552,8 @@ Validators:
| Name | Environment Variable | Type | Read-Only | Default | Description | | Name | Environment Variable | Type | Read-Only | Default | Description |
| ---- | -------------------- | ---- | --------- | ------- | ----------- | | ---- | -------------------- | ---- | --------- | ------- | ----------- |
| provider | `EOS_PVFORECAST__PROVIDER` | `Optional[str]` | `rw` | `None` | PVForecast provider id of provider to be used. | | provider | `EOS_PVFORECAST__PROVIDER` | `Optional[str]` | `rw` | `None` | PVForecast provider id of provider to be used. |
| provider_settings | `EOS_PVFORECAST__PROVIDER_SETTINGS` | `Union[akkudoktoreos.prediction.pvforecastimport.PVForecastImportCommonSettings, akkudoktoreos.prediction.pvforecastvrm.PVforecastVrmCommonSettings, NoneType]` | `rw` | `None` | Provider settings |
| planes | `EOS_PVFORECAST__PLANES` | `Optional[list[akkudoktoreos.prediction.pvforecast.PVForecastPlaneSetting]]` | `rw` | `None` | Plane configuration. | | planes | `EOS_PVFORECAST__PLANES` | `Optional[list[akkudoktoreos.prediction.pvforecast.PVForecastPlaneSetting]]` | `rw` | `None` | Plane configuration. |
| max_planes | `EOS_PVFORECAST__MAX_PLANES` | `Optional[int]` | `rw` | `0` | Maximum number of planes that can be set | | provider_settings | `EOS_PVFORECAST__PROVIDER_SETTINGS` | `Optional[akkudoktoreos.prediction.pvforecastimport.PVForecastImportCommonSettings]` | `rw` | `None` | Provider settings |
| planes_peakpower | | `List[float]` | `ro` | `N/A` | Compute a list of the peak power per active planes. | | planes_peakpower | | `List[float]` | `ro` | `N/A` | Compute a list of the peak power per active planes. |
| planes_azimuth | | `List[float]` | `ro` | `N/A` | Compute a list of the azimuths per active planes. | | planes_azimuth | | `List[float]` | `ro` | `N/A` | Compute a list of the azimuths per active planes. |
| planes_tilt | | `List[float]` | `ro` | `N/A` | Compute a list of the tilts per active planes. | | planes_tilt | | `List[float]` | `ro` | `N/A` | Compute a list of the tilts per active planes. |
@@ -601,11 +569,10 @@ Validators:
{ {
"pvforecast": { "pvforecast": {
"provider": "PVForecastAkkudoktor", "provider": "PVForecastAkkudoktor",
"provider_settings": null,
"planes": [ "planes": [
{ {
"surface_tilt": 10.0, "surface_tilt": 10.0,
"surface_azimuth": 180.0, "surface_azimuth": 10.0,
"userhorizon": [ "userhorizon": [
10.0, 10.0,
20.0, 20.0,
@@ -627,7 +594,7 @@ Validators:
}, },
{ {
"surface_tilt": 20.0, "surface_tilt": 20.0,
"surface_azimuth": 90.0, "surface_azimuth": 20.0,
"userhorizon": [ "userhorizon": [
5.0, 5.0,
15.0, 15.0,
@@ -648,7 +615,7 @@ Validators:
"strings_per_inverter": 2 "strings_per_inverter": 2
} }
], ],
"max_planes": 0 "provider_settings": null
} }
} }
``` ```
@@ -661,11 +628,10 @@ Validators:
{ {
"pvforecast": { "pvforecast": {
"provider": "PVForecastAkkudoktor", "provider": "PVForecastAkkudoktor",
"provider_settings": null,
"planes": [ "planes": [
{ {
"surface_tilt": 10.0, "surface_tilt": 10.0,
"surface_azimuth": 180.0, "surface_azimuth": 10.0,
"userhorizon": [ "userhorizon": [
10.0, 10.0,
20.0, 20.0,
@@ -687,7 +653,7 @@ Validators:
}, },
{ {
"surface_tilt": 20.0, "surface_tilt": 20.0,
"surface_azimuth": 90.0, "surface_azimuth": 20.0,
"userhorizon": [ "userhorizon": [
5.0, 5.0,
15.0, 15.0,
@@ -708,14 +674,14 @@ Validators:
"strings_per_inverter": 2 "strings_per_inverter": 2
} }
], ],
"max_planes": 0, "provider_settings": null,
"planes_peakpower": [ "planes_peakpower": [
5.0, 5.0,
3.5 3.5
], ],
"planes_azimuth": [ "planes_azimuth": [
180.0, 10.0,
90.0 20.0
], ],
"planes_tilt": [ "planes_tilt": [
10.0, 10.0,
@@ -741,116 +707,6 @@ Validators:
} }
``` ```
### PV Forecast Plane Configuration
:::{table} pvforecast::planes::list
:widths: 10 10 5 5 30
:align: left
| Name | Type | Read-Only | Default | Description |
| ---- | ---- | --------- | ------- | ----------- |
| surface_tilt | `Optional[float]` | `rw` | `30.0` | Tilt angle from horizontal plane. Ignored for two-axis tracking. |
| surface_azimuth | `Optional[float]` | `rw` | `180.0` | Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, west=270). |
| userhorizon | `Optional[List[float]]` | `rw` | `None` | Elevation of horizon in degrees, at equally spaced azimuth clockwise from north. |
| peakpower | `Optional[float]` | `rw` | `None` | Nominal power of PV system in kW. |
| pvtechchoice | `Optional[str]` | `rw` | `crystSi` | PV technology. One of 'crystSi', 'CIS', 'CdTe', 'Unknown'. |
| mountingplace | `Optional[str]` | `rw` | `free` | Type of mounting for PV system. Options are 'free' for free-standing and 'building' for building-integrated. |
| loss | `Optional[float]` | `rw` | `14.0` | Sum of PV system losses in percent |
| trackingtype | `Optional[int]` | `rw` | `None` | Type of suntracking. 0=fixed, 1=single horizontal axis aligned north-south, 2=two-axis tracking, 3=vertical axis tracking, 4=single horizontal axis aligned east-west, 5=single inclined axis aligned north-south. |
| optimal_surface_tilt | `Optional[bool]` | `rw` | `False` | Calculate the optimum tilt angle. Ignored for two-axis tracking. |
| optimalangles | `Optional[bool]` | `rw` | `False` | Calculate the optimum tilt and azimuth angles. Ignored for two-axis tracking. |
| albedo | `Optional[float]` | `rw` | `None` | Proportion of the light hitting the ground that it reflects back. |
| module_model | `Optional[str]` | `rw` | `None` | Model of the PV modules of this plane. |
| inverter_model | `Optional[str]` | `rw` | `None` | Model of the inverter of this plane. |
| inverter_paco | `Optional[int]` | `rw` | `None` | AC power rating of the inverter [W]. |
| modules_per_string | `Optional[int]` | `rw` | `None` | Number of the PV modules of the strings of this plane. |
| strings_per_inverter | `Optional[int]` | `rw` | `None` | Number of the strings of the inverter of this plane. |
:::
#### Example Input/Output
```{eval-rst}
.. code-block:: json
{
"pvforecast": {
"planes": [
{
"surface_tilt": 10.0,
"surface_azimuth": 180.0,
"userhorizon": [
10.0,
20.0,
30.0
],
"peakpower": 5.0,
"pvtechchoice": "crystSi",
"mountingplace": "free",
"loss": 14.0,
"trackingtype": 0,
"optimal_surface_tilt": false,
"optimalangles": false,
"albedo": null,
"module_model": null,
"inverter_model": null,
"inverter_paco": 6000,
"modules_per_string": 20,
"strings_per_inverter": 2
},
{
"surface_tilt": 20.0,
"surface_azimuth": 90.0,
"userhorizon": [
5.0,
15.0,
25.0
],
"peakpower": 3.5,
"pvtechchoice": "crystSi",
"mountingplace": "free",
"loss": 14.0,
"trackingtype": 1,
"optimal_surface_tilt": false,
"optimalangles": false,
"albedo": null,
"module_model": null,
"inverter_model": null,
"inverter_paco": 4000,
"modules_per_string": 20,
"strings_per_inverter": 2
}
]
}
}
```
### Common settings for VRM API
:::{table} pvforecast::provider_settings
:widths: 10 10 5 5 30
:align: left
| Name | Type | Read-Only | Default | Description |
| ---- | ---- | --------- | ------- | ----------- |
| pvforecast_vrm_token | `str` | `rw` | `your-token` | Token for Connecting VRM API |
| pvforecast_vrm_idsite | `int` | `rw` | `12345` | VRM-Installation-ID |
:::
#### Example Input/Output
```{eval-rst}
.. code-block:: json
{
"pvforecast": {
"provider_settings": {
"pvforecast_vrm_token": "your-token",
"pvforecast_vrm_idsite": 12345
}
}
}
```
### Common settings for pvforecast data import from file or JSON string ### Common settings for pvforecast data import from file or JSON string
:::{table} pvforecast::provider_settings :::{table} pvforecast::provider_settings
@@ -878,6 +734,89 @@ Validators:
} }
``` ```
### PV Forecast Plane Configuration
:::{table} pvforecast::planes::list
:widths: 10 10 5 5 30
:align: left
| Name | Type | Read-Only | Default | Description |
| ---- | ---- | --------- | ------- | ----------- |
| surface_tilt | `Optional[float]` | `rw` | `None` | Tilt angle from horizontal plane. Ignored for two-axis tracking. |
| surface_azimuth | `Optional[float]` | `rw` | `None` | Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, west=270). |
| userhorizon | `Optional[List[float]]` | `rw` | `None` | Elevation of horizon in degrees, at equally spaced azimuth clockwise from north. |
| peakpower | `Optional[float]` | `rw` | `None` | Nominal power of PV system in kW. |
| pvtechchoice | `Optional[str]` | `rw` | `crystSi` | PV technology. One of 'crystSi', 'CIS', 'CdTe', 'Unknown'. |
| mountingplace | `Optional[str]` | `rw` | `free` | Type of mounting for PV system. Options are 'free' for free-standing and 'building' for building-integrated. |
| loss | `Optional[float]` | `rw` | `14.0` | Sum of PV system losses in percent |
| trackingtype | `Optional[int]` | `rw` | `None` | Type of suntracking. 0=fixed, 1=single horizontal axis aligned north-south, 2=two-axis tracking, 3=vertical axis tracking, 4=single horizontal axis aligned east-west, 5=single inclined axis aligned north-south. |
| optimal_surface_tilt | `Optional[bool]` | `rw` | `False` | Calculate the optimum tilt angle. Ignored for two-axis tracking. |
| optimalangles | `Optional[bool]` | `rw` | `False` | Calculate the optimum tilt and azimuth angles. Ignored for two-axis tracking. |
| albedo | `Optional[float]` | `rw` | `None` | Proportion of the light hitting the ground that it reflects back. |
| module_model | `Optional[str]` | `rw` | `None` | Model of the PV modules of this plane. |
| inverter_model | `Optional[str]` | `rw` | `None` | Model of the inverter of this plane. |
| inverter_paco | `Optional[int]` | `rw` | `None` | AC power rating of the inverter. [W] |
| modules_per_string | `Optional[int]` | `rw` | `None` | Number of the PV modules of the strings of this plane. |
| strings_per_inverter | `Optional[int]` | `rw` | `None` | Number of the strings of the inverter of this plane. |
:::
#### Example Input/Output
```{eval-rst}
.. code-block:: json
{
"pvforecast": {
"planes": [
{
"surface_tilt": 10.0,
"surface_azimuth": 10.0,
"userhorizon": [
10.0,
20.0,
30.0
],
"peakpower": 5.0,
"pvtechchoice": "crystSi",
"mountingplace": "free",
"loss": 14.0,
"trackingtype": 0,
"optimal_surface_tilt": false,
"optimalangles": false,
"albedo": null,
"module_model": null,
"inverter_model": null,
"inverter_paco": 6000,
"modules_per_string": 20,
"strings_per_inverter": 2
},
{
"surface_tilt": 20.0,
"surface_azimuth": 20.0,
"userhorizon": [
5.0,
15.0,
25.0
],
"peakpower": 3.5,
"pvtechchoice": "crystSi",
"mountingplace": "free",
"loss": 14.0,
"trackingtype": 1,
"optimal_surface_tilt": false,
"optimalangles": false,
"albedo": null,
"module_model": null,
"inverter_model": null,
"inverter_paco": 4000,
"modules_per_string": 20,
"strings_per_inverter": 2
}
]
}
}
```
## Weather Forecast Configuration ## Weather Forecast Configuration
:::{table} weather :::{table} weather
@@ -938,11 +877,11 @@ Validators:
| Name | Environment Variable | Type | Read-Only | Default | Description | | Name | Environment Variable | Type | Read-Only | Default | Description |
| ---- | -------------------- | ---- | --------- | ------- | ----------- | | ---- | -------------------- | ---- | --------- | ------- | ----------- |
| host | `EOS_SERVER__HOST` | `Optional[pydantic.networks.IPvAnyAddress]` | `rw` | `127.0.0.1` | EOS server IP address. | | host | `EOS_SERVER__HOST` | `Optional[pydantic.networks.IPvAnyAddress]` | `rw` | `0.0.0.0` | EOS server IP address. |
| port | `EOS_SERVER__PORT` | `Optional[int]` | `rw` | `8503` | EOS server IP port number. | | port | `EOS_SERVER__PORT` | `Optional[int]` | `rw` | `8503` | EOS server IP port number. |
| verbose | `EOS_SERVER__VERBOSE` | `Optional[bool]` | `rw` | `False` | Enable debug output | | verbose | `EOS_SERVER__VERBOSE` | `Optional[bool]` | `rw` | `False` | Enable debug output |
| startup_eosdash | `EOS_SERVER__STARTUP_EOSDASH` | `Optional[bool]` | `rw` | `True` | EOS server to start EOSdash server. | | startup_eosdash | `EOS_SERVER__STARTUP_EOSDASH` | `Optional[bool]` | `rw` | `True` | EOS server to start EOSdash server. |
| eosdash_host | `EOS_SERVER__EOSDASH_HOST` | `Optional[pydantic.networks.IPvAnyAddress]` | `rw` | `127.0.0.1` | EOSdash server IP address. | | eosdash_host | `EOS_SERVER__EOSDASH_HOST` | `Optional[pydantic.networks.IPvAnyAddress]` | `rw` | `0.0.0.0` | EOSdash server IP address. |
| eosdash_port | `EOS_SERVER__EOSDASH_PORT` | `Optional[int]` | `rw` | `8504` | EOSdash server IP port number. | | eosdash_port | `EOS_SERVER__EOSDASH_PORT` | `Optional[int]` | `rw` | `8504` | EOSdash server IP port number. |
::: :::
@@ -953,11 +892,11 @@ Validators:
{ {
"server": { "server": {
"host": "127.0.0.1", "host": "0.0.0.0",
"port": 8503, "port": 8503,
"verbose": false, "verbose": false,
"startup_eosdash": true, "startup_eosdash": true,
"eosdash_host": "127.0.0.1", "eosdash_host": "0.0.0.0",
"eosdash_port": 8504 "eosdash_port": 8504
} }
} }
@@ -1004,9 +943,7 @@ Validators:
"interval": 300.0 "interval": 300.0
}, },
"logging": { "logging": {
"level": null, "level": "INFO"
"console_level": "TRACE",
"file_level": "TRACE"
}, },
"devices": { "devices": {
"batteries": [ "batteries": [
@@ -1052,7 +989,6 @@ Validators:
"elecprice": { "elecprice": {
"provider": "ElecPriceAkkudoktor", "provider": "ElecPriceAkkudoktor",
"charges_kwh": 0.21, "charges_kwh": 0.21,
"vat_rate": 1.19,
"provider_settings": null "provider_settings": null
}, },
"load": { "load": {
@@ -1061,11 +997,10 @@ Validators:
}, },
"pvforecast": { "pvforecast": {
"provider": "PVForecastAkkudoktor", "provider": "PVForecastAkkudoktor",
"provider_settings": null,
"planes": [ "planes": [
{ {
"surface_tilt": 10.0, "surface_tilt": 10.0,
"surface_azimuth": 180.0, "surface_azimuth": 10.0,
"userhorizon": [ "userhorizon": [
10.0, 10.0,
20.0, 20.0,
@@ -1087,7 +1022,7 @@ Validators:
}, },
{ {
"surface_tilt": 20.0, "surface_tilt": 20.0,
"surface_azimuth": 90.0, "surface_azimuth": 20.0,
"userhorizon": [ "userhorizon": [
5.0, 5.0,
15.0, 15.0,
@@ -1108,18 +1043,18 @@ Validators:
"strings_per_inverter": 2 "strings_per_inverter": 2
} }
], ],
"max_planes": 0 "provider_settings": null
}, },
"weather": { "weather": {
"provider": "WeatherImport", "provider": "WeatherImport",
"provider_settings": null "provider_settings": null
}, },
"server": { "server": {
"host": "127.0.0.1", "host": "0.0.0.0",
"port": 8503, "port": 8503,
"verbose": false, "verbose": false,
"startup_eosdash": true, "startup_eosdash": true,
"eosdash_host": "127.0.0.1", "eosdash_host": "0.0.0.0",
"eosdash_port": 8504 "eosdash_port": 8504
}, },
"utils": {} "utils": {}

View File

@@ -430,13 +430,7 @@ Returns:
**Request Body**: **Request Body**:
- `application/json`: { - `application/json`: {
"anyOf": [ "description": "The value to assign to the specified configuration path.",
{},
{
"type": "null"
}
],
"description": "The value to assign to the specified configuration path (can be None).",
"title": "Value" "title": "Value"
} }
@@ -464,55 +458,6 @@ Health check endpoint to verify that the EOS server is alive.
--- ---
## GET /v1/logging/log
**Links**: [local](http://localhost:8503/docs#/default/fastapi_logging_get_log_v1_logging_log_get), [eos](https://petstore3.swagger.io/?url=https://raw.githubusercontent.com/Akkudoktor-EOS/EOS/refs/heads/main/openapi.json#/default/fastapi_logging_get_log_v1_logging_log_get)
Fastapi Logging Get Log
```
Get structured log entries from the EOS log file.
Filters and returns log entries based on the specified query parameters. The log
file is expected to contain newline-delimited JSON entries.
Args:
limit (int): Maximum number of entries to return.
level (Optional[str]): Filter logs by severity level (e.g., DEBUG, INFO).
contains (Optional[str]): Return only logs that include this string in the message.
regex (Optional[str]): Return logs that match this regular expression in the message.
from_time (Optional[str]): ISO 8601 timestamp to filter logs not older than this.
to_time (Optional[str]): ISO 8601 timestamp to filter logs not newer than this.
tail (bool): If True, fetch the most recent log entries (like `tail`).
Returns:
JSONResponse: A JSON list of log entries.
```
**Parameters**:
- `limit` (query, optional): Maximum number of log entries to return.
- `level` (query, optional): Filter by log level (e.g., INFO, ERROR).
- `contains` (query, optional): Filter logs containing this substring.
- `regex` (query, optional): Filter logs by matching regex in message.
- `from_time` (query, optional): Start time (ISO format) for filtering logs.
- `to_time` (query, optional): End time (ISO format) for filtering logs.
- `tail` (query, optional): If True, returns the most recent lines (tail mode).
**Responses**:
- **200**: Successful Response
- **422**: Validation Error
---
## PUT /v1/measurement/data ## PUT /v1/measurement/data
**Links**: [local](http://localhost:8503/docs#/default/fastapi_measurement_data_put_v1_measurement_data_put), [eos](https://petstore3.swagger.io/?url=https://raw.githubusercontent.com/Akkudoktor-EOS/EOS/refs/heads/main/openapi.json#/default/fastapi_measurement_data_put_v1_measurement_data_put) **Links**: [local](http://localhost:8503/docs#/default/fastapi_measurement_data_put_v1_measurement_data_put), [eos](https://petstore3.swagger.io/?url=https://raw.githubusercontent.com/Akkudoktor-EOS/EOS/refs/heads/main/openapi.json#/default/fastapi_measurement_data_put_v1_measurement_data_put)
@@ -798,8 +743,7 @@ Args:
"$ref": "#/components/schemas/PydanticDateTimeData" "$ref": "#/components/schemas/PydanticDateTimeData"
}, },
{ {
"type": "object", "type": "object"
"additionalProperties": true
}, },
{ {
"type": "null" "type": "null"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 664 KiB

View File

@@ -0,0 +1,9 @@
% SPDX-License-Identifier: Apache-2.0
# About Akkudoktor EOS
The Energy System Simulation and Optimization System (EOS) provides a comprehensive solution for
simulating and optimizing an energy system based on renewable energy sources. With a focus on
photovoltaic (PV) systems, battery storage (batteries), load management (consumer requirements),
heat pumps, electric vehicles, and consideration of electricity price data, this system enables
forecasting and optimization of energy flow and costs over a specified period.

View File

@@ -20,22 +20,17 @@ EOS Architecture
### Configuration ### Configuration
The configuration controls all aspects of EOS: optimization, prediction, measurement, and energy The configuration controls all aspects of EOS: optimization, prediction, measurement, and energy management.
management.
### Energy Management ### Energy Management
Energy management is the overall process to provide planning data for scheduling the different Energy management is the overall process to provide planning data for scheduling the different devices in your system in an optimal way. Energy management cares for the update of predictions and the optimization of the planning based on the simulated behavior of the devices. The planning is on the hour. Sub-hour energy management is left
devices in your system in an optimal way. Energy management cares for the update of predictions and
the optimization of the planning based on the simulated behavior of the devices. The planning is on
the hour.
### Optimization ### Optimization
### Device Simulations ### Device Simulations
Device simulations simulate devices' behavior based on internal logic and predicted data. They Device simulations simulate devices' behavior based on internal logic and predicted data. They provide the data needed for optimization.
provide the data needed for optimization.
### Predictions ### Predictions
@@ -43,8 +38,7 @@ Predictions provide predicted future data to be used by the optimization.
### Measurements ### Measurements
Measurements are utilized to refine predictions using real data from your system, thereby enhancing Measurements are utilized to refine predictions using real data from your system, thereby enhancing accuracy.
accuracy.
### EOS Server ### EOS Server

View File

@@ -31,10 +31,10 @@ Use endpoint `POST /v1/config/reset` to reset the configuration to the values in
The configuration sources and their priorities are as follows: The configuration sources and their priorities are as follows:
1. `Settings`: Provided during runtime by the REST interface 1. **Runtime Config Updates**: Provided during runtime by the REST interface
2. `Environment Variables`: Defined at startup of the REST server and during runtime 2. **Environment Variables**: Defined at startup of the REST server and during runtime
3. `EOS Configuration File`: Read at startup of the REST server and on request 3. **EOS Configuration File**: Read at startup of the REST server and on request
4. `Default Values` 4. **Default Values**
### Runtime Config Updates ### Runtime Config Updates

View File

@@ -1,5 +1,4 @@
% SPDX-License-Identifier: Apache-2.0 % SPDX-License-Identifier: Apache-2.0
(integration-page)=
# Integration # Integration
@@ -18,24 +17,18 @@ APIs, and online services in creative and practical ways.
Andreas Schmitz uses [Node-RED](https://nodered.org/) as part of his home automation setup. Andreas Schmitz uses [Node-RED](https://nodered.org/) as part of his home automation setup.
### Node-Red Resources ### Resources
- [Installation Guide (German)](https://www.youtube.com/playlist?list=PL8_vk9A-s7zLD865Oou6y3EeQLlNtu-Hn) - [Installation Guide (German)](https://meintechblog.de/2024/09/05/andreas-schmitz-joerg-installiert-mein-energieoptimierungssystem/) — A detailed guide on integrating an early version of EOS with
\— A detailed guide on integrating EOS with `Node-RED`. `Node-RED`.
## Home Assistant ## Home Assistant
[Home Assistant](https://www.home-assistant.io/) is an open-source home automation platform that [Home Assistant](https://www.home-assistant.io/) is an open-source home automation platform that
emphasizes local control and user privacy. emphasizes local control and user privacy.
(duetting-solution)= ### Resources
### Home Assistant Resources
- Duetting's [EOS Home Assistant Addon](https://github.com/Duetting/ha_eos_addon) — Additional - Duetting's [EOS Home Assistant Addon](https://github.com/Duetting/ha_eos_addon) — Additional
details can be found in this [discussion thread](https://github.com/Akkudoktor-EOS/EOS/discussions/294). details can be found in this
[discussion thread](https://github.com/Akkudoktor-EOS/EOS/discussions/294).
## EOS Connect
[EOS connect](https://github.com/ohAnd/EOS_connect) uses `EOS` for energy management and optimization,
and connects to smart home platforms to monitor, forecast, and control energy flows.

View File

@@ -1,180 +0,0 @@
% SPDX-License-Identifier: Apache-2.0
# Introduction
The Energy System Simulation and Optimization System (EOS) provides a comprehensive
solution for simulating and optimizing an energy system based on renewable energy
sources. With a focus on photovoltaic (PV) systems, battery storage (batteries), load
management (consumer requirements), heat pumps, electric vehicles, and consideration of
electricity price data, this system enables forecasting and optimization of energy flow
and costs over a specified period.
After successfully installing a PV system with or without battery storage, most owners
first priority is often to charge the electric car with surplus energy in order to use
the electricity generated by the PV system cost-effectively for electromobility.
After initial experiences, the desire to include battery storage and dynamic electricity
prices in the solution soon arises. The market already offers various commercial and
non-commercial solutions for this, such as the popular open source hardware and software
solutions evcc or openWB.
Some solutions take into account the current values of the system such as PV power
output, battery storage charge level or the current electricity price to decide whether
to charge the electric car with PV surplus or from the grid (e.g. openWB), some use
historical consumption values and PV forecast data for their calculations, but leave out
the current electricity prices and charging the battery storage from the power grid
(Predbat). Others are specialiced on working in combination with a specific smart home
solution (e.g. emhass). Still others focus on certain consumers, such as the electric car,
or are currently working on integrating the forecast values (evcc). And some are commercial
devices that require an electrician to install them and expect a certain ecosystem
(e.g. Sunny Home Manager).
The Akkudoktor EOS
- takes into account historical, current and forecast data such as consumption values, PV
forecast data, electricity price forecast, battery storage and electric car charge levels
- the simulation also takes into account the possibility of charging the battery storage
from the grid at low electricity prices
- is not limited to certain consumers, but includes electric cars, heat pumps or more
powerful consumers such as tumble dryers
- is independent of a specific smart home solution and can also be integrated into
self-developed solutions if desired
- is a free and independent open source software solution
![Introdution](../_static/introduction/introduction.png)
The challenge is to charge (electric car) or start the consumers (washing machine, dryer)
at the right time and to do so as cost-efficiently as possible. If PV yield forecast,
battery storage and dynamic electricity price forecasts are included in the calculation,
the possibilities increase, but unfortunately so does the complexity.
The Akkudoktor EOS addresses this challenge by simulating energy flows in the household
based on target values, forecast data and current operating data over a 48-hour
observation period, running through a large number of different scenarios and finally
providing a cost-optimized plan for the current day controlling the relevant consumers.
## Prerequisites
- Technical requirements
- Input data
### Technical requirements
- reasonably fast computer on which EOS is installed
- controllable energy system consisting of photovoltaic system, solar battery storage,
energy intensive consumers that must provide the appropriate interfaces
- integration solution for integrating the energy system and EOS
### Input Data
![Overview](../_static/introduction/overview.png)
The EOS requires various types of data for the simulation:
Forecast data
- PV yield forecast
- Expected household consumption
- Electricity price forecast
- Forecast temperature trend (if heatpump is used)
Basic data and current operating data
- Current charge level of the battery storage
- Value of electricity in the battery storage
- Current charge level of the electric car
- Energy consumption and running time of dishwasher, washing machine and tumble dryer
Target values
- Charge level the electric car should reach in the next few hours
- Consumers to run in the next few hours
There are various service providers available for PV forecasting that calculate forecast
data for a PV system based on the various influencing factors, such as system size,
orientation, location, time of year and weather conditions. EOS also offers a
[PV forecasting service](#prediction-page) which can be used. This service uses
public data in the background.
For the forecast of household consumption EOS provides a standard load curve for an
average day based on annual household consumption that you can fetch via API. This data
was compiled based on data from several households and provides an initial usable basis.
Alternatively your own collected historical data could be used to reflect your personal
consumption behaviour.
## Simulation Results
Based on the input data, the EOS uses a genetic algorithm to create a cost-optimized
schedule for the coming hours from numerous simulations of the overall system.
The plan created contains for each of the coming hours
- Control information
- whether and with what power the battery storage should be charged from the grid
- when the battery storage should be charged via the PV system
- whether discharging the battery storage is permitted or not
- when and with what power the electric car should be charged
- when a household appliance should be activated
- Energy history information
- Total load of the house
- Grid consumption
- Feed-in
- Load of the planned household appliances
- Charge level of the battery storage
- Charge level of the electric car
- Active losses
- Cost information
- Revenue per hour (when fed into the grid)
- Total costs per hour (when drawn from the grid)
- Overall balance (revenue-costs)
- Cost development
If required, the simulation result can also be created and downloaded in graphical
form as a PDF from EOS.
## Integration
The Akkudoktor EOS can be integrated into a wide variety of systems with a variety
of components.
![Integration](../_static/introduction/integration.png)
However, the components are not integrated by the EOS itself, but must be integrated by
the user using an integration solution and currently requires some effort and technical
know-how.
Any [integration](#integration-page) solution that can act as an intermediary between the
components and the REST API of EOS can be used. One possible solution that enables the
integration of components and EOS is Node-RED. Another solution could be Home Assistant
usings its built in features.
Access to the data and functions of the components can be done in a variety of ways.
Node-RED offers a large number of types of nodes that allow access via the protocols
commonly used in this area, such as Modbus or MQTT. Access to any existing databases,
such as InfluxDB or PostgreSQL, is also possible via nodes provided by Node-RED.
It becomes easier if a smart home solution like Home Assistant, openHAB or ioBroker or
solutions such as evcc or openWB are already in use. In this case, these smart home
solutions already take over the technical integration and communication with the components
at a technical level and Node-RED offers nodes for accessing these solutions, so that the
corresponding sources can be easily integrated into a flow.
In Home Assistant you could use an automation to prepare the input payload for EOS and
then use the RESTful integration to call EOS. Based on this concept there is already a
Home Assistant add-on created by [Duetting](#duetting-solution).
The plan created by EOS must also be executed via the chosen integration solution,
with the respective devices receiving their instructions according to the plan.
## Limitations
The plan calculated by EOS is cost-optimized due to the genetic algorithm used, but not
necessarily cost-optimal, since genetic algorithms do not always find the global optimum,
but usually find good local optima very quickly in a large solution space.
## Links
- [German Videos explaining the basic concept and installation process of EOS (YouTube)](https://www.youtube.com/playlist?list=PL8_vk9A-s7zLD865Oou6y3EeQLlNtu-Hn)
- [German Forum of Akkudoktor EOS](https://akkudoktor.net/c/der-akkudoktor/eos)
- [Akkudoktor-EOS GitHub Repository](https://github.com/Akkudoktor-EOS/EOS)
- [Latest EOS Documentation](https://akkudoktor-eos.readthedocs.io/en/latest/)

View File

@@ -1,75 +0,0 @@
% SPDX-License-Identifier: Apache-2.0
(logging-page)=
# Logging
EOS automatically records important events and messages to help you understand whats happening and
to troubleshoot problems.
## How Logging Works
- By default, logs are shown in your terminal (console).
- You can also save logs to a file for later review.
- Log files are rotated automatically to avoid becoming too large.
## Controlling Log Details
### 1. Command-Line Option
Set the amount of log detail shown on the console by using `--log-level` when starting EOS.
Example:
```{eval-rst}
.. tabs::
.. tab:: Windows
.. code-block:: powershell
.venv\Scripts\python src/akkudoktoreos/server/eos.py --log-level DEBUG
.. tab:: Linux
.. code-block:: bash
.venv/bin/python src/akkudoktoreos/server/eos.py --log-level DEBUG
```
Common levels:
- DEBUG (most detail)
- INFO (default)
- WARNING
- ERROR
- CRITICAL (least detail)
### 2. Configuration File
You can also set logging options in your EOS configuration file (EOS.config.json).
```Json
{
"logging": {
"console_level": "INFO",
"file_level": "DEBUG"
}
}
```
### 3. Environment Variable
You can also control the log level by setting the `EOS_LOGGING__CONSOLE_LEVEL` and the
`EOS_LOGGING__FILE_LEVEL` environment variables.
```bash
EOS_LOGGING__CONSOLE_LEVEL="INFO"
EOS_LOGGING__FILE_LEVEL="DEBUG"
```
## File Logging
If the `file_level` configuration is set, log records are written to a rotating log file. The log
file is in the data output directory and named `eos.log`. You may directly read the file or use
the `/v1/logging/log` endpoint to access the file log.

View File

@@ -5,9 +5,9 @@
Measurements are utilized to refine predictions using real data from your system, thereby enhancing Measurements are utilized to refine predictions using real data from your system, thereby enhancing
accuracy. accuracy.
- Household Load Measurement - **Household Load Measurement**
- Grid Export Measurement - **Grid Export Measurement**
- Grid Import Measurement - **Grid Import Measurement**
## Storing Measurements ## Storing Measurements

View File

@@ -2,207 +2,7 @@
# Optimization # Optimization
## Introduction :::{admonition} Todo
:class: note
The `POST /optimize` API endpoint optimizes your energy management system based on various inputs Describe optimization.
including electricity prices, battery storage capacity, PV forecast, and temperature data. :::
## Input Payload
### Sample Request
```json
{
"ems": {
"preis_euro_pro_wh_akku": 0.0007,
"einspeiseverguetung_euro_pro_wh": 0.00007,
"gesamtlast": [500, 500, ..., 500, 500],
"pv_prognose_wh": [300, 0, 0, ..., 2160, 1840],
"strompreis_euro_pro_wh": [0.0003784, 0.0003868, ..., 0.00034102, 0.00033709]
},
"pv_akku": {
"device_id": "battery1",
"capacity_wh": 12000,
"charging_efficiency": 0.92,
"discharging_efficiency": 0.92,
"max_charge_power_w": 5700,
"initial_soc_percentage": 66,
"min_soc_percentage": 5,
"max_soc_percentage": 100
},
"inverter": {
"device_id": "inverter1",
"max_power_wh": 15500
"battery_id": "battery1",
},
"eauto": {
"device_id": "auto1",
"capacity_wh": 64000,
"charging_efficiency": 0.88,
"discharging_efficiency": 0.88,
"max_charge_power_w": 11040,
"initial_soc_percentage": 98,
"min_soc_percentage": 60,
"max_soc_percentage": 100
},
"temperature_forecast": [18.3, 18, ..., 20.16, 19.84],
"start_solution": null
}
```
## Input Parameters
### Energy Management System (EMS)
#### Battery Cost (`preis_euro_pro_wh_akku`)
- Unit: €/Wh
- Purpose: Represents the residual value of energy stored in the battery
- Impact: Lower values encourage battery depletion, higher values preserve charge at the end of the simulation.
#### Feed-in Tariff (`einspeiseverguetung_euro_pro_wh`)
- Unit: €/Wh
- Purpose: Compensation received for feeding excess energy back to the grid
#### Total Load Forecast (`gesamtlast`)
- Unit: W
- Time Range: 48 hours (00:00 today to 23:00 tomorrow)
- Format: Array of hourly values
- Note: Exclude optimizable loads (EV charging, battery charging, etc.)
##### Data Sources
1. Standard Load Profile: `GET /v1/prediction/list?key=load_mean` for a standard load profile based
on your yearly consumption.
2. Adjusted Load Profile: `GET /v1/prediction/list?key=load_mean_adjusted` for a combination of a
standard load profile based on your yearly consumption incl. data from last 48h.
#### PV Generation Forecast (`pv_prognose_wh`)
- Unit: W
- Time Range: 48 hours (00:00 today to 23:00 tomorrow)
- Format: Array of hourly values
- Data Source: `GET /v1/prediction/series?key=pvforecast_ac_power`
#### Electricity Price Forecast (`strompreis_euro_pro_wh`)
- Unit: €/Wh
- Time Range: 48 hours (00:00 today to 23:00 tomorrow)
- Format: Array of hourly values
- Data Source: `GET /v1/prediction/list?key=elecprice_marketprice_wh`
Verify prices against your local tariffs.
### Battery Storage System
#### Configuration
- `device_id`: ID of battery
- `capacity_wh`: Total battery capacity in Wh
- `charging_efficiency`: Charging efficiency (0-1)
- `discharging_efficiency`: Discharging efficiency (0-1)
- `max_charge_power_w`: Maximum charging power in W
#### State of Charge (SoC)
- `initial_soc_percentage`: Current battery level (%)
- `min_soc_percentage`: Minimum allowed SoC (%)
- `max_soc_percentage`: Maximum allowed SoC (%)
### Inverter
- `device_id`: ID of inverter
- `max_power_wh`: Maximum inverter power in Wh
- `battery_id`: ID of battery
### Electric Vehicle (EV)
- `device_id`: ID of electric vehicle
- `capacity_wh`: Battery capacity in Wh
- `charging_efficiency`: Charging efficiency (0-1)
- `discharging_efficiency`: Discharging efficiency (0-1)
- `max_charge_power_w`: Maximum charging power in W
- `initial_soc_percentage`: Current charge level (%)
- `min_soc_percentage`: Minimum allowed SoC (%)
- `max_soc_percentage`: Maximum allowed SoC (%)
### Temperature Forecast
- Unit: °C
- Time Range: 48 hours (00:00 today to 23:00 tomorrow)
- Format: Array of hourly values
- Data Source: `GET /v1/prediction/list?key=weather_temp_air`
## Output Format
### Sample Response
```json
{
"ac_charge": [0.625, 0, ..., 0.75, 0],
"dc_charge": [1, 1, ..., 1, 1],
"discharge_allowed": [0, 0, 1, ..., 0, 0],
"eautocharge_hours_float": [0.625, 0, ..., 0.75, 0],
"result": {
"Last_Wh_pro_Stunde": [...],
"EAuto_SoC_pro_Stunde": [...],
"Einnahmen_Euro_pro_Stunde": [...],
"Gesamt_Verluste": 1514.96,
"Gesamtbilanz_Euro": 2.51,
"Gesamteinnahmen_Euro": 2.88,
"Gesamtkosten_Euro": 5.39,
"akku_soc_pro_stunde": [...]
}
}
```
### Output Parameters
#### Battery Control
- `ac_charge`: Grid charging schedule (0-1)
- `dc_charge`: DC charging schedule (0-1)
- `discharge_allowed`: Discharge permission (0 or 1)
0 (no charge)
1 (charge with full load)
`ac_charge` multiplied by the maximum charge power of the battery results in the planned charging power.
#### EV Charging
- `eautocharge_hours_float`: EV charging schedule (0-1)
#### Results
The `result` object contains detailed information about the optimization outcome.
The length of the array is between 25 and 48 and starts at the current hour and ends at 23:00 tomorrow.
- `Last_Wh_pro_Stunde`: Array of hourly load values in Wh
- Shows the total energy consumption per hour
- Includes household load, battery charging/discharging, and EV charging
- `EAuto_SoC_pro_Stunde`: Array of hourly EV state of charge values (%)
- Shows the projected EV battery level throughout the optimization period
- `Einnahmen_Euro_pro_Stunde`: Array of hourly revenue values in Euro
- `Gesamt_Verluste`: Total energy losses in Wh
- `Gesamtbilanz_Euro`: Overall financial balance in Euro
- `Gesamteinnahmen_Euro`: Total revenue in Euro
- `Gesamtkosten_Euro`: Total costs in Euro
- `akku_soc_pro_stunde`: Array of hourly battery state of charge values (%)
## Timeframe overview
```{figure} ../_static/optimization_timeframes.png
:alt: Timeframe Overview
Timeframe Overview
```

View File

@@ -1,15 +1,14 @@
% SPDX-License-Identifier: Apache-2.0 % SPDX-License-Identifier: Apache-2.0
(prediction-page)=
# Predictions # Predictions
Predictions, along with simulations and measurements, form the foundation upon which energy Predictions, along with simulations and measurements, form the foundation upon which energy
optimization is executed. In EOS, a standard set of predictions is managed, including: optimization is executed. In EOS, a standard set of predictions is managed, including:
- Household Load Prediction - **Household Load Prediction**
- Electricity Price Prediction - **Electricity Price Prediction**
- PV Power Prediction - **PV Power Prediction**
- Weather Prediction - **Weather Prediction**
## Storing Predictions ## Storing Predictions
@@ -61,15 +60,13 @@ A dictionary with the following structure:
#### 2. DateTimeDataFrame #### 2. DateTimeDataFrame
A JSON string created from a [pandas](https://pandas.pydata.org/docs/index.html) dataframe with a A JSON string created from a [pandas](https://pandas.pydata.org/docs/index.html) dataframe with a
`DatetimeIndex`. Use `DatetimeIndex`. Use [pandas.DataFrame.to_json(orient="index")](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html#pandas.DataFrame.to_json).
[pandas.DataFrame.to_json(orient="index")](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html#pandas.DataFrame.to_json).
The column name of the data must be the same as the names of the `prediction key`s. The column name of the data must be the same as the names of the `prediction key`s.
#### 3. DateTimeSeries #### 3. DateTimeSeries
A JSON string created from a [pandas](https://pandas.pydata.org/docs/index.html) series with a A JSON string created from a [pandas](https://pandas.pydata.org/docs/index.html) series with a
`DatetimeIndex`. Use `DatetimeIndex`. Use [pandas.Series.to_json(orient="index")](https://pandas.pydata.org/docs/reference/api/pandas.Series.to_json.html#pandas.Series.to_json).
[pandas.Series.to_json(orient="index")](https://pandas.pydata.org/docs/reference/api/pandas.Series.to_json.html#pandas.Series.to_json).
## Adjusted Predictions ## Adjusted Predictions
@@ -119,11 +116,9 @@ Configuration options:
- `provider`: Electricity price provider id of provider to be used. - `provider`: Electricity price provider id of provider to be used.
- `ElecPriceAkkudoktor`: Retrieves from Akkudoktor.net. - `ElecPriceAkkudoktor`: Retrieves from Akkudoktor.net.
- `ElecPriceEnergyCharts`: Retrieves from Energy-Charts.info.
- `ElecPriceImport`: Imports from a file or JSON string. - `ElecPriceImport`: Imports from a file or JSON string.
- `charges_kwh`: Electricity price charges (€/kWh). - `charges_kwh`: Electricity price charges (€/kWh).
- `vat_rate`: VAT rate factor applied to electricity price when charges are used (default: 1.19).
- `provider_settings.import_file_path`: Path to the file to import electricity price forecast data from. - `provider_settings.import_file_path`: Path to the file to import electricity price forecast data from.
- `provider_settings.import_json`: JSON string, dictionary of electricity price forecast value lists. - `provider_settings.import_json`: JSON string, dictionary of electricity price forecast value lists.
@@ -135,24 +130,6 @@ prices by extrapolating historical price data combined with the most recent actu
from Akkudoktor.net. Electricity price charges given in the `charges_kwh` configuration from Akkudoktor.net. Electricity price charges given in the `charges_kwh` configuration
option are added. option are added.
### ElecPriceEnergyCharts Provider
The `ElecPriceEnergyCharts` provider retrieves day-ahead electricity market prices from
[Energy-Charts.info](https://www.Energy-Charts.info). It supports both short-term and extended forecasting by combining
real-time market data with historical price trends.
- For the next 24 hours, market prices are fetched directly from Energy-Charts.info.
- For periods beyond 24 hours, prices are estimated using extrapolation based on historical data and the latest
available market values.
Charges and VAT
- If `charges_kwh` configuration option is greater than 0, the electricity price is calculated as:
`(market price + charges_kwh) * vat_rate` where `vat_rate` is configurable (default: 1.19 for 19% VAT).
- If `charges_kwh` is set to 0, the electricity price is simply: `market_price` (no VAT applied).
**Note:** For the most accurate forecasts, it is recommended to set the `historic_hours` parameter to 840.
### ElecPriceImport Provider ### ElecPriceImport Provider
The `ElecPriceImport` provider is designed to import electricity prices from a file or a JSON The `ElecPriceImport` provider is designed to import electricity prices from a file or a JSON
@@ -185,7 +162,6 @@ Configuration options:
- `provider`: Load provider id of provider to be used. - `provider`: Load provider id of provider to be used.
- `LoadAkkudoktor`: Retrieves from local database. - `LoadAkkudoktor`: Retrieves from local database.
- `LoadVrm`: Retrieves data from the VRM API by Victron Energy.
- `LoadImport`: Imports from a file or JSON string. - `LoadImport`: Imports from a file or JSON string.
- `provider_settings.loadakkudoktor_year_energy`: Yearly energy consumption (kWh). - `provider_settings.loadakkudoktor_year_energy`: Yearly energy consumption (kWh).
@@ -198,27 +174,6 @@ The `LoadAkkudoktor` provider retrieves generic load data from a local database
align with the annual energy consumption specified in the `loadakkudoktor_year_energy` configuration align with the annual energy consumption specified in the `loadakkudoktor_year_energy` configuration
option. option.
### LoadVrm Provider
The `LoadVrm` provider retrieves load forecast data from the VRM API by Victron Energy.
To receive forecasts, the system data must be configured under Dynamic ESS in the VRM portal.
To query the forecasts, an API token is required, which can also be created in the VRM portal under Preferences.
This token must be stored in the EOS configuration along with the VRM-Installations-ID.
```python
{
"load": {
"provider": "LoadVrm",
"provider_settings": {
"load_vrm_token": "dummy-token",
"load_vrm_idsite": 12345
}
```
The prediction keys for the load forecast data are:
- `load_mean`: Predicted load mean value (W).
### LoadImport Provider ### LoadImport Provider
The `LoadImport` provider is designed to import load forecast data from a file or a JSON The `LoadImport` provider is designed to import load forecast data from a file or a JSON
@@ -257,25 +212,16 @@ Configuration options:
- `provider`: PVForecast provider id of provider to be used. - `provider`: PVForecast provider id of provider to be used.
- `PVForecastAkkudoktor`: Retrieves from Akkudoktor.net. - `PVForecastAkkudoktor`: Retrieves from Akkudoktor.net.
- `PVForecastVrm`: Retrieves data from the VRM API by Victron Energy.
- `PVForecastImport`: Imports from a file or JSON string. - `PVForecastImport`: Imports from a file or JSON string.
- `planes[].surface_tilt`: Tilt angle from horizontal plane. Ignored for two-axis tracking. - `planes[].surface_tilt`: Tilt angle from horizontal plane. Ignored for two-axis tracking.
- `planes[].surface_azimuth`: Orientation (azimuth angle) of the (fixed) plane. - `planes[].surface_azimuth`: Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, west=270).
Clockwise from north (north=0, east=90, south=180, west=270).
- `planes[].userhorizon`: Elevation of horizon in degrees, at equally spaced azimuth clockwise from north. - `planes[].userhorizon`: Elevation of horizon in degrees, at equally spaced azimuth clockwise from north.
- `planes[].peakpower`: Nominal power of PV system in kW. - `planes[].peakpower`: Nominal power of PV system in kW.
- `planes[].pvtechchoice`: PV technology. One of 'crystSi', 'CIS', 'CdTe', 'Unknown'. - `planes[].pvtechchoice`: PV technology. One of 'crystSi', 'CIS', 'CdTe', 'Unknown'.
- `planes[].mountingplace`: Type of mounting for PV system. - `planes[].mountingplace`: Type of mounting for PV system. Options are 'free' for free-standing and 'building' for building-integrated.
Options are 'free' for free-standing and 'building' for building-integrated.
- `planes[].loss`: Sum of PV system losses in percent - `planes[].loss`: Sum of PV system losses in percent
- `planes[].trackingtype`: Type of suntracking. - `planes[].trackingtype`: Type of suntracking. 0=fixed, 1=single horizontal axis aligned north-south, 2=two-axis tracking, 3=vertical axis tracking, 4=single horizontal axis aligned east-west, 5=single inclined axis aligned north-south.
0=fixed,
1=single horizontal axis aligned north-south,
2=two-axis tracking,
3=vertical axis tracking,
4=single horizontal axis aligned east-west,
5=single inclined axis aligned north-south.
- `planes[].optimal_surface_tilt`: Calculate the optimum tilt angle. Ignored for two-axis tracking. - `planes[].optimal_surface_tilt`: Calculate the optimum tilt angle. Ignored for two-axis tracking.
- `planes[].optimalangles`: Calculate the optimum tilt and azimuth angles. Ignored for two-axis tracking. - `planes[].optimalangles`: Calculate the optimum tilt and azimuth angles. Ignored for two-axis tracking.
- `planes[].albedo`: Proportion of the light hitting the ground that it reflects back. - `planes[].albedo`: Proportion of the light hitting the ground that it reflects back.
@@ -287,73 +233,39 @@ Configuration options:
- `provider_settings.import_file_path`: Path to the file to import PV forecast data from. - `provider_settings.import_file_path`: Path to the file to import PV forecast data from.
- `provider_settings.import_json`: JSON string, dictionary of PV forecast value lists. - `provider_settings.import_json`: JSON string, dictionary of PV forecast value lists.
--- ------
Detailed definitions taken from Some of the planes configuration options directly follow the [PVGIS](https://joint-research-centre.ec.europa.eu/photovoltaic-geographical-information-system-pvgis/getting-started-pvgis/pvgis-user-manual_en) nomenclature.
[PVGIS](https://joint-research-centre.ec.europa.eu/photovoltaic-geographical-information-system-pvgis/getting-started-pvgis/pvgis-user-manual_en).
Detailed definitions taken from **PVGIS**:
- `pvtechchoice` - `pvtechchoice`
The performance of PV modules depends on the temperature and on the solar irradiance, but the exact The performance of PV modules depends on the temperature and on the solar irradiance, but the exact dependence varies between different types of PV modules. At the moment we can estimate the losses due to temperature and irradiance effects for the following types of modules: crystalline silicon cells; thin film modules made from CIS or CIGS and thin film modules made from Cadmium Telluride (CdTe).
dependence varies between different types of PV modules. At the moment we can estimate the losses
due to temperature and irradiance effects for the following types of modules: crystalline silicon
cells; thin film modules made from CIS or CIGS and thin film modules made from Cadmium Telluride
(CdTe).
For other technologies (especially various amorphous technologies), this correction cannot be For other technologies (especially various amorphous technologies), this correction cannot be calculated here. If you choose one of the first three options here the calculation of performance will take into account the temperature dependence of the performance of the chosen technology. If you choose the other option (other/unknown), the calculation will assume a loss of 8% of power due to temperature effects (a generic value which has found to be reasonable for temperate climates).
calculated here. If you choose one of the first three options here the calculation of performance
will take into account the temperature dependence of the performance of the chosen technology. If
you choose the other option (other/unknown), the calculation will assume a loss of 8% of power due
to temperature effects (a generic value which has found to be reasonable for temperate climates).
PV power output also depends on the spectrum of the solar radiation. PVGIS can calculate how the PV power output also depends on the spectrum of the solar radiation. PVGIS can calculate how the variations of the spectrum of sunlight affects the overall energy production from a PV system. At the moment this calculation can be done for crystalline silicon and CdTe modules. Note that this calculation is not yet available when using the NSRDB solar radiation database.
variations of the spectrum of sunlight affects the overall energy production from a PV system. At
the moment this calculation can be done for crystalline silicon and CdTe modules. Note that this
calculation is not yet available when using the NSRDB solar radiation database.
- `peakpower` - `peakpower`
This is the power that the manufacturer declares that the PV array can produce under standard test This is the power that the manufacturer declares that the PV array can produce under standard test conditions (STC), which are a constant 1000W of solar irradiation per square meter in the plane of the array, at an array temperature of 25°C. The peak power should be entered in kilowatt-peak (kWp). If you do not know the declared peak power of your modules but instead know the area of the modules and the declared conversion efficiency (in percent), you can calculate the peak power as power = area * efficiency / 100.
conditions (STC), which are a constant 1000W of solar irradiation per square meter in the plane of
the array, at an array temperature of 25°C. The peak power should be entered in kilowatt-peak (kWp).
If you do not know the declared peak power of your modules but instead know the area of the modules
and the declared conversion efficiency (in percent), you can calculate the peak power as
power = area \* efficiency / 100.
Bifacial modules: PVGIS doesn't make specific calculations for bifacial modules at present. Users Bifacial modules: PVGIS doesn't make specific calculations for bifacial modules at present. Users who wish to explore the possible benefits of this technology can input the power value for Bifacial Nameplate Irradiance. This can also be can also be estimated from the front side peak power P_STC value and the bifaciality factor, φ (if reported in the module data sheet) as: P_BNPI = P_STC * (1 + φ * 0.135). NB this bifacial approach is not appropriate for BAPV or BIPV installations or for modules mounting on a N-S axis i.e. facing E-W.
who wish to explore the possible benefits of this technology can input the power value for Bifacial
Nameplate Irradiance. This can also be can also be estimated from the front side peak power P_STC
value and the bifaciality factor, φ (if reported in the module data sheet) as:
P_BNPI = P_STC \* (1 + φ \* 0.135). NB this bifacial approach is not appropriate for BAPV or BIPV
installations or for modules mounting on a N-S axis i.e. facing E-W.
- `loss` - `loss`
The estimated system losses are all the losses in the system, which cause the power actually The estimated system losses are all the losses in the system, which cause the power actually delivered to the electricity grid to be lower than the power produced by the PV modules. There are several causes for this loss, such as losses in cables, power inverters, dirt (sometimes snow) on the modules and so on. Over the years the modules also tend to lose a bit of their power, so the average yearly output over the lifetime of the system will be a few percent lower than the output in the first years.
delivered to the electricity grid to be lower than the power produced by the PV modules. There are
several causes for this loss, such as losses in cables, power inverters, dirt (sometimes snow) on
the modules and so on. Over the years the modules also tend to lose a bit of their power, so the
average yearly output over the lifetime of the system will be a few percent lower than the output
in the first years.
We have given a default value of 14% for the overall losses. If you have a good idea that your value We have given a default value of 14% for the overall losses. If you have a good idea that your value will be different (maybe due to a really high-efficiency inverter) you may reduce this value a little.
will be different (maybe due to a really high-efficiency inverter) you may reduce this value a little.
- `mountingplace` - `mountingplace`
For fixed (non-tracking) systems, the way the modules are mounted will have an influence on the For fixed (non-tracking) systems, the way the modules are mounted will have an influence on the temperature of the module, which in turn affects the efficiency. Experiments have shown that if the movement of air behind the modules is restricted, the modules can get considerably hotter (up to 15°C at 1000W/m2 of sunlight).
temperature of the module, which in turn affects the efficiency. Experiments have shown that if the
movement of air behind the modules is restricted, the modules can get considerably hotter
(up to 15°C at 1000W/m2 of sunlight).
In PVGIS there are two possibilities: free-standing, meaning that the modules are mounted on a rack In PVGIS there are two possibilities: free-standing, meaning that the modules are mounted on a rack with air flowing freely behind the modules; and building- integrated, which means that the modules are completely built into the structure of the wall or roof of a building, with no air movement behind the modules.
with air flowing freely behind the modules; and building- integrated, which means that the modules
are completely built into the structure of the wall or roof of a building, with no air movement
behind the modules.
Some types of mounting are in between these two extremes, for instance if the modules are mounted on Some types of mounting are in between these two extremes, for instance if the modules are mounted on a roof with curved roof tiles, allowing air to move behind the modules. In such cases, the performance will be somewhere between the results of the two calculations that are possible here.
a roof with curved roof tiles, allowing air to move behind the modules. In such cases, the
performance will be somewhere between the results of the two calculations that are possible here.
- `userhorizon` - `userhorizon`
@@ -365,10 +277,9 @@ represent equal angular distance around the horizon. For instance, if you have 3
point is due north, the next is 10 degrees east of north, and so on, until the last point, 10 point is due north, the next is 10 degrees east of north, and so on, until the last point, 10
degrees west of north. degrees west of north.
--- ------
Most of the configuration options are in line with the Most of the planes configuration options are in line with the [PVLib](https://pvlib-python.readthedocs.io/en/stable/_modules/pvlib/iotools/pvgis.html) definition for PVGIS data.
[PVLib](https://pvlib-python.readthedocs.io/en/stable/_modules/pvlib/iotools/pvgis.html) definition for PVGIS data.
Detailed definitions from **PVLib** for PVGIS data. Detailed definitions from **PVLib** for PVGIS data.
@@ -381,7 +292,7 @@ Tilt angle from horizontal plane.
Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180,
west=270). This is offset 180 degrees from the convention used by PVGIS. west=270). This is offset 180 degrees from the convention used by PVGIS.
--- ------
### PVForecastAkkudoktor Provider ### PVForecastAkkudoktor Provider
@@ -396,8 +307,7 @@ The following prediction configuration options of the PV system must be set:
For each plane of the PV system the following configuration options must be set: For each plane of the PV system the following configuration options must be set:
- `pvforecast.planes[].surface_tilt`: Tilt angle from horizontal plane. Ignored for two-axis tracking. - `pvforecast.planes[].surface_tilt`: Tilt angle from horizontal plane. Ignored for two-axis tracking.
- `pvforecast.planes[].surface_azimuth`: Orientation (azimuth angle) of the (fixed) plane. - `pvforecast.planes[].surface_azimuth`: Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, west=270).
Clockwise from north (north=0, east=90, south=180, west=270).
- `pvforecast.planes[].userhorizon`: Elevation of horizon in degrees, at equally spaced azimuth clockwise from north. - `pvforecast.planes[].userhorizon`: Elevation of horizon in degrees, at equally spaced azimuth clockwise from north.
- `pvforecast.planes[].inverter_paco`: AC power rating of the inverter. [W] - `pvforecast.planes[].inverter_paco`: AC power rating of the inverter. [W]
- `pvforecast.planes[].peakpower`: Nominal power of PV system in kW. - `pvforecast.planes[].peakpower`: Nominal power of PV system in kW.
@@ -418,56 +328,34 @@ Example:
"surface_azimuth": -10, "surface_azimuth": -10,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [20, 27, 22, 20], "userhorizon": [20, 27, 22, 20],
"inverter_paco": 10000 "inverter_paco": 10000,
}, },
{ {
"peakpower": 4.8, "peakpower": 4.8,
"surface_azimuth": -90, "surface_azimuth": -90,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [30, 30, 30, 50], "userhorizon": [30, 30, 30, 50],
"inverter_paco": 10000 "inverter_paco": 10000,
}, },
{ {
"peakpower": 1.4, "peakpower": 1.4,
"surface_azimuth": -40, "surface_azimuth": -40,
"surface_tilt": 60, "surface_tilt": 60,
"userhorizon": [60, 30, 0, 30], "userhorizon": [60, 30, 0, 30],
"inverter_paco": 2000 "inverter_paco": 2000,
}, },
{ {
"peakpower": 1.6, "peakpower": 1.6,
"surface_azimuth": 5, "surface_azimuth": 5,
"surface_tilt": 45, "surface_tilt": 45,
"userhorizon": [45, 25, 30, 60], "userhorizon": [45, 25, 30, 60],
"inverter_paco": 1400 "inverter_paco": 1400,
} }
] ]
} }
} }
``` ```
### PVForecastVrm Provider
The `PVForecastVrm` provider retrieves pv power forecast data from the VRM API by Victron Energy.
To receive forecasts, the system data must be configured under Dynamic ESS in the VRM portal.
To query the forecasts, an API token is required, which can also be created in the VRM portal under Preferences.
This token must be stored in the EOS configuration along with the VRM-Installations-ID.
```python
{
"pvforecast": {
"provider": "PVForecastVrm",
"provider_settings": {
"pvforecast_vrm_token": "dummy-token",
"pvforecast_vrm_idsite": 12345
}
}
```
The prediction keys for the PV forecast data are:
- `pvforecast_dc_power`: Total DC power (W).
### PVForecastImport Provider ### PVForecastImport Provider
The `PVForecastImport` provider is designed to import PV forecast data from a file or a JSON The `PVForecastImport` provider is designed to import PV forecast data from a file or a JSON
@@ -476,8 +364,8 @@ becomes available.
The prediction keys for the PV forecast data are: The prediction keys for the PV forecast data are:
- `pvforecast_ac_power`: Total AC power (W). - `pvforecast_ac_power`: Total DC power (W).
- `pvforecast_dc_power`: Total DC power (W). - `pvforecast_dc_power`: Total AC power (W).
The PV forecast data must be provided in one of the formats described in The PV forecast data must be provided in one of the formats described in
<project:#prediction-import-providers>. The data source can be given in the <project:#prediction-import-providers>. The data source can be given in the
@@ -519,8 +407,8 @@ Configuration options:
- `provider`: Load provider id of provider to be used. - `provider`: Load provider id of provider to be used.
- `BrightSky`: Retrieves from [BrightSky](https://api.brightsky.dev). - `BrightSky`: Retrieves from https://api.brightsky.dev.
- `ClearOutside`: Retrieves from [ClearOutside](https://clearoutside.com/forecast). - `ClearOutside`: Retrieves from https://clearoutside.com/forecast.
- `LoadImport`: Imports from a file or JSON string. - `LoadImport`: Imports from a file or JSON string.
- `provider_settings.import_file_path`: Path to the file to import weatherforecast data from. - `provider_settings.import_file_path`: Path to the file to import weatherforecast data from.

View File

@@ -75,53 +75,37 @@ This project uses the `EOS.config.json` file to manage configuration settings.
### Default Configuration ### Default Configuration
A default configuration file `default.config.json` is provided. This file contains all the necessary A default configuration file `default.config.json` is provided. This file contains all the necessary configuration keys with their default values.
configuration keys with their default values.
### Custom Configuration ### Custom Configuration
Users can specify a custom configuration directory by setting the environment variable `EOS_DIR`. Users can specify a custom configuration directory by setting the environment variable `EOS_DIR`.
- If the directory specified by `EOS_DIR` contains an existing `EOS.config.json` file, the - If the directory specified by `EOS_DIR` contains an existing `EOS.config.json` file, the application will use this configuration file.
application will use this configuration file. - If the `EOS.config.json` file does not exist in the specified directory, the `default.config.json` file will be copied to the directory as `EOS.config.json`.
- If the `EOS.config.json` file does not exist in the specified directory, the `default.config.json`
file will be copied to the directory as `EOS.config.json`.
### Configuration Updates ### Configuration Updates
If the configuration keys in the `EOS.config.json` file are missing or different from those in If the configuration keys in the `EOS.config.json` file are missing or different from those in `default.config.json`, they will be automatically updated to match the default settings, ensuring that all required keys are present.
`default.config.json`, they will be automatically updated to match the default settings, ensuring
that all required keys are present.
## Classes and Functionalities ## Classes and Functionalities
This project uses various classes to simulate and optimize the components of an energy system. Each This project uses various classes to simulate and optimize the components of an energy system. Each class represents a specific aspect of the system, as described below:
class represents a specific aspect of the system, as described below:
- `Battery`: Simulates a battery storage system, including capacity, state of charge, and now - `Battery`: Simulates a battery storage system, including capacity, state of charge, and now charge and discharge losses.
charge and discharge losses.
- `PVForecast`: Provides forecast data for photovoltaic generation, based on weather data and - `PVForecast`: Provides forecast data for photovoltaic generation, based on weather data and historical generation data.
historical generation data.
- `Load`: Models the load requirements of a household or business, enabling the prediction of future - `Load`: Models the load requirements of a household or business, enabling the prediction of future energy demand.
energy demand.
- `Heatpump`: Simulates a heat pump, including its energy consumption and efficiency under various - `Heatpump`: Simulates a heat pump, including its energy consumption and efficiency under various operating conditions.
operating conditions.
- `Strompreis`: Provides information on electricity prices, enabling optimization of energy - `Strompreis`: Provides information on electricity prices, enabling optimization of energy consumption and generation based on tariff information.
consumption and generation based on tariff information.
- `EMS`: The Energy Management System (EMS) coordinates the interaction between the various - `EMS`: The Energy Management System (EMS) coordinates the interaction between the various components, performs optimization, and simulates the operation of the entire energy system.
components, performs optimization, and simulates the operation of the entire energy system.
These classes work together to enable a detailed simulation and optimization of the energy system. These classes work together to enable a detailed simulation and optimization of the energy system. For each class, specific parameters and settings can be adjusted to test different scenarios and strategies.
For each class, specific parameters and settings can be adjusted to test different scenarios and
strategies.
### Customization and Extension ### Customization and Extension
Each class is designed to be easily customized and extended to integrate additional functions or Each class is designed to be easily customized and extended to integrate additional functions or improvements. For example, new methods can be added for more accurate modeling of PV system or battery behavior. Developers are invited to modify and extend the system according to their needs.
improvements. For example, new methods can be added for more accurate modeling of PV system or
battery behavior. Developers are invited to modify and extend the system according to their needs.

View File

@@ -8,45 +8,23 @@
```{toctree} ```{toctree}
:maxdepth: 2 :maxdepth: 2
:caption: Overview :caption: 'Contents:'
akkudoktoreos/introduction.md
```
```{toctree}
:maxdepth: 2
:caption: Tutorials
welcome.md
akkudoktoreos/about.md
develop/getting_started.md develop/getting_started.md
```
```{toctree}
:maxdepth: 2
:caption: How-To Guides
develop/CONTRIBUTING.md develop/CONTRIBUTING.md
```
```{toctree}
:maxdepth: 2
:caption: Reference
akkudoktoreos/architecture.md akkudoktoreos/architecture.md
akkudoktoreos/configuration.md akkudoktoreos/configuration.md
akkudoktoreos/optimization.md akkudoktoreos/optimization.md
akkudoktoreos/prediction.md akkudoktoreos/prediction.md
akkudoktoreos/measurement.md akkudoktoreos/measurement.md
akkudoktoreos/integration.md akkudoktoreos/integration.md
akkudoktoreos/logging.md
akkudoktoreos/serverapi.md akkudoktoreos/serverapi.md
akkudoktoreos/api.rst akkudoktoreos/api.rst
``` ```
## Indices and tables # Indices and tables
- {ref}`genindex` - {ref}`genindex`
- {ref}`modindex` - {ref}`modindex`

View File

@@ -1,20 +0,0 @@
{
"plugins": {
"md007": {
"enabled": true,
"code_block_line_length" : 160
},
"md013": {
"enabled": true,
"line_length" : 120
},
"md041": {
"enabled": false
}
},
"extensions": {
"front-matter" : {
"enabled" : true
}
}
}

View File

@@ -1,12 +1,12 @@
% SPDX-License-Identifier: Apache-2.0 % SPDX-License-Identifier: Apache-2.0
# Welcome to the EOS documentation # Welcome to the EOS documentation!
This documentation is continuously written. It is edited via text files in the This documentation is continuously written. It is edited via text files in the
[Markdown/ Markedly Structured Text](https://myst-parser.readthedocs.io/en/latest/index.html) [Markdown/ Markedly Structured Text](https://myst-parser.readthedocs.io/en/latest/index.html)
markup language and then compiled into a static website/ offline document using the open source tool markup language and then compiled into a static website/ offline document using the open source tool
[Sphinx](https://www.sphinx-doc.org) and is available on [Sphinx](https://www.sphinx-doc.org) and will someday land on
[Read the Docs](https://akkudoktor-eos.readthedocs.io/en/latest/). [Read the Docs](https://akkudoktoreos.readthedocs.io/en/latest/index.html).
You can contribute to EOS's documentation by opening You can contribute to EOS's documentation by opening
[GitHub issues](https://github.com/Akkudoktor-EOS/EOS/issues) [GitHub issues](https://github.com/Akkudoktor-EOS/EOS/issues)

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,7 @@ authors = [
description = "This project provides a comprehensive solution for simulating and optimizing an energy system based on renewable energy sources. With a focus on photovoltaic (PV) systems, battery storage (batteries), load management (consumer requirements), heat pumps, electric vehicles, and consideration of electricity price data, this system enables forecasting and optimization of energy flow and costs over a specified period." description = "This project provides a comprehensive solution for simulating and optimizing an energy system based on renewable energy sources. With a focus on photovoltaic (PV) systems, battery storage (batteries), load management (consumer requirements), heat pumps, electric vehicles, and consideration of electricity price data, this system enables forecasting and optimization of energy flow and costs over a specified period."
readme = "README.md" readme = "README.md"
license = {file = "LICENSE"} license = {file = "LICENSE"}
requires-python = ">=3.11" requires-python = ">=3.10"
classifiers = [ classifiers = [
"Development Status :: 3 - Alpha", "Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3", "Programming Language :: Python :: 3",
@@ -43,18 +43,12 @@ profile = "black"
[tool.ruff] [tool.ruff]
line-length = 100 line-length = 100
exclude = [
"tests",
"scripts",
]
output-format = "full"
[tool.ruff.lint] [tool.ruff.lint]
select = [ select = [
"F", # Enable all `Pyflakes` rules. "F", # Enable all `Pyflakes` rules.
"D", # Enable all `pydocstyle` rules, limiting to those that adhere to the "D", # Enable all `pydocstyle` rules, limiting to those that adhere to the
# Google convention via `convention = "google"`, below. # Google convention via `convention = "google"`, below.
"S", # Enable all `flake8-bandit` rules.
] ]
ignore = [ ignore = [
# Prevent errors due to ruff false positives # Prevent errors due to ruff false positives

View File

@@ -1,15 +1,13 @@
-r requirements.txt -r requirements.txt
gitlint==0.19.1 gitpython==3.1.44
GitPython==3.1.45 myst-parser==4.0.0
myst-parser==4.0.1 sphinx==8.1.3
sphinx==8.2.3
sphinx_rtd_theme==3.0.2 sphinx_rtd_theme==3.0.2
sphinx-tabs==3.4.7 sphinx-tabs==3.4.7
pymarkdownlnt==0.9.32 pytest==8.3.4
pytest==8.4.2 pytest-cov==6.0.0
pytest-cov==7.0.0
pytest-xprocess==1.0.2 pytest-xprocess==1.0.2
pre-commit pre-commit
mypy==1.18.2 mypy==1.13.0
types-requests==2.32.4.20250913 types-requests==2.32.0.20241016
pandas-stubs==2.3.2.250827 pandas-stubs==2.2.3.241126

View File

@@ -1,25 +1,24 @@
cachebox==5.0.2 cachebox==4.4.2
numpy==2.3.3 numpy==2.2.2
numpydantic==1.6.11 numpydantic==1.6.7
matplotlib==3.10.6 matplotlib==3.10.0
fastapi[standard]==0.115.14 fastapi[standard]==0.115.7
python-fasthtml==0.12.29 python-fasthtml==0.12.0
MonsterUI==1.0.29 MonsterUI==0.0.29
markdown-it-py==3.0.0 markdown-it-py==3.0.0
mdit-py-plugins==0.5.0 mdit-py-plugins==0.4.2
bokeh==3.8.0 bokeh==3.6.3
uvicorn==0.36.0 uvicorn==0.34.0
scikit-learn==1.7.2 scikit-learn==1.6.1
timezonefinder==7.0.2 timezonefinder==6.5.8
deap==1.4.3 deap==1.4.2
requests==2.32.5 requests==2.32.3
pandas==2.3.2 pandas==2.2.3
pendulum==3.1.0 pendulum==3.0.0
platformdirs==4.4.0 platformdirs==4.3.6
psutil==7.1.0 psutil==6.1.1
pvlib==0.13.1 pvlib==0.11.2
pydantic==2.11.9 pydantic==2.10.6
statsmodels==0.14.5 statsmodels==0.14.4
pydantic-settings==2.11.0 pydantic-settings==2.7.0
linkify-it-py==2.0.3 linkify-it-py==2.0.3
loguru==0.7.3

View File

@@ -4,20 +4,22 @@
import argparse import argparse
import json import json
import os import os
import re
import sys import sys
import textwrap import textwrap
from pathlib import Path from pathlib import Path
from typing import Any, Union from typing import Any, Union
from loguru import logger
from pydantic.fields import ComputedFieldInfo, FieldInfo from pydantic.fields import ComputedFieldInfo, FieldInfo
from pydantic_core import PydanticUndefined from pydantic_core import PydanticUndefined
from akkudoktoreos.config.config import ConfigEOS, GeneralSettings, get_config from akkudoktoreos.config.config import ConfigEOS, GeneralSettings, get_config
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticBaseModel from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.utils.docs import get_model_structure_from_examples from akkudoktoreos.utils.docs import get_model_structure_from_examples
logger = get_logger(__name__)
documented_types: set[PydanticBaseModel] = set() documented_types: set[PydanticBaseModel] = set()
undocumented_types: dict[PydanticBaseModel, tuple[str, list[str]]] = dict() undocumented_types: dict[PydanticBaseModel, tuple[str, list[str]]] = dict()
@@ -143,7 +145,6 @@ def generate_config_table_md(
field_type = field_info.annotation if regular_field else field_info.return_type field_type = field_info.annotation if regular_field else field_info.return_type
default_value = get_default_value(field_info, regular_field) default_value = get_default_value(field_info, regular_field)
description = field_info.description if field_info.description else "-" description = field_info.description if field_info.description else "-"
deprecated = field_info.deprecated if field_info.deprecated else None
read_only = "rw" if regular_field else "ro" read_only = "rw" if regular_field else "ro"
type_name = get_type_name(field_type) type_name = get_type_name(field_type)
@@ -153,11 +154,6 @@ def generate_config_table_md(
env_entry = f"| `{prefix}{config_name}` " env_entry = f"| `{prefix}{config_name}` "
else: else:
env_entry = "| " env_entry = "| "
if deprecated:
if isinstance(deprecated, bool):
description = "Deprecated!"
else:
description = deprecated
table += f"| {field_name} {env_entry}| `{type_name}` | `{read_only}` | `{default_value}` | {description} |\n" table += f"| {field_name} {env_entry}| `{type_name}` | `{read_only}` | `{default_value}` | {description} |\n"
inner_types: dict[PydanticBaseModel, tuple[str, list[str]]] = dict() inner_types: dict[PydanticBaseModel, tuple[str, list[str]]] = dict()
@@ -263,7 +259,7 @@ def generate_config_md(config_eos: ConfigEOS) -> str:
markdown = "# Configuration Table\n\n" markdown = "# Configuration Table\n\n"
# Generate tables for each top level config # Generate tables for each top level config
for field_name, field_info in config_eos.__class__.model_fields.items(): for field_name, field_info in config_eos.model_fields.items():
field_type = field_info.annotation field_type = field_info.annotation
markdown += generate_config_table_md( markdown += generate_config_table_md(
field_type, [field_name], f"EOS_{field_name.upper()}__", True field_type, [field_name], f"EOS_{field_name.upper()}__", True
@@ -283,13 +279,6 @@ def generate_config_md(config_eos: ConfigEOS) -> str:
markdown = markdown.rstrip("\n") markdown = markdown.rstrip("\n")
markdown += "\n" markdown += "\n"
# Assure log path does not leak to documentation
markdown = re.sub(
r'(?<=["\'])/[^"\']*/output/eos\.log(?=["\'])',
'/home/user/.local/share/net.akkudoktoreos.net/output/eos.log',
markdown
)
return markdown return markdown
@@ -309,7 +298,7 @@ def main():
try: try:
config_md = generate_config_md(config_eos) config_md = generate_config_md(config_eos)
if os.name == "nt": if os.name == "nt":
config_md = config_md.replace("\\\\", "/") config_md = config_md.replace("127.0.0.1", "0.0.0.0").replace("\\\\", "/")
if args.output_file: if args.output_file:
# Write to file # Write to file
with open(args.output_file, "w", encoding="utf-8", newline="\n") as f: with open(args.output_file, "w", encoding="utf-8", newline="\n") as f:

View File

@@ -42,9 +42,6 @@ def generate_openapi() -> dict:
general = openapi_spec["components"]["schemas"]["ConfigEOS"]["properties"]["general"]["default"] general = openapi_spec["components"]["schemas"]["ConfigEOS"]["properties"]["general"]["default"]
general["config_file_path"] = "/home/user/.config/net.akkudoktoreos.net/EOS.config.json" general["config_file_path"] = "/home/user/.config/net.akkudoktoreos.net/EOS.config.json"
general["config_folder_path"] = "/home/user/.config/net.akkudoktoreos.net" general["config_folder_path"] = "/home/user/.config/net.akkudoktoreos.net"
# Fix file path for logging settings to not show local/test file path
logging = openapi_spec["components"]["schemas"]["ConfigEOS"]["properties"]["logging"]["default"]
logging["file_path"] = "/home/user/.local/share/net.akkudoktoreos.net/output/eos.log"
return openapi_spec return openapi_spec
@@ -61,6 +58,8 @@ def main():
try: try:
openapi_spec = generate_openapi() openapi_spec = generate_openapi()
openapi_spec_str = json.dumps(openapi_spec, indent=2) openapi_spec_str = json.dumps(openapi_spec, indent=2)
if os.name == "nt":
openapi_spec_str = openapi_spec_str.replace("127.0.0.1", "0.0.0.0")
if args.output_file: if args.output_file:
# Write to file # Write to file
with open(args.output_file, "w", encoding="utf-8", newline="\n") as f: with open(args.output_file, "w", encoding="utf-8", newline="\n") as f:

View File

@@ -286,7 +286,7 @@ def main():
try: try:
openapi_md = generate_openapi_md() openapi_md = generate_openapi_md()
if os.name == "nt": if os.name == "nt":
openapi_md = openapi_md.replace("127.0.0.1", "127.0.0.1") openapi_md = openapi_md.replace("127.0.0.1", "0.0.0.0")
if args.output_file: if args.output_file:
# Write to file # Write to file
with open(args.output_file, "w", encoding="utf-8", newline="\n") as f: with open(args.output_file, "w", encoding="utf-8", newline="\n") as f:

View File

@@ -1 +0,0 @@
# Placeholder for gitlint user rules (see https://jorisroovers.com/gitlint/latest/rules/user_defined_rules/).

View File

@@ -12,12 +12,15 @@ import numpy as np
from akkudoktoreos.config.config import get_config from akkudoktoreos.config.config import get_config
from akkudoktoreos.core.ems import get_ems from akkudoktoreos.core.ems import get_ems
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.optimization.genetic import ( from akkudoktoreos.optimization.genetic import (
OptimizationParameters, OptimizationParameters,
optimization_problem, optimization_problem,
) )
from akkudoktoreos.prediction.prediction import get_prediction from akkudoktoreos.prediction.prediction import get_prediction
get_logger(__name__, logging_level="DEBUG")
def prepare_optimization_real_parameters() -> OptimizationParameters: def prepare_optimization_real_parameters() -> OptimizationParameters:
"""Prepare and return optimization parameters with real world data. """Prepare and return optimization parameters with real world data.

View File

@@ -14,7 +14,6 @@ import shutil
from pathlib import Path from pathlib import Path
from typing import Any, ClassVar, Optional, Type from typing import Any, ClassVar, Optional, Type
from loguru import logger
from platformdirs import user_config_dir, user_data_dir from platformdirs import user_config_dir, user_data_dir
from pydantic import Field, computed_field from pydantic import Field, computed_field
from pydantic_settings import ( from pydantic_settings import (
@@ -30,8 +29,9 @@ from akkudoktoreos.core.cachesettings import CacheCommonSettings
from akkudoktoreos.core.coreabc import SingletonMixin from akkudoktoreos.core.coreabc import SingletonMixin
from akkudoktoreos.core.decorators import classproperty from akkudoktoreos.core.decorators import classproperty
from akkudoktoreos.core.emsettings import EnergyManagementCommonSettings from akkudoktoreos.core.emsettings import EnergyManagementCommonSettings
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.logsettings import LoggingCommonSettings from akkudoktoreos.core.logsettings import LoggingCommonSettings
from akkudoktoreos.core.pydantic import PydanticModelNestedValueMixin, merge_models from akkudoktoreos.core.pydantic import access_nested_value, merge_models
from akkudoktoreos.devices.settings import DevicesCommonSettings from akkudoktoreos.devices.settings import DevicesCommonSettings
from akkudoktoreos.measurement.measurement import MeasurementCommonSettings from akkudoktoreos.measurement.measurement import MeasurementCommonSettings
from akkudoktoreos.optimization.optimization import OptimizationCommonSettings from akkudoktoreos.optimization.optimization import OptimizationCommonSettings
@@ -44,6 +44,8 @@ from akkudoktoreos.server.server import ServerCommonSettings
from akkudoktoreos.utils.datetimeutil import to_timezone from akkudoktoreos.utils.datetimeutil import to_timezone
from akkudoktoreos.utils.utils import UtilsCommonSettings from akkudoktoreos.utils.utils import UtilsCommonSettings
logger = get_logger(__name__)
def get_absolute_path( def get_absolute_path(
basepath: Optional[Path | str], subpath: Optional[Path | str] basepath: Optional[Path | str], subpath: Optional[Path | str]
@@ -78,6 +80,10 @@ class GeneralSettings(SettingsBaseModel):
Properties: Properties:
timezone (Optional[str]): Computed time zone string based on the specified latitude timezone (Optional[str]): Computed time zone string based on the specified latitude
and longitude. and longitude.
Validators:
validate_latitude (float): Ensures `latitude` is within the range -90 to 90.
validate_longitude (float): Ensures `longitude` is within the range -180 to 180.
""" """
_config_folder_path: ClassVar[Optional[Path]] = None _config_folder_path: ClassVar[Optional[Path]] = None
@@ -132,7 +138,7 @@ class GeneralSettings(SettingsBaseModel):
return self._config_file_path return self._config_file_path
class SettingsEOS(BaseSettings, PydanticModelNestedValueMixin): class SettingsEOS(BaseSettings):
"""Settings for all EOS. """Settings for all EOS.
Used by updating the configuration with specific settings only. Used by updating the configuration with specific settings only.
@@ -277,16 +283,6 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
ENCODING: ClassVar[str] = "UTF-8" ENCODING: ClassVar[str] = "UTF-8"
CONFIG_FILE_NAME: ClassVar[str] = "EOS.config.json" CONFIG_FILE_NAME: ClassVar[str] = "EOS.config.json"
def __hash__(self) -> int:
# ConfigEOS is a singleton
return hash("config_eos")
def __eq__(self, other: Any) -> bool:
if not isinstance(other, ConfigEOS):
return False
# ConfigEOS is a singleton
return True
@classmethod @classmethod
def settings_customise_sources( def settings_customise_sources(
cls, cls,
@@ -380,15 +376,6 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
def _setup(self, *args: Any, **kwargs: Any) -> None: def _setup(self, *args: Any, **kwargs: Any) -> None:
"""Re-initialize global settings.""" """Re-initialize global settings."""
# Check for config file content/ version type
config_file, exists = self._get_config_file_path()
if exists:
with config_file.open("r", encoding="utf-8", newline=None) as f_config:
config_txt = f_config.read()
if '"directories": {' in config_txt or '"server_eos_host": ' in config_txt:
error_msg = f"Configuration file '{config_file}' is outdated. Please remove or update manually."
logger.error(error_msg)
raise ValueError(error_msg)
# Assure settings base knows EOS configuration # Assure settings base knows EOS configuration
SettingsBaseModel.config = self SettingsBaseModel.config = self
# (Re-)load settings # (Re-)load settings
@@ -407,9 +394,7 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
ValueError: If the `settings` is not a `SettingsEOS` instance. ValueError: If the `settings` is not a `SettingsEOS` instance.
""" """
if not isinstance(settings, SettingsEOS): if not isinstance(settings, SettingsEOS):
error_msg = f"Settings must be an instance of SettingsEOS: '{settings}'." raise ValueError(f"Settings must be an instance of SettingsEOS: '{settings}'.")
logger.error(error_msg)
raise ValueError(error_msg)
self.merge_settings_from_dict(settings.model_dump(exclude_none=True, exclude_unset=True)) self.merge_settings_from_dict(settings.model_dump(exclude_none=True, exclude_unset=True))
@@ -441,6 +426,32 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
""" """
self._setup() self._setup()
def set_config_value(self, path: str, value: Any) -> None:
"""Set a configuration value based on the provided path.
Supports string paths (with '/' separators) or sequence paths (list/tuple).
Trims leading and trailing '/' from string paths.
Args:
path (str): The path to the configuration key (e.g., "key1/key2/key3" or key1/key2/0).
value (Any]): The value to set.
"""
access_nested_value(self, path, True, value)
def get_config_value(self, path: str) -> Any:
"""Get a configuration value based on the provided path.
Supports string paths (with '/' separators) or sequence paths (list/tuple).
Trims leading and trailing '/' from string paths.
Args:
path (str): The path to the configuration key (e.g., "key1/key2/key3" or key1/key2/0).
Returns:
Any: The retrieved value.
"""
return access_nested_value(self, path, False)
def _create_initial_config_file(self) -> None: def _create_initial_config_file(self) -> None:
if self.general.config_file_path and not self.general.config_file_path.exists(): if self.general.config_file_path and not self.general.config_file_path.exists():
self.general.config_file_path.parent.mkdir(parents=True, exist_ok=True) self.general.config_file_path.parent.mkdir(parents=True, exist_ok=True)
@@ -486,10 +497,10 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
@classmethod @classmethod
def _get_config_file_path(cls) -> tuple[Path, bool]: def _get_config_file_path(cls) -> tuple[Path, bool]:
"""Find a valid configuration file or return the desired path for a new config file. """Finds the a valid configuration file or returns the desired path for a new config file.
Returns: Returns:
tuple[Path, bool]: The path to the configuration file and if there is already a config file there tuple[Path, bool]: The path to the configuration directory and if there is already a config file there
""" """
config_dirs = [] config_dirs = []
env_base_dir = os.getenv(cls.EOS_DIR) env_base_dir = os.getenv(cls.EOS_DIR)
@@ -518,7 +529,7 @@ class ConfigEOS(SingletonMixin, SettingsEOSDefaults):
if not self.general.config_file_path: if not self.general.config_file_path:
raise ValueError("Configuration file path unknown.") raise ValueError("Configuration file path unknown.")
with self.general.config_file_path.open("w", encoding="utf-8", newline="\n") as f_out: with self.general.config_file_path.open("w", encoding="utf-8", newline="\n") as f_out:
json_str = super().model_dump_json(indent=4) json_str = super().model_dump_json()
f_out.write(json_str) f_out.write(json_str)
def update(self) -> None: def update(self) -> None:

View File

@@ -27,14 +27,17 @@ from typing import (
) )
import cachebox import cachebox
from loguru import logger
from pendulum import DateTime, Duration from pendulum import DateTime, Duration
from pydantic import Field from pydantic import Field
from akkudoktoreos.core.coreabc import ConfigMixin, SingletonMixin from akkudoktoreos.core.coreabc import ConfigMixin, SingletonMixin
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticBaseModel from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration
logger = get_logger(__name__)
# --------------------------------- # ---------------------------------
# In-Memory Caching Functionality # In-Memory Caching Functionality
# --------------------------------- # ---------------------------------
@@ -953,7 +956,7 @@ def cache_in_file(
logger.debug("Used cache file for function: " + func.__name__) logger.debug("Used cache file for function: " + func.__name__)
cache_file.seek(0) cache_file.seek(0)
if "b" in mode: if "b" in mode:
result = pickle.load(cache_file) # noqa: S301 result = pickle.load(cache_file)
else: else:
result = cache_file.read() result = cache_file.read()
except Exception as e: except Exception as e:

View File

@@ -13,10 +13,13 @@ Classes:
import threading import threading
from typing import Any, ClassVar, Dict, Optional, Type from typing import Any, ClassVar, Dict, Optional, Type
from loguru import logger
from pendulum import DateTime from pendulum import DateTime
from pydantic import computed_field from pydantic import computed_field
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
config_eos: Any = None config_eos: Any = None
measurement_eos: Any = None measurement_eos: Any = None
prediction_eos: Any = None prediction_eos: Any = None

View File

@@ -19,7 +19,6 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Type, Union, over
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import pendulum import pendulum
from loguru import logger
from numpydantic import NDArray, Shape from numpydantic import NDArray, Shape
from pendulum import DateTime, Duration from pendulum import DateTime, Duration
from pydantic import ( from pydantic import (
@@ -32,6 +31,7 @@ from pydantic import (
) )
from akkudoktoreos.core.coreabc import ConfigMixin, SingletonMixin, StartMixin from akkudoktoreos.core.coreabc import ConfigMixin, SingletonMixin, StartMixin
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import ( from akkudoktoreos.core.pydantic import (
PydanticBaseModel, PydanticBaseModel,
PydanticDateTimeData, PydanticDateTimeData,
@@ -39,6 +39,8 @@ from akkudoktoreos.core.pydantic import (
) )
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration
logger = get_logger(__name__)
class DataBase(ConfigMixin, StartMixin, PydanticBaseModel): class DataBase(ConfigMixin, StartMixin, PydanticBaseModel):
"""Base class for handling generic data. """Base class for handling generic data.
@@ -809,8 +811,7 @@ class DataSequence(DataBase, MutableSequence):
dates, values = self.key_to_lists( dates, values = self.key_to_lists(
key=key, start_datetime=start_datetime, end_datetime=end_datetime, dropna=dropna key=key, start_datetime=start_datetime, end_datetime=end_datetime, dropna=dropna
) )
series = pd.Series(data=values, index=pd.DatetimeIndex(dates), name=key) return pd.Series(data=values, index=pd.DatetimeIndex(dates), name=key)
return series
def key_from_series(self, key: str, series: pd.Series) -> None: def key_from_series(self, key: str, series: pd.Series) -> None:
"""Update the DataSequence from a Pandas Series. """Update the DataSequence from a Pandas Series.

View File

@@ -1,6 +1,10 @@
from collections.abc import Callable from collections.abc import Callable
from typing import Any, Optional from typing import Any, Optional
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
class classproperty: class classproperty:
"""A decorator to define a read-only property at the class level. """A decorator to define a read-only property at the class level.
@@ -30,7 +34,7 @@ class classproperty:
argument and returns a value. argument and returns a value.
Raises: Raises:
RuntimeError: If `fget` is not defined when `__get__` is called. AssertionError: If `fget` is not defined when `__get__` is called.
""" """
def __init__(self, fget: Callable[[Any], Any]) -> None: def __init__(self, fget: Callable[[Any], Any]) -> None:
@@ -39,6 +43,5 @@ class classproperty:
def __get__(self, _: Any, owner_cls: Optional[type[Any]] = None) -> Any: def __get__(self, _: Any, owner_cls: Optional[type[Any]] = None) -> Any:
if owner_cls is None: if owner_cls is None:
return self return self
if self.fget is None: assert self.fget is not None
raise RuntimeError("'fget' not defined when `__get__` is called")
return self.fget(owner_cls) return self.fget(owner_cls)

View File

@@ -1,8 +1,6 @@
import traceback
from typing import Any, ClassVar, Optional from typing import Any, ClassVar, Optional
import numpy as np import numpy as np
from loguru import logger
from numpydantic import NDArray, Shape from numpydantic import NDArray, Shape
from pendulum import DateTime from pendulum import DateTime
from pydantic import ConfigDict, Field, computed_field, field_validator, model_validator from pydantic import ConfigDict, Field, computed_field, field_validator, model_validator
@@ -10,6 +8,7 @@ from typing_extensions import Self
from akkudoktoreos.core.cache import CacheUntilUpdateStore from akkudoktoreos.core.cache import CacheUntilUpdateStore
from akkudoktoreos.core.coreabc import ConfigMixin, PredictionMixin, SingletonMixin from akkudoktoreos.core.coreabc import ConfigMixin, PredictionMixin, SingletonMixin
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import ParametersBaseModel, PydanticBaseModel from akkudoktoreos.core.pydantic import ParametersBaseModel, PydanticBaseModel
from akkudoktoreos.devices.battery import Battery from akkudoktoreos.devices.battery import Battery
from akkudoktoreos.devices.generic import HomeAppliance from akkudoktoreos.devices.generic import HomeAppliance
@@ -17,6 +16,8 @@ from akkudoktoreos.devices.inverter import Inverter
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime
from akkudoktoreos.utils.utils import NumpyEncoder from akkudoktoreos.utils.utils import NumpyEncoder
logger = get_logger(__name__)
class EnergyManagementParameters(ParametersBaseModel): class EnergyManagementParameters(ParametersBaseModel):
pv_prognose_wh: list[float] = Field( pv_prognose_wh: list[float] = Field(
@@ -281,8 +282,6 @@ class EnergyManagement(SingletonMixin, ConfigMixin, PredictionMixin, PydanticBas
self.prediction.update_data(force_enable=force_enable, force_update=force_update) self.prediction.update_data(force_enable=force_enable, force_update=force_update)
# TODO: Create optimisation problem that calls into devices.update_data() for simulations. # TODO: Create optimisation problem that calls into devices.update_data() for simulations.
logger.info("Energy management run (crippled version - prediction update only)")
def manage_energy(self) -> None: def manage_energy(self) -> None:
"""Repeating task for managing energy. """Repeating task for managing energy.
@@ -302,28 +301,26 @@ class EnergyManagement(SingletonMixin, ConfigMixin, PredictionMixin, PydanticBas
Note: The task maintains the interval even if some intervals are missed. Note: The task maintains the interval even if some intervals are missed.
""" """
current_datetime = to_datetime() current_datetime = to_datetime()
interval = self.config.ems.interval # interval maybe changed in between
if EnergyManagement._last_datetime is None: if EnergyManagement._last_datetime is None:
# Never run before # Never run before
try: try:
# Remember energy run datetime.
EnergyManagement._last_datetime = current_datetime
# Try to run a first energy management. May fail due to config incomplete. # Try to run a first energy management. May fail due to config incomplete.
self.run() self.run()
# Remember energy run datetime.
EnergyManagement._last_datetime = current_datetime
except Exception as e: except Exception as e:
trace = "".join(traceback.TracebackException.from_exception(e).format()) message = f"EOS init: {e}"
message = f"EOS init: {e}\n{trace}"
logger.error(message) logger.error(message)
return return
if interval is None or interval == float("nan"): if self.config.ems.interval is None or self.config.ems.interval == float("nan"):
# No Repetition # No Repetition
return return
if ( if (
compare_datetimes(current_datetime, EnergyManagement._last_datetime).time_diff compare_datetimes(current_datetime, self._last_datetime).time_diff
< interval < self.config.ems.interval
): ):
# Wait for next run # Wait for next run
return return
@@ -331,16 +328,15 @@ class EnergyManagement(SingletonMixin, ConfigMixin, PredictionMixin, PydanticBas
try: try:
self.run() self.run()
except Exception as e: except Exception as e:
trace = "".join(traceback.TracebackException.from_exception(e).format()) message = f"EOS run: {e}"
message = f"EOS run: {e}\n{trace}"
logger.error(message) logger.error(message)
# Remember the energy management run - keep on interval even if we missed some intervals # Remember the energy management run - keep on interval even if we missed some intervals
while ( while (
compare_datetimes(current_datetime, EnergyManagement._last_datetime).time_diff compare_datetimes(current_datetime, EnergyManagement._last_datetime).time_diff
>= interval >= self.config.ems.interval
): ):
EnergyManagement._last_datetime = EnergyManagement._last_datetime.add(seconds=interval) EnergyManagement._last_datetime.add(seconds=self.config.ems.interval)
def set_start_hour(self, start_hour: Optional[int] = None) -> None: def set_start_hour(self, start_hour: Optional[int] = None) -> None:
"""Sets start datetime to given hour. """Sets start datetime to given hour.
@@ -394,8 +390,7 @@ class EnergyManagement(SingletonMixin, ConfigMixin, PredictionMixin, PydanticBas
# Fetch objects # Fetch objects
battery = self.battery battery = self.battery
if battery is None: assert battery # to please mypy
raise ValueError(f"battery not set: {battery}")
ev = self.ev ev = self.ev
home_appliance = self.home_appliance home_appliance = self.home_appliance
inverter = self.inverter inverter = self.inverter

View File

@@ -1,3 +1,20 @@
"""Abstract and base classes for logging.""" """Abstract and base classes for logging."""
LOGGING_LEVELS: list[str] = ["TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] import logging
def logging_str_to_level(level_str: str) -> int:
"""Convert log level string to logging level."""
if level_str == "DEBUG":
level = logging.DEBUG
elif level_str == "INFO":
level = logging.INFO
elif level_str == "WARNING":
level = logging.WARNING
elif level_str == "CRITICAL":
level = logging.CRITICAL
elif level_str == "ERROR":
level = logging.ERROR
else:
raise ValueError(f"Unknown loggin level: {level_str}")
return level

View File

@@ -1,241 +1,95 @@
"""Utility for configuring Loguru loggers.""" """Utility functions for handling logging tasks.
Functions:
----------
- get_logger: Creates and configures a logger with console and optional rotating file logging.
Example usage:
--------------
# Logger setup
>>> logger = get_logger(__name__, log_file="app.log", logging_level="DEBUG")
>>> logger.info("Logging initialized.")
Notes:
------
- The logger supports rotating log files to prevent excessive log file size.
"""
import json
import logging as pylogging import logging as pylogging
import os import os
import re from logging.handlers import RotatingFileHandler
import sys from typing import Optional
from pathlib import Path
from types import FrameType
from typing import Any, List, Optional
import pendulum from akkudoktoreos.core.logabc import logging_str_to_level
from loguru import logger
from akkudoktoreos.core.logabc import LOGGING_LEVELS
class InterceptHandler(pylogging.Handler): def get_logger(
"""A logging handler that redirects standard Python logging messages to Loguru. name: str,
log_file: Optional[str] = None,
logging_level: Optional[str] = None,
max_bytes: int = 5000000,
backup_count: int = 5,
) -> pylogging.Logger:
"""Creates and configures a logger with a given name.
This handler ensures consistency between the `logging` module and Loguru by intercepting The logger supports logging to both the console and an optional log file. File logging is
logs sent to the standard logging system and re-emitting them through Loguru with proper handled by a rotating file handler to prevent excessive log file size.
formatting and context (including exception info and call depth).
Attributes:
loglevel_mapping (dict): Mapping from standard logging levels to Loguru level names.
"""
loglevel_mapping: dict[int, str] = {
50: "CRITICAL",
40: "ERROR",
30: "WARNING",
20: "INFO",
10: "DEBUG",
5: "TRACE",
0: "NOTSET",
}
def emit(self, record: pylogging.LogRecord) -> None:
"""Emits a logging record by forwarding it to Loguru with preserved metadata.
Args:
record (logging.LogRecord): A record object containing log message and metadata.
"""
try:
level = logger.level(record.levelname).name
except AttributeError:
level = self.loglevel_mapping.get(record.levelno, "INFO")
frame: Optional[FrameType] = pylogging.currentframe()
depth: int = 2
while frame and frame.f_code.co_filename == pylogging.__file__:
frame = frame.f_back
depth += 1
log = logger.bind(request_id="app")
log.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
console_handler_id = None
file_handler_id = None
def track_logging_config(config_eos: Any, path: str, old_value: Any, value: Any) -> None:
"""Track logging config changes."""
global console_handler_id, file_handler_id
if not path.startswith("logging"):
raise ValueError(f"Logging shall not track '{path}'")
if not config_eos.logging.console_level:
# No value given - check environment value - may also be None
config_eos.logging.console_level = os.getenv("EOS_LOGGING__LEVEL")
if not config_eos.logging.file_level:
# No value given - check environment value - may also be None
config_eos.logging.file_level = os.getenv("EOS_LOGGING__LEVEL")
# Remove handlers
if console_handler_id:
try:
logger.remove(console_handler_id)
except Exception as e:
logger.debug("Exception on logger.remove: {}", e, exc_info=True)
console_handler_id = None
if file_handler_id:
try:
logger.remove(file_handler_id)
except Exception as e:
logger.debug("Exception on logger.remove: {}", e, exc_info=True)
file_handler_id = None
# Create handlers with new configuration
# Always add console handler
if config_eos.logging.console_level not in LOGGING_LEVELS:
logger.error(
f"Invalid console log level '{config_eos.logging.console_level} - forced to INFO'."
)
config_eos.logging.console_level = "INFO"
console_handler_id = logger.add(
sys.stderr,
enqueue=True,
backtrace=True,
level=config_eos.logging.console_level,
# format=_console_format
)
# Add file handler
if config_eos.logging.file_level and config_eos.logging.file_path:
if config_eos.logging.file_level not in LOGGING_LEVELS:
logger.error(
f"Invalid file log level '{config_eos.logging.console_level}' - forced to INFO."
)
config_eos.logging.file_level = "INFO"
file_handler_id = logger.add(
sink=config_eos.logging.file_path,
rotation="100 MB",
retention="3 days",
enqueue=True,
backtrace=True,
level=config_eos.logging.file_level,
serialize=True, # JSON dict formatting
# format=_file_format
)
# Redirect standard logging to Loguru
pylogging.basicConfig(handlers=[InterceptHandler()], level=0)
# Redirect uvicorn and fastapi logging to Loguru
pylogging.getLogger("uvicorn.access").handlers = [InterceptHandler()]
for pylogger_name in ["uvicorn", "uvicorn.error", "fastapi"]:
pylogger = pylogging.getLogger(pylogger_name)
pylogger.handlers = [InterceptHandler()]
pylogger.propagate = False
logger.info(
f"Logger reconfigured - console: {config_eos.logging.console_level}, file: {config_eos.logging.file_level}."
)
def read_file_log(
log_path: Path,
limit: int = 100,
level: Optional[str] = None,
contains: Optional[str] = None,
regex: Optional[str] = None,
from_time: Optional[str] = None,
to_time: Optional[str] = None,
tail: bool = False,
) -> List[dict]:
"""Read and filter structured log entries from a JSON-formatted log file.
Args: Args:
log_path (Path): Path to the JSON-formatted log file. name (str): The name of the logger, typically `__name__` from the calling module.
limit (int, optional): Maximum number of log entries to return. Defaults to 100. log_file (Optional[str]): Path to the log file for file logging. If None, no file logging is done.
level (Optional[str], optional): Filter logs by log level (e.g., "INFO", "ERROR"). Defaults to None. logging_level (Optional[str]): Logging level (e.g., "INFO", "DEBUG"). Defaults to "INFO".
contains (Optional[str], optional): Filter logs that contain this substring in their message. Case-insensitive. Defaults to None. max_bytes (int): Maximum size in bytes for log file before rotation. Defaults to 5 MB.
regex (Optional[str], optional): Filter logs whose message matches this regular expression. Defaults to None. backup_count (int): Number of backup log files to keep. Defaults to 5.
from_time (Optional[str], optional): ISO 8601 datetime string to filter logs not earlier than this time. Defaults to None.
to_time (Optional[str], optional): ISO 8601 datetime string to filter logs not later than this time. Defaults to None.
tail (bool, optional): If True, read the last lines of the file (like `tail -n`). Defaults to False.
Returns: Returns:
List[dict]: A list of filtered log entries as dictionaries. logging.Logger: Configured logger instance.
Raises: Example:
FileNotFoundError: If the log file does not exist. logger = get_logger(__name__, log_file="app.log", logging_level="DEBUG")
ValueError: If the datetime strings are invalid or improperly formatted. logger.info("Application started")
Exception: For other unforeseen I/O or parsing errors.
""" """
if not log_path.exists(): # Create a logger with the specified name
raise FileNotFoundError("Log file not found") logger = pylogging.getLogger(name)
logger.propagate = True
# This is already supported by pydantic-settings in LoggingCommonSettings, however in case
# loading the config itself fails and to set the level before we load the config, we set it here manually.
if logging_level is None and (env_level := os.getenv("EOS_LOGGING__LEVEL")) is not None:
logging_level = env_level
if logging_level is not None:
level = logging_str_to_level(logging_level)
logger.setLevel(level)
try: # The log message format
from_dt = pendulum.parse(from_time) if from_time else None formatter = pylogging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
to_dt = pendulum.parse(to_time) if to_time else None
except Exception as e:
raise ValueError(f"Invalid date/time format: {e}")
regex_pattern = re.compile(regex) if regex else None # Prevent loggers from being added multiple times
# There may already be a logger from pytest
if not logger.handlers:
# Create a console handler with a standard output stream
console_handler = pylogging.StreamHandler()
if logging_level is not None:
console_handler.setLevel(level)
console_handler.setFormatter(formatter)
def matches_filters(log: dict) -> bool: # Add the console handler to the logger
if level and log.get("level", {}).get("name") != level.upper(): logger.addHandler(console_handler)
return False
if contains and contains.lower() not in log.get("message", "").lower():
return False
if regex_pattern and not regex_pattern.search(log.get("message", "")):
return False
if from_dt or to_dt:
try:
log_time = pendulum.parse(log["time"])
except Exception:
return False
if from_dt and log_time < from_dt:
return False
if to_dt and log_time > to_dt:
return False
return True
matched_logs = [] if log_file and len(logger.handlers) < 2: # We assume a console logger to be the first logger
lines: list[str] = [] # If a log file path is specified, create a rotating file handler
if tail: # Ensure the log directory exists
with log_path.open("rb") as f: log_dir = os.path.dirname(log_file)
f.seek(0, 2) if log_dir and not os.path.exists(log_dir):
end = f.tell() os.makedirs(log_dir)
buffer = bytearray()
pointer = end
while pointer > 0 and len(lines) < limit * 5: # Create a rotating file handler
pointer -= 1 file_handler = RotatingFileHandler(log_file, maxBytes=max_bytes, backupCount=backup_count)
f.seek(pointer) if logging_level is not None:
byte = f.read(1) file_handler.setLevel(level)
if byte == b"\n": file_handler.setFormatter(formatter)
if buffer:
line = buffer[::-1].decode("utf-8", errors="ignore")
lines.append(line)
buffer.clear()
else:
buffer.append(byte[0])
if buffer:
line = buffer[::-1].decode("utf-8", errors="ignore")
lines.append(line)
lines = lines[::-1]
else:
with log_path.open("r", encoding="utf-8", newline=None) as f_txt:
lines = f_txt.readlines()
for line in lines: # Add the file handler to the logger
if not line.strip(): logger.addHandler(file_handler)
continue
try:
log = json.loads(line)
except json.JSONDecodeError:
continue
if matches_filters(log):
matched_logs.append(log)
if len(matched_logs) >= limit:
break
return matched_logs return logger

View File

@@ -3,13 +3,13 @@
Kept in an extra module to avoid cyclic dependencies on package import. Kept in an extra module to avoid cyclic dependencies on package import.
""" """
from pathlib import Path import logging
from typing import Optional from typing import Optional
from pydantic import Field, computed_field, field_validator from pydantic import Field, computed_field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logabc import LOGGING_LEVELS from akkudoktoreos.core.logabc import logging_str_to_level
class LoggingCommonSettings(SettingsBaseModel): class LoggingCommonSettings(SettingsBaseModel):
@@ -17,47 +17,27 @@ class LoggingCommonSettings(SettingsBaseModel):
level: Optional[str] = Field( level: Optional[str] = Field(
default=None, default=None,
deprecated="This is deprecated. Use console_level and file_level instead.", description="EOS default logging level.",
examples=["INFO", "DEBUG", "WARNING", "ERROR", "CRITICAL"],
) )
console_level: Optional[str] = Field(
default=None,
description="Logging level when logging to console.",
examples=LOGGING_LEVELS,
)
file_level: Optional[str] = Field(
default=None,
description="Logging level when logging to file.",
examples=LOGGING_LEVELS,
)
@computed_field # type: ignore[prop-decorator]
@property
def file_path(self) -> Optional[Path]:
"""Computed log file path based on data output path."""
try:
path = SettingsBaseModel.config.general.data_output_path / "eos.log"
except:
# Config may not be fully set up
path = None
return path
# Validators # Validators
@field_validator("console_level", "file_level", mode="after") @field_validator("level", mode="after")
@classmethod @classmethod
def validate_level(cls, value: Optional[str]) -> Optional[str]: def set_default_logging_level(cls, value: Optional[str]) -> Optional[str]:
"""Validate logging level string.""" if isinstance(value, str) and value.upper() == "NONE":
value = None
if value is None: if value is None:
# Nothing to set
return None return None
if isinstance(value, str): level = logging_str_to_level(value)
level = value.upper() logging.getLogger().setLevel(level)
if level == "NONE":
return None
if level not in LOGGING_LEVELS:
raise ValueError(f"Logging level {value} not supported")
value = level
else:
raise TypeError(f"Invalid {type(value)} of logging level {value}")
return value return value
# Computed fields
@computed_field # type: ignore[prop-decorator]
@property
def root_level(self) -> str:
"""Root logger logging level."""
level = logging.getLogger().getEffectiveLevel()
level_name = logging.getLevelName(level)
return level_name

View File

@@ -12,35 +12,20 @@ Key Features:
pandas DataFrames and Series with datetime indexes. pandas DataFrames and Series with datetime indexes.
""" """
import inspect
import json import json
import re import re
import uuid
import weakref
from copy import deepcopy from copy import deepcopy
from typing import ( from typing import Any, Dict, List, Optional, Type, Union
Any,
Callable,
Dict,
List,
Optional,
Type,
Union,
get_args,
get_origin,
)
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
import pandas as pd import pandas as pd
import pendulum import pendulum
from loguru import logger
from pandas.api.types import is_datetime64_any_dtype from pandas.api.types import is_datetime64_any_dtype
from pydantic import ( from pydantic import (
AwareDatetime, AwareDatetime,
BaseModel, BaseModel,
ConfigDict, ConfigDict,
Field, Field,
PrivateAttr,
RootModel, RootModel,
TypeAdapter, TypeAdapter,
ValidationError, ValidationError,
@@ -50,10 +35,6 @@ from pydantic import (
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration
# Global weakref dictionary to hold external state per model instance
# Used as a workaround for PrivateAttr not working in e.g. Mixin Classes
_model_private_state: "weakref.WeakKeyDictionary[Union[PydanticBaseModel, PydanticModelNestedValueMixin], Dict[str, Any]]" = weakref.WeakKeyDictionary()
def merge_models(source: BaseModel, update_dict: dict[str, Any]) -> dict[str, Any]: def merge_models(source: BaseModel, update_dict: dict[str, Any]) -> dict[str, Any]:
def deep_update(source_dict: dict[str, Any], update_dict: dict[str, Any]) -> dict[str, Any]: def deep_update(source_dict: dict[str, Any], update_dict: dict[str, Any]) -> dict[str, Any]:
@@ -70,6 +51,70 @@ def merge_models(source: BaseModel, update_dict: dict[str, Any]) -> dict[str, An
return merged_dict return merged_dict
def access_nested_value(
model: BaseModel, path: str, setter: bool, value: Optional[Any] = None
) -> Any:
"""Get or set a nested model value based on the provided path.
Supports string paths (with '/' separators) or sequence paths (list/tuple).
Trims leading and trailing '/' from string paths.
Args:
model (BaseModel): The model object for partial assignment.
path (str): The path to the model key (e.g., "key1/key2/key3" or key1/key2/0).
setter (bool): True to set value at path, False to return value at path.
value (Optional[Any]): The value to set.
Returns:
Any: The retrieved value if acting as a getter, or None if setting a value.
"""
path_elements = path.strip("/").split("/")
cfg: Any = model
parent: BaseModel = model
model_key: str = ""
for i, key in enumerate(path_elements):
is_final_key = i == len(path_elements) - 1
if isinstance(cfg, list):
try:
idx = int(key)
if is_final_key:
if not setter: # Getter
return cfg[idx]
else: # Setter
new_list = list(cfg)
new_list[idx] = value
# Trigger validation
setattr(parent, model_key, new_list)
else:
cfg = cfg[idx]
except ValidationError as e:
raise ValueError(f"Error updating model: {e}") from e
except (ValueError, IndexError) as e:
raise IndexError(f"Invalid list index at {path}: {key}") from e
elif isinstance(cfg, BaseModel):
parent = cfg
model_key = key
if is_final_key:
if not setter: # Getter
return getattr(cfg, key)
else: # Setter
try:
# Verification also if nested value is provided opposed to just setattr
# Will merge partial assignment
cfg = cfg.__pydantic_validator__.validate_assignment(cfg, key, value)
except Exception as e:
raise ValueError(f"Error updating model: {e}") from e
else:
cfg = getattr(cfg, key)
else:
raise KeyError(f"Key '{key}' not found in model.")
class PydanticTypeAdapterDateTime(TypeAdapter[pendulum.DateTime]): class PydanticTypeAdapterDateTime(TypeAdapter[pendulum.DateTime]):
"""Custom type adapter for Pendulum DateTime fields.""" """Custom type adapter for Pendulum DateTime fields."""
@@ -101,538 +146,11 @@ class PydanticTypeAdapterDateTime(TypeAdapter[pendulum.DateTime]):
return bool(re.match(iso8601_pattern, value)) return bool(re.match(iso8601_pattern, value))
class PydanticModelNestedValueMixin: class PydanticBaseModel(BaseModel):
"""A mixin providing methods to get, set and track nested values within a Pydantic model. """Base model class with automatic serialization and deserialization of `pendulum.DateTime` fields.
The methods use a '/'-separated path to denote the nested values. This model serializes pendulum.DateTime objects to ISO 8601 strings and
Supports handling `Optional`, `List`, and `Dict` types, ensuring correct initialization of deserializes ISO 8601 strings to pendulum.DateTime objects.
missing attributes.
Example:
class Address(PydanticBaseModel):
city: str
class User(PydanticBaseModel):
name: str
address: Address
def on_city_change(old, new, path):
print(f"{path}: {old} -> {new}")
user = User(name="Alice", address=Address(city="NY"))
user.track_nested_value("address/city", on_city_change)
user.set_nested_value("address/city", "LA") # triggers callback
"""
def track_nested_value(self, path: str, callback: Callable[[Any, str, Any, Any], None]) -> None:
"""Register a callback for a specific path (or subtree).
Callback triggers if set path is equal or deeper.
Args:
path (str): '/'-separated path to track.
callback (callable): Function called as callback(model_instance, set_path, old_value, new_value).
"""
try:
self._validate_path_structure(path)
pass
except:
raise ValueError(f"Path '{path}' is invalid")
path = path.strip("/")
# Use private data workaround
# Should be:
# _nested_value_callbacks: dict[str, list[Callable[[str, Any, Any], None]]]
# = PrivateAttr(default_factory=dict)
nested_value_callbacks = get_private_attr(self, "nested_value_callbacks", dict())
if path not in nested_value_callbacks:
nested_value_callbacks[path] = []
nested_value_callbacks[path].append(callback)
set_private_attr(self, "nested_value_callbacks", nested_value_callbacks)
logger.debug("Nested value callbacks {}", nested_value_callbacks)
def _validate_path_structure(self, path: str) -> None:
"""Validate that a '/'-separated path is structurally valid for this model.
Checks that each segment of the path corresponds to a field or index in the model's type structure,
without requiring that all intermediate values are currently initialized. This method is intended
to ensure that the path could be valid for nested access or assignment, according to the model's
class definition.
Args:
path (str): The '/'-separated attribute/index path to validate (e.g., "address/city" or "items/0/value").
Raises:
ValueError: If any segment of the path does not correspond to a valid field in the model,
or an invalid transition is made (such as an attribute on a non-model).
Example:
class Address(PydanticBaseModel):
city: str
class User(PydanticBaseModel):
name: str
address: Address
user = User(name="Alice", address=Address(city="NY"))
user._validate_path_structure("address/city") # OK
user._validate_path_structure("address/zipcode") # Raises ValueError
"""
path_elements = path.strip("/").split("/")
# The model we are currently working on
model: Any = self
# The model we get the type information from. It is a pydantic BaseModel
parent: BaseModel = model
# The field that provides type information for the current key
# Fields may have nested types that translates to a sequence of keys, not just one
# - my_field: Optional[list[OtherModel]] -> e.g. "myfield/0" for index 0
# parent_key = ["myfield",] ... ["myfield", "0"]
# parent_key_types = [list, OtherModel]
parent_key: list[str] = []
parent_key_types: list = []
for i, key in enumerate(path_elements):
is_final_key = i == len(path_elements) - 1
# Add current key to parent key to enable nested type tracking
parent_key.append(key)
# Get next value
next_value = None
if isinstance(model, BaseModel):
# Track parent and key for possible assignment later
parent = model
parent_key = [
key,
]
parent_key_types = self._get_key_types(model.__class__, key)
# If this is the final key, set the value
if is_final_key:
return
# Attempt to access the next attribute, handling None values
next_value = getattr(model, key, None)
# Handle missing values (initialize dict/list/model if necessary)
if next_value is None:
next_type = parent_key_types[len(parent_key) - 1]
next_value = self._initialize_value(next_type)
elif isinstance(model, list):
# Handle lists
try:
idx = int(key)
except Exception as e:
raise IndexError(
f"Invalid list index '{key}' at '{path}': key = '{key}'; parent = '{parent}', parent_key = '{parent_key}'; model = '{model}'; {e}"
)
# Get next type from parent key type information
next_type = parent_key_types[len(parent_key) - 1]
if len(model) > idx:
next_value = model[idx]
else:
return
if is_final_key:
return
elif isinstance(model, dict):
# Handle dictionaries (auto-create missing keys)
# Get next type from parent key type information
next_type = parent_key_types[len(parent_key) - 1]
if is_final_key:
return
if key not in model:
return
else:
next_value = model[key]
else:
raise KeyError(f"Key '{key}' not found in model.")
# Move deeper
model = next_value
def get_nested_value(self, path: str) -> Any:
"""Retrieve a nested value from the model using a '/'-separated path.
Supports accessing nested attributes and list indices.
Args:
path (str): A '/'-separated path to the nested attribute (e.g., "key1/key2/0").
Returns:
Any: The retrieved value.
Raises:
KeyError: If a key is not found in the model.
IndexError: If a list index is out of bounds or invalid.
Example:
```python
class Address(PydanticBaseModel):
city: str
class User(PydanticBaseModel):
name: str
address: Address
user = User(name="Alice", address=Address(city="New York"))
city = user.get_nested_value("address/city")
print(city) # Output: "New York"
```
"""
path_elements = path.strip("/").split("/")
model: Any = self
for key in path_elements:
if isinstance(model, list):
try:
model = model[int(key)]
except (ValueError, IndexError) as e:
raise IndexError(f"Invalid list index at '{path}': {key}; {e}")
elif isinstance(model, dict):
try:
model = model[key]
except Exception as e:
raise KeyError(f"Invalid dict key at '{path}': {key}; {e}")
elif isinstance(model, BaseModel):
model = getattr(model, key)
else:
raise KeyError(f"Key '{key}' not found in model.")
return model
def set_nested_value(self, path: str, value: Any) -> None:
"""Set a nested value in the model using a '/'-separated path.
Supports modifying nested attributes and list indices while preserving Pydantic validation.
Automatically initializes missing `Optional`, `Union`, `dict`, and `list` fields if necessary.
If a missing field cannot be initialized, raises an exception.
Triggers the callbacks registered by track_nested_value().
Args:
path (str): A '/'-separated path to the nested attribute (e.g., "key1/key2/0").
value (Any): The new value to set.
Raises:
KeyError: If a key is not found in the model.
IndexError: If a list index is out of bounds or invalid.
ValueError: If a validation error occurs.
TypeError: If a missing field cannot be initialized.
Example:
```python
class Address(PydanticBaseModel):
city: Optional[str]
class User(PydanticBaseModel):
name: str
address: Optional[Address]
settings: Optional[Dict[str, Any]]
user = User(name="Alice", address=None, settings=None)
user.set_nested_value("address/city", "Los Angeles")
user.set_nested_value("settings/theme", "dark")
print(user.address.city) # Output: "Los Angeles"
print(user.settings) # Output: {'theme': 'dark'}
```
"""
path = path.strip("/")
# Store old value (if possible)
try:
old_value = self.get_nested_value(path)
except Exception as e:
# We can not get the old value
# raise ValueError(f"Can not get old (current) value of '{path}': {e}") from e
old_value = None
# Proceed with core logic
self._set_nested_value(path, value)
# Trigger all callbacks whose path is a prefix of set path
triggered = set()
nested_value_callbacks = get_private_attr(self, "nested_value_callbacks", dict())
for cb_path, callbacks in nested_value_callbacks.items():
# Match: cb_path == path, or cb_path is a prefix (parent) of path
pass
if path == cb_path or path.startswith(cb_path + "/"):
for cb in callbacks:
# Prevent duplicate calls
if (cb_path, id(cb)) not in triggered:
cb(self, path, old_value, value)
triggered.add((cb_path, id(cb)))
def _set_nested_value(self, path: str, value: Any) -> None:
"""Set a nested value core logic.
Args:
path (str): A '/'-separated path to the nested attribute (e.g., "key1/key2/0").
value (Any): The new value to set.
Raises:
KeyError: If a key is not found in the model.
IndexError: If a list index is out of bounds or invalid.
ValueError: If a validation error occurs.
TypeError: If a missing field cannot be initialized.
"""
path_elements = path.strip("/").split("/")
# The model we are currently working on
model: Any = self
# The model we get the type information from. It is a pydantic BaseModel
parent: BaseModel = model
# The field that provides type information for the current key
# Fields may have nested types that translates to a sequence of keys, not just one
# - my_field: Optional[list[OtherModel]] -> e.g. "myfield/0" for index 0
# parent_key = ["myfield",] ... ["myfield", "0"]
# parent_key_types = [list, OtherModel]
parent_key: list[str] = []
parent_key_types: list = []
for i, key in enumerate(path_elements):
is_final_key = i == len(path_elements) - 1
# Add current key to parent key to enable nested type tracking
parent_key.append(key)
# Get next value
next_value = None
if isinstance(model, BaseModel):
# Track parent and key for possible assignment later
parent = model
parent_key = [
key,
]
parent_key_types = self._get_key_types(model.__class__, key)
# If this is the final key, set the value
if is_final_key:
try:
model.__pydantic_validator__.validate_assignment(model, key, value)
except ValidationError as e:
raise ValueError(f"Error updating model: {e}") from e
return
# Attempt to access the next attribute, handling None values
next_value = getattr(model, key, None)
# Handle missing values (initialize dict/list/model if necessary)
if next_value is None:
next_type = parent_key_types[len(parent_key) - 1]
next_value = self._initialize_value(next_type)
if next_value is None:
raise TypeError(
f"Unable to initialize missing value for key '{key}' in path '{path}' with type {next_type} of {parent_key}:{parent_key_types}."
)
setattr(parent, key, next_value)
# pydantic may copy on validation assignment - reread to get the copied model
next_value = getattr(model, key, None)
elif isinstance(model, list):
# Handle lists (ensure index exists and modify safely)
try:
idx = int(key)
except Exception as e:
raise IndexError(
f"Invalid list index '{key}' at '{path}': key = '{key}'; parent = '{parent}', parent_key = '{parent_key}'; model = '{model}'; {e}"
)
# Get next type from parent key type information
next_type = parent_key_types[len(parent_key) - 1]
if len(model) > idx:
next_value = model[idx]
else:
# Extend the list with default values if index is out of range
while len(model) <= idx:
next_value = self._initialize_value(next_type)
if next_value is None:
raise TypeError(
f"Unable to initialize missing value for key '{key}' in path '{path}' with type {next_type} of {parent_key}:{parent_key_types}."
)
model.append(next_value)
if is_final_key:
if (
(isinstance(next_type, type) and not isinstance(value, next_type))
or (next_type is dict and not isinstance(value, dict))
or (next_type is list and not isinstance(value, list))
):
raise TypeError(
f"Expected type {next_type} for key '{key}' in path '{path}', but got {type(value)}: {value}"
)
model[idx] = value
return
elif isinstance(model, dict):
# Handle dictionaries (auto-create missing keys)
# Get next type from parent key type information
next_type = parent_key_types[len(parent_key) - 1]
if is_final_key:
if (
(isinstance(next_type, type) and not isinstance(value, next_type))
or (next_type is dict and not isinstance(value, dict))
or (next_type is list and not isinstance(value, list))
):
raise TypeError(
f"Expected type {next_type} for key '{key}' in path '{path}', but got {type(value)}: {value}"
)
model[key] = value
return
if key not in model:
next_value = self._initialize_value(next_type)
if next_value is None:
raise TypeError(
f"Unable to initialize missing value for key '{key}' in path '{path}' with type {next_type} of {parent_key}:{parent_key_types}."
)
model[key] = next_value
else:
next_value = model[key]
else:
raise KeyError(f"Key '{key}' not found in model.")
# Move deeper
model = next_value
@staticmethod
def _get_key_types(model: Type[BaseModel], key: str) -> List[Union[Type[Any], list, dict]]:
"""Returns a list of nested types for a given Pydantic model key.
- Skips `Optional` and `Union`, using only the first non-None type.
- Skips dictionary keys and only adds value types.
- Keeps `list` and `dict` as origins.
Args:
model (Type[BaseModel]): The Pydantic model class to inspect.
key (str): The attribute name in the model.
Returns:
List[Union[Type[Any], list, dict]]: A list of extracted types, preserving `list` and `dict` origins.
Raises:
TypeError: If the key does not exist or lacks a valid type annotation.
"""
if not inspect.isclass(model):
raise TypeError(f"Model '{model}' is not of class type.")
if key not in model.model_fields:
raise TypeError(f"Field '{key}' does not exist in model '{model.__name__}'.")
field_annotation = model.model_fields[key].annotation
if not field_annotation:
raise TypeError(
f"Missing type annotation for field '{key}' in model '{model.__name__}'."
)
nested_types: list[Union[Type[Any], list, dict]] = []
queue: list[Any] = [field_annotation]
while queue:
annotation = queue.pop(0)
origin = get_origin(annotation)
args = get_args(annotation)
# Handle Union (Optional[X] is treated as Union[X, None])
if origin is Union:
queue.extend(arg for arg in args if arg is not type(None))
continue
# Handle lists and dictionaries
if origin is list:
nested_types.append(list)
if args:
queue.append(args[0]) # Extract value type for list[T]
continue
if origin is dict:
nested_types.append(dict)
if len(args) == 2:
queue.append(args[1]) # Extract only the value type for dict[K, V]
continue
# If it's a BaseModel, add it to the list
if isinstance(annotation, type) and issubclass(annotation, BaseModel):
nested_types.append(annotation)
continue
# Otherwise, it's a standard type (e.g., str, int, bool, float, etc.)
nested_types.append(annotation)
return nested_types
@staticmethod
def _initialize_value(type_hint: Type[Any] | None | list[Any] | dict[Any, Any]) -> Any:
"""Initialize a missing value based on the provided type hint.
Args:
type_hint (Type[Any] | None | list[Any] | dict[Any, Any]): The type hint that determines
how the missing value should be initialized.
Returns:
Any: An instance of the expected type (e.g., list, dict, or Pydantic model), or `None`
if initialization is not possible.
Raises:
TypeError: If instantiation fails.
Example:
- For `list[str]`, returns `[]`
- For `dict[str, Any]`, returns `{}`
- For `Address` (a Pydantic model), returns a new `Address()` instance.
"""
if type_hint is None:
return None
# Handle direct instances of list or dict
if isinstance(type_hint, list):
return []
if isinstance(type_hint, dict):
return {}
origin = get_origin(type_hint)
# Handle generic list and dictionary
if origin is list:
return []
if origin is dict:
return {}
# Handle Pydantic models
if isinstance(type_hint, type) and issubclass(type_hint, BaseModel):
try:
return type_hint.model_construct()
except Exception as e:
raise TypeError(f"Failed to initialize model '{type_hint.__name__}': {e}")
# Handle standard built-in types (int, float, str, bool, etc.)
if isinstance(type_hint, type):
try:
return type_hint()
except Exception as e:
raise TypeError(f"Failed to initialize instance of '{type_hint.__name__}': {e}")
raise TypeError(f"Unsupported type hint '{type_hint}' for initialization.")
class PydanticBaseModel(PydanticModelNestedValueMixin, BaseModel):
"""Base model with pendulum datetime support, nested value utilities, and stable hashing.
This class provides:
- ISO 8601 serialization/deserialization of `pendulum.DateTime` fields.
- Nested attribute access and mutation via `PydanticModelNestedValueMixin`.
- A consistent hash using a UUID for use in sets and as dictionary keys
""" """
# Enable custom serialization globally in config # Enable custom serialization globally in config
@@ -642,17 +160,6 @@ class PydanticBaseModel(PydanticModelNestedValueMixin, BaseModel):
validate_assignment=True, validate_assignment=True,
) )
_uuid: str = PrivateAttr(default_factory=lambda: str(uuid.uuid4()))
"""str: A private UUID string generated on instantiation, used for hashing."""
def __hash__(self) -> int:
"""Returns a stable hash based on the instance's UUID.
Returns:
int: Hash value derived from the model's UUID.
"""
return hash(self._uuid)
@field_validator("*", mode="before") @field_validator("*", mode="before")
def validate_and_convert_pendulum(cls, value: Any, info: ValidationInfo) -> Any: def validate_and_convert_pendulum(cls, value: Any, info: ValidationInfo) -> Any:
"""Validator to convert fields of type `pendulum.DateTime`. """Validator to convert fields of type `pendulum.DateTime`.
@@ -681,8 +188,8 @@ class PydanticBaseModel(PydanticModelNestedValueMixin, BaseModel):
if expected_type is pendulum.DateTime or expected_type is AwareDatetime: if expected_type is pendulum.DateTime or expected_type is AwareDatetime:
try: try:
value = to_datetime(value) value = to_datetime(value)
except Exception as e: except:
raise ValueError(f"Cannot convert {value!r} to datetime: {e}") pass
return value return value
# Override Pydantics serialization for all DateTime fields # Override Pydantics serialization for all DateTime fields
@@ -1070,27 +577,3 @@ class PydanticDateTimeSeries(PydanticBaseModel):
class ParametersBaseModel(PydanticBaseModel): class ParametersBaseModel(PydanticBaseModel):
model_config = ConfigDict(extra="forbid") model_config = ConfigDict(extra="forbid")
def set_private_attr(
model: Union[PydanticBaseModel, PydanticModelNestedValueMixin], key: str, value: Any
) -> None:
"""Set a private attribute for a model instance (not stored in model itself)."""
if model not in _model_private_state:
_model_private_state[model] = {}
_model_private_state[model][key] = value
def get_private_attr(
model: Union[PydanticBaseModel, PydanticModelNestedValueMixin], key: str, default: Any = None
) -> Any:
"""Get a private attribute or return default."""
return _model_private_state.get(model, {}).get(key, default)
def del_private_attr(
model: Union[PydanticBaseModel, PydanticModelNestedValueMixin], key: str
) -> None:
"""Delete a private attribute."""
if model in _model_private_state and key in _model_private_state[model]:
del _model_private_state[model][key]

View File

@@ -3,6 +3,7 @@ from typing import Any, Optional
import numpy as np import numpy as np
from pydantic import Field, field_validator from pydantic import Field, field_validator
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.devices.devicesabc import ( from akkudoktoreos.devices.devicesabc import (
DeviceBase, DeviceBase,
DeviceOptimizeResult, DeviceOptimizeResult,
@@ -10,6 +11,8 @@ from akkudoktoreos.devices.devicesabc import (
) )
from akkudoktoreos.utils.utils import NumpyEncoder from akkudoktoreos.utils.utils import NumpyEncoder
logger = get_logger(__name__)
def max_charging_power_field(description: Optional[str] = None) -> float: def max_charging_power_field(description: Optional[str] = None) -> float:
if description is None: if description is None:
@@ -118,8 +121,7 @@ class Battery(DeviceBase):
def _setup(self) -> None: def _setup(self) -> None:
"""Sets up the battery parameters based on configuration or provided parameters.""" """Sets up the battery parameters based on configuration or provided parameters."""
if self.parameters is None: assert self.parameters is not None
raise ValueError(f"Parameters not set: {self.parameters}")
self.capacity_wh = self.parameters.capacity_wh self.capacity_wh = self.parameters.capacity_wh
self.initial_soc_percentage = self.parameters.initial_soc_percentage self.initial_soc_percentage = self.parameters.initial_soc_percentage
self.charging_efficiency = self.parameters.charging_efficiency self.charging_efficiency = self.parameters.charging_efficiency

View File

@@ -1,12 +1,15 @@
from typing import Optional from typing import Optional
from akkudoktoreos.core.coreabc import SingletonMixin from akkudoktoreos.core.coreabc import SingletonMixin
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.devices.battery import Battery from akkudoktoreos.devices.battery import Battery
from akkudoktoreos.devices.devicesabc import DevicesBase from akkudoktoreos.devices.devicesabc import DevicesBase
from akkudoktoreos.devices.generic import HomeAppliance from akkudoktoreos.devices.generic import HomeAppliance
from akkudoktoreos.devices.inverter import Inverter from akkudoktoreos.devices.inverter import Inverter
from akkudoktoreos.devices.settings import DevicesCommonSettings from akkudoktoreos.devices.settings import DevicesCommonSettings
logger = get_logger(__name__)
class Devices(SingletonMixin, DevicesBase): class Devices(SingletonMixin, DevicesBase):
def __init__(self, settings: Optional[DevicesCommonSettings] = None): def __init__(self, settings: Optional[DevicesCommonSettings] = None):
@@ -36,14 +39,10 @@ class Devices(SingletonMixin, DevicesBase):
device.post_setup() device.post_setup()
# Initialize the Devices simulation, it is a singleton. # Initialize the Devices simulation, it is a singleton.
devices: Optional[Devices] = None devices = Devices()
def get_devices() -> Devices: def get_devices() -> Devices:
global devices
# Fix circular import at runtime
if devices is None:
devices = Devices()
"""Gets the EOS Devices simulation.""" """Gets the EOS Devices simulation."""
return devices return devices

View File

@@ -3,7 +3,6 @@
from enum import Enum from enum import Enum
from typing import Optional, Type from typing import Optional, Type
from loguru import logger
from pendulum import DateTime from pendulum import DateTime
from pydantic import Field, computed_field from pydantic import Field, computed_field
@@ -13,9 +12,12 @@ from akkudoktoreos.core.coreabc import (
EnergyManagementSystemMixin, EnergyManagementSystemMixin,
PredictionMixin, PredictionMixin,
) )
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import ParametersBaseModel from akkudoktoreos.core.pydantic import ParametersBaseModel
from akkudoktoreos.utils.datetimeutil import to_duration from akkudoktoreos.utils.datetimeutil import to_duration
logger = get_logger(__name__)
class DeviceParameters(ParametersBaseModel): class DeviceParameters(ParametersBaseModel):
device_id: str = Field(description="ID of device", examples="device1") device_id: str = Field(description="ID of device", examples="device1")
@@ -168,8 +170,7 @@ class DevicesBase(DevicesStartEndMixin, PredictionMixin):
def add_device(self, device: Optional["DeviceBase"]) -> None: def add_device(self, device: Optional["DeviceBase"]) -> None:
if device is None: if device is None:
return return
if device.device_id in self.devices: assert device.device_id not in self.devices, f"{device.device_id} already registered"
raise ValueError(f"{device.device_id} already registered")
self.devices[device.device_id] = device self.devices[device.device_id] = device
def remove_device(self, device: Type["DeviceBase"] | str) -> bool: def remove_device(self, device: Type["DeviceBase"] | str) -> bool:

View File

@@ -3,8 +3,11 @@ from typing import Optional
import numpy as np import numpy as np
from pydantic import Field from pydantic import Field
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.devices.devicesabc import DeviceBase, DeviceParameters from akkudoktoreos.devices.devicesabc import DeviceBase, DeviceParameters
logger = get_logger(__name__)
class HomeApplianceParameters(DeviceParameters): class HomeApplianceParameters(DeviceParameters):
"""Home Appliance Device Simulation Configuration.""" """Home Appliance Device Simulation Configuration."""
@@ -31,8 +34,7 @@ class HomeAppliance(DeviceBase):
super().__init__(parameters) super().__init__(parameters)
def _setup(self) -> None: def _setup(self) -> None:
if self.parameters is None: assert self.parameters is not None
raise ValueError(f"Parameters not set: {self.parameters}")
self.load_curve = np.zeros(self.hours) # Initialize the load curve with zeros self.load_curve = np.zeros(self.hours) # Initialize the load curve with zeros
self.duration_h = self.parameters.duration_h self.duration_h = self.parameters.duration_h
self.consumption_wh = self.parameters.consumption_wh self.consumption_wh = self.parameters.consumption_wh

View File

@@ -1,6 +1,6 @@
from typing import List, Sequence from typing import List, Sequence
from loguru import logger from akkudoktoreos.core.logging import get_logger
class Heatpump: class Heatpump:
@@ -22,6 +22,7 @@ class Heatpump:
def __init__(self, max_heat_output: int, hours: int): def __init__(self, max_heat_output: int, hours: int):
self.max_heat_output = max_heat_output self.max_heat_output = max_heat_output
self.hours = hours self.hours = hours
self.log = get_logger(__name__)
def __check_outside_temperature_range__(self, temp_celsius: float) -> bool: def __check_outside_temperature_range__(self, temp_celsius: float) -> bool:
"""Check if temperature is in valid range between -100 and 100 degree Celsius. """Check if temperature is in valid range between -100 and 100 degree Celsius.
@@ -58,7 +59,7 @@ class Heatpump:
f"Outside temperature '{outside_temperature_celsius}' not in range " f"Outside temperature '{outside_temperature_celsius}' not in range "
"(min: -100 Celsius, max: 100 Celsius)" "(min: -100 Celsius, max: 100 Celsius)"
) )
logger.error(err_msg) self.log.error(err_msg)
raise ValueError(err_msg) raise ValueError(err_msg)
def calculate_heating_output(self, outside_temperature_celsius: float) -> float: def calculate_heating_output(self, outside_temperature_celsius: float) -> float:
@@ -86,7 +87,7 @@ class Heatpump:
f"Outside temperature '{outside_temperature_celsius}' not in range " f"Outside temperature '{outside_temperature_celsius}' not in range "
"(min: -100 Celsius, max: 100 Celsius)" "(min: -100 Celsius, max: 100 Celsius)"
) )
logger.error(err_msg) self.log.error(err_msg)
raise ValueError(err_msg) raise ValueError(err_msg)
def calculate_heat_power(self, outside_temperature_celsius: float) -> float: def calculate_heat_power(self, outside_temperature_celsius: float) -> float:
@@ -110,7 +111,7 @@ class Heatpump:
f"Outside temperature '{outside_temperature_celsius}' not in range " f"Outside temperature '{outside_temperature_celsius}' not in range "
"(min: -100 Celsius, max: 100 Celsius)" "(min: -100 Celsius, max: 100 Celsius)"
) )
logger.error(err_msg) self.log.error(err_msg)
raise ValueError(err_msg) raise ValueError(err_msg)
def simulate_24h(self, temperatures: Sequence[float]) -> List[float]: def simulate_24h(self, temperatures: Sequence[float]) -> List[float]:

View File

@@ -1,11 +1,13 @@
from typing import Optional from typing import Optional
from loguru import logger
from pydantic import Field from pydantic import Field
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.devices.devicesabc import DeviceBase, DeviceParameters from akkudoktoreos.devices.devicesabc import DeviceBase, DeviceParameters
from akkudoktoreos.prediction.interpolator import get_eos_load_interpolator from akkudoktoreos.prediction.interpolator import get_eos_load_interpolator
logger = get_logger(__name__)
class InverterParameters(DeviceParameters): class InverterParameters(DeviceParameters):
"""Inverter Device Simulation Configuration.""" """Inverter Device Simulation Configuration."""
@@ -25,25 +27,8 @@ class Inverter(DeviceBase):
self.parameters: Optional[InverterParameters] = None self.parameters: Optional[InverterParameters] = None
super().__init__(parameters) super().__init__(parameters)
self.scr_lookup: dict = {}
def _calculate_scr(self, consumption: float, generation: float) -> float:
"""Check if the consumption and production is in the lookup table. If not, calculate and store the value."""
if consumption not in self.scr_lookup:
self.scr_lookup[consumption] = {}
if generation not in self.scr_lookup[consumption]:
scr = self.self_consumption_predictor.calculate_self_consumption(
consumption, generation
)
self.scr_lookup[consumption][generation] = scr
return scr
return self.scr_lookup[consumption][generation]
def _setup(self) -> None: def _setup(self) -> None:
if self.parameters is None: assert self.parameters is not None
raise ValueError(f"Parameters not set: {self.parameters}")
if self.parameters.battery_id is None: if self.parameters.battery_id is None:
# For the moment raise exception # For the moment raise exception
# TODO: Make battery configurable by config # TODO: Make battery configurable by config
@@ -56,8 +41,7 @@ class Inverter(DeviceBase):
) # Maximum power that the inverter can handle ) # Maximum power that the inverter can handle
def _post_setup(self) -> None: def _post_setup(self) -> None:
if self.parameters is None: assert self.parameters is not None
raise ValueError(f"Parameters not set: {self.parameters}")
self.battery = self.devices.get_device_by_id(self.parameters.battery_id) self.battery = self.devices.get_device_by_id(self.parameters.battery_id)
def process_energy( def process_energy(
@@ -76,8 +60,9 @@ class Inverter(DeviceBase):
grid_import = -remaining_power # Negative indicates feeding into the grid grid_import = -remaining_power # Negative indicates feeding into the grid
self_consumption = self.max_power_wh self_consumption = self.max_power_wh
else: else:
# Calculate scr with lookup table scr = self.self_consumption_predictor.calculate_self_consumption(
scr = self._calculate_scr(consumption, generation) consumption, generation
)
# Remaining power after consumption # Remaining power after consumption
remaining_power = (generation - consumption) * scr # EVQ remaining_power = (generation - consumption) * scr # EVQ

View File

@@ -3,10 +3,13 @@ from typing import Optional
from pydantic import Field from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.devices.battery import BaseBatteryParameters from akkudoktoreos.devices.battery import BaseBatteryParameters
from akkudoktoreos.devices.generic import HomeApplianceParameters from akkudoktoreos.devices.generic import HomeApplianceParameters
from akkudoktoreos.devices.inverter import InverterParameters from akkudoktoreos.devices.inverter import InverterParameters
logger = get_logger(__name__)
class DevicesCommonSettings(SettingsBaseModel): class DevicesCommonSettings(SettingsBaseModel):
"""Base configuration for devices simulation settings.""" """Base configuration for devices simulation settings."""

View File

@@ -9,7 +9,6 @@ The measurements can be added programmatically or imported from a file or JSON s
from typing import Any, ClassVar, List, Optional from typing import Any, ClassVar, List, Optional
import numpy as np import numpy as np
from loguru import logger
from numpydantic import NDArray, Shape from numpydantic import NDArray, Shape
from pendulum import DateTime, Duration from pendulum import DateTime, Duration
from pydantic import Field, computed_field from pydantic import Field, computed_field
@@ -17,8 +16,11 @@ from pydantic import Field, computed_field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.coreabc import SingletonMixin from akkudoktoreos.core.coreabc import SingletonMixin
from akkudoktoreos.core.dataabc import DataImportMixin, DataRecord, DataSequence from akkudoktoreos.core.dataabc import DataImportMixin, DataRecord, DataSequence
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.utils.datetimeutil import to_duration from akkudoktoreos.utils.datetimeutil import to_duration
logger = get_logger(__name__)
class MeasurementCommonSettings(SettingsBaseModel): class MeasurementCommonSettings(SettingsBaseModel):
"""Measurement Configuration.""" """Measurement Configuration."""

View File

@@ -4,7 +4,6 @@ from typing import Any, Optional
import numpy as np import numpy as np
from deap import algorithms, base, creator, tools from deap import algorithms, base, creator, tools
from loguru import logger
from pydantic import Field, field_validator, model_validator from pydantic import Field, field_validator, model_validator
from typing_extensions import Self from typing_extensions import Self
@@ -14,6 +13,7 @@ from akkudoktoreos.core.coreabc import (
EnergyManagementSystemMixin, EnergyManagementSystemMixin,
) )
from akkudoktoreos.core.ems import EnergyManagementParameters, SimulationResult from akkudoktoreos.core.ems import EnergyManagementParameters, SimulationResult
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import ParametersBaseModel from akkudoktoreos.core.pydantic import ParametersBaseModel
from akkudoktoreos.devices.battery import ( from akkudoktoreos.devices.battery import (
Battery, Battery,
@@ -25,6 +25,8 @@ from akkudoktoreos.devices.generic import HomeAppliance, HomeApplianceParameters
from akkudoktoreos.devices.inverter import Inverter, InverterParameters from akkudoktoreos.devices.inverter import Inverter, InverterParameters
from akkudoktoreos.utils.utils import NumpyEncoder from akkudoktoreos.utils.utils import NumpyEncoder
logger = get_logger(__name__)
class OptimizationParameters(ParametersBaseModel): class OptimizationParameters(ParametersBaseModel):
ems: EnergyManagementParameters ems: EnergyManagementParameters
@@ -119,7 +121,7 @@ class optimization_problem(ConfigMixin, DevicesMixin, EnergyManagementSystemMixi
if self.fix_seed is not None: if self.fix_seed is not None:
random.seed(self.fix_seed) random.seed(self.fix_seed)
elif logger.level == "DEBUG": elif logger.level == "DEBUG":
self.fix_seed = random.randint(1, 100000000000) # noqa: S311 self.fix_seed = random.randint(1, 100000000000)
random.seed(self.fix_seed) random.seed(self.fix_seed)
def decode_charge_discharge( def decode_charge_discharge(

View File

@@ -3,6 +3,9 @@ from typing import List, Optional
from pydantic import Field from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
class OptimizationCommonSettings(SettingsBaseModel): class OptimizationCommonSettings(SettingsBaseModel):

View File

@@ -3,8 +3,11 @@
from pydantic import ConfigDict from pydantic import ConfigDict
from akkudoktoreos.core.coreabc import ConfigMixin, PredictionMixin from akkudoktoreos.core.coreabc import ConfigMixin, PredictionMixin
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticBaseModel from akkudoktoreos.core.pydantic import PydanticBaseModel
logger = get_logger(__name__)
class OptimizationBase(ConfigMixin, PredictionMixin, PydanticBaseModel): class OptimizationBase(ConfigMixin, PredictionMixin, PydanticBaseModel):
"""Base class for handling optimization data. """Base class for handling optimization data.

View File

@@ -1,20 +1,9 @@
from typing import Optional from typing import Optional
from pydantic import Field, field_validator from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider
from akkudoktoreos.prediction.elecpriceimport import ElecPriceImportCommonSettings from akkudoktoreos.prediction.elecpriceimport import ElecPriceImportCommonSettings
from akkudoktoreos.prediction.prediction import get_prediction
prediction_eos = get_prediction()
# Valid elecprice providers
elecprice_providers = [
provider.provider_id()
for provider in prediction_eos.providers
if isinstance(provider, ElecPriceProvider)
]
class ElecPriceCommonSettings(SettingsBaseModel): class ElecPriceCommonSettings(SettingsBaseModel):
@@ -28,23 +17,7 @@ class ElecPriceCommonSettings(SettingsBaseModel):
charges_kwh: Optional[float] = Field( charges_kwh: Optional[float] = Field(
default=None, ge=0, description="Electricity price charges (€/kWh).", examples=[0.21] default=None, ge=0, description="Electricity price charges (€/kWh).", examples=[0.21]
) )
vat_rate: Optional[float] = Field(
default=1.19,
ge=0,
description="VAT rate factor applied to electricity price when charges are used.",
examples=[1.19],
)
provider_settings: Optional[ElecPriceImportCommonSettings] = Field( provider_settings: Optional[ElecPriceImportCommonSettings] = Field(
default=None, description="Provider settings", examples=[None] default=None, description="Provider settings", examples=[None]
) )
# Validators
@field_validator("provider", mode="after")
@classmethod
def validate_provider(cls, value: Optional[str]) -> Optional[str]:
if value is None or value in elecprice_providers:
return value
raise ValueError(
f"Provider '{value}' is not a valid electricity price provider: {elecprice_providers}."
)

View File

@@ -9,8 +9,11 @@ from typing import List, Optional
from pydantic import Field, computed_field from pydantic import Field, computed_field
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord
logger = get_logger(__name__)
class ElecPriceDataRecord(PredictionRecord): class ElecPriceDataRecord(PredictionRecord):
"""Represents a electricity price data record containing various price attributes at a specific datetime. """Represents a electricity price data record containing various price attributes at a specific datetime.

View File

@@ -11,15 +11,17 @@ from typing import Any, List, Optional, Union
import numpy as np import numpy as np
import pandas as pd import pandas as pd
import requests import requests
from loguru import logger
from pydantic import ValidationError from pydantic import ValidationError
from statsmodels.tsa.holtwinters import ExponentialSmoothing from statsmodels.tsa.holtwinters import ExponentialSmoothing
from akkudoktoreos.core.cache import cache_in_file from akkudoktoreos.core.cache import cache_in_file
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticBaseModel from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration
logger = get_logger(__name__)
class AkkudoktorElecPriceMeta(PydanticBaseModel): class AkkudoktorElecPriceMeta(PydanticBaseModel):
start_timestamp: str start_timestamp: str
@@ -102,13 +104,12 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
- add the file cache again. - add the file cache again.
""" """
source = "https://api.akkudoktor.net" source = "https://api.akkudoktor.net"
if not self.start_datetime: assert self.start_datetime # mypy fix
raise ValueError(f"Start DateTime not set: {self.start_datetime}")
# Try to take data from 5 weeks back for prediction # Try to take data from 5 weeks back for prediction
date = to_datetime(self.start_datetime - to_duration("35 days"), as_string="YYYY-MM-DD") date = to_datetime(self.start_datetime - to_duration("35 days"), as_string="YYYY-MM-DD")
last_date = to_datetime(self.end_datetime, as_string="YYYY-MM-DD") last_date = to_datetime(self.end_datetime, as_string="YYYY-MM-DD")
url = f"{source}/prices?start={date}&end={last_date}&tz={self.config.general.timezone}" url = f"{source}/prices?start={date}&end={last_date}&tz={self.config.general.timezone}"
response = requests.get(url, timeout=10) response = requests.get(url)
logger.debug(f"Response from {url}: {response}") logger.debug(f"Response from {url}: {response}")
response.raise_for_status() # Raise an error for bad responses response.raise_for_status() # Raise an error for bad responses
akkudoktor_data = self._validate_data(response.content) akkudoktor_data = self._validate_data(response.content)
@@ -147,8 +148,7 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
""" """
# Get Akkudoktor electricity price data # Get Akkudoktor electricity price data
akkudoktor_data = self._request_forecast(force_update=force_update) # type: ignore akkudoktor_data = self._request_forecast(force_update=force_update) # type: ignore
if not self.start_datetime: assert self.start_datetime # mypy fix
raise ValueError(f"Start DateTime not set: {self.start_datetime}")
# Assumption that all lists are the same length and are ordered chronologically # Assumption that all lists are the same length and are ordered chronologically
# in ascending order and have the same timestamps. # in ascending order and have the same timestamps.
@@ -178,10 +178,7 @@ class ElecPriceAkkudoktor(ElecPriceProvider):
) )
amount_datasets = len(self.records) amount_datasets = len(self.records)
if not highest_orig_datetime: # mypy fix assert highest_orig_datetime # mypy fix
error_msg = f"Highest original datetime not available: {highest_orig_datetime}"
logger.error(error_msg)
raise ValueError(error_msg)
# some of our data is already in the future, so we need to predict less. If we got less data we increase the prediction hours # some of our data is already in the future, so we need to predict less. If we got less data we increase the prediction hours
needed_hours = int( needed_hours = int(

View File

@@ -1,257 +0,0 @@
"""Retrieves and processes electricity price forecast data from Energy-Charts.
This module provides classes and mappings to manage electricity price data obtained from the
Energy-Charts API, including support for various electricity price attributes such as temperature,
humidity, cloud cover, and solar irradiance. The data is mapped to the `ElecPriceDataRecord`
format, enabling consistent access to forecasted and historical electricity price attributes.
"""
from datetime import datetime
from typing import Any, List, Optional, Union
import numpy as np
import pandas as pd
import requests
from loguru import logger
from pydantic import ValidationError
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from akkudoktoreos.core.cache import cache_in_file
from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration
class EnergyChartsElecPrice(PydanticBaseModel):
license_info: str
unix_seconds: List[int]
price: List[float]
unit: str
deprecated: bool
class ElecPriceEnergyCharts(ElecPriceProvider):
"""Fetch and process electricity price forecast data from Energy-Charts.
ElecPriceEnergyCharts is a singleton-based class that retrieves electricity price forecast data
from the Energy-Charts API and maps it to `ElecPriceDataRecord` fields, applying
any necessary scaling or unit corrections. It manages the forecast over a range
of hours into the future and retains historical data.
Attributes:
hours (int, optional): Number of hours in the future for the forecast.
historic_hours (int, optional): Number of past hours for retaining data.
start_datetime (datetime, optional): Start datetime for forecasts, defaults to the current datetime.
end_datetime (datetime, computed): The forecast's end datetime, computed based on `start_datetime` and `hours`.
keep_datetime (datetime, computed): The datetime to retain historical data, computed from `start_datetime` and `historic_hours`.
Methods:
provider_id(): Returns a unique identifier for the provider.
_request_forecast(): Fetches the forecast from the Energy-Charts API.
_update_data(): Processes and updates forecast data from Energy-Charts in ElecPriceDataRecord format.
"""
highest_orig_datetime: Optional[datetime] = None
@classmethod
def provider_id(cls) -> str:
"""Return the unique identifier for the Energy-Charts provider."""
return "ElecPriceEnergyCharts"
@classmethod
def _validate_data(cls, json_str: Union[bytes, Any]) -> EnergyChartsElecPrice:
"""Validate Energy-Charts Electricity Price forecast data."""
try:
energy_charts_data = EnergyChartsElecPrice.model_validate_json(json_str)
except ValidationError as e:
error_msg = ""
for error in e.errors():
field = " -> ".join(str(x) for x in error["loc"])
message = error["msg"]
error_type = error["type"]
error_msg += f"Field: {field}\nError: {message}\nType: {error_type}\n"
logger.error(f"Energy-Charts schema change: {error_msg}")
raise ValueError(error_msg)
return energy_charts_data
@cache_in_file(with_ttl="1 hour")
def _request_forecast(self, start_date: Optional[str] = None) -> EnergyChartsElecPrice:
"""Fetch electricity price forecast data from Energy-Charts API.
This method sends a request to Energy-Charts API to retrieve forecast data for a specified
date range. The response data is parsed and returned as JSON for further processing.
Returns:
dict: The parsed JSON response from Energy-Charts API containing forecast data.
Raises:
ValueError: If the API response does not include expected `electricity price` data.
"""
source = "https://api.energy-charts.info"
if start_date is None:
# Try to take data from 5 weeks back for prediction
start_date = to_datetime(
self.start_datetime - to_duration("35 days"), as_string="YYYY-MM-DD"
)
last_date = to_datetime(self.end_datetime, as_string="YYYY-MM-DD")
url = f"{source}/price?bzn=DE-LU&start={start_date}&end={last_date}"
response = requests.get(url, timeout=30)
logger.debug(f"Response from {url}: {response}")
response.raise_for_status() # Raise an error for bad responses
energy_charts_data = self._validate_data(response.content)
# We are working on fresh data (no cache), report update time
self.update_datetime = to_datetime(in_timezone=self.config.general.timezone)
return energy_charts_data
def _parse_data(self, energy_charts_data: EnergyChartsElecPrice) -> pd.Series:
# Assumption that all lists are the same length and are ordered chronologically
# in ascending order and have the same timestamps.
# Get charges_kwh in wh
charges_wh = (self.config.elecprice.charges_kwh or 0) / 1000
# Initialize
highest_orig_datetime = None # newest datetime from the api after that we want to update.
series_data = pd.Series(dtype=float) # Initialize an empty series
# Iterate over timestamps and prices together
for unix_sec, price_eur_per_mwh in zip(
energy_charts_data.unix_seconds, energy_charts_data.price
):
orig_datetime = to_datetime(unix_sec, in_timezone=self.config.general.timezone)
# Track the latest datetime
if highest_orig_datetime is None or orig_datetime > highest_orig_datetime:
highest_orig_datetime = orig_datetime
# Convert EUR/MWh to EUR/Wh, apply charges and VAT if charges > 0
if charges_wh > 0:
vat_rate = self.config.elecprice.vat_rate or 1.19
price_wh = ((price_eur_per_mwh / 1_000_000) + charges_wh) * vat_rate
else:
price_wh = price_eur_per_mwh / 1_000_000
# Store in series
series_data.at[orig_datetime] = price_wh
return series_data
def _cap_outliers(self, data: np.ndarray, sigma: int = 2) -> np.ndarray:
mean = data.mean()
std = data.std()
lower_bound = mean - sigma * std
upper_bound = mean + sigma * std
capped_data = data.clip(min=lower_bound, max=upper_bound)
return capped_data
def _predict_ets(self, history: np.ndarray, seasonal_periods: int, hours: int) -> np.ndarray:
clean_history = self._cap_outliers(history)
model = ExponentialSmoothing(
clean_history, seasonal="add", seasonal_periods=seasonal_periods
).fit()
return model.forecast(hours)
def _predict_median(self, history: np.ndarray, hours: int) -> np.ndarray:
clean_history = self._cap_outliers(history)
return np.full(hours, np.median(clean_history))
def _update_data(
self, force_update: Optional[bool] = False
) -> None: # tuple[np.ndarray, np.ndarray, np.ndarray]:
"""Update forecast data in the ElecPriceDataRecord format.
Retrieves data from Energy-Charts, maps each Energy-Charts field to the corresponding
`ElecPriceDataRecord` and applies any necessary scaling.
The final mapped and processed data is inserted into the sequence as `ElecPriceDataRecord`.
"""
# New prices are available every day at 14:00
now = pd.Timestamp.now(tz=self.config.general.timezone)
midnight = now.normalize()
hours_ahead = 23 if now.time() < pd.Timestamp("14:00").time() else 47
end = midnight + pd.Timedelta(hours=hours_ahead)
if not self.start_datetime:
raise ValueError(f"Start DateTime not set: {self.start_datetime}")
# Determine if update is needed and how many days
past_days = 35
if self.highest_orig_datetime:
history_series = self.key_to_series(
key="elecprice_marketprice_wh", start_datetime=self.start_datetime
)
# If history lower, then start_datetime
if history_series.index.min() <= self.start_datetime:
past_days = 0
needs_update = end > self.highest_orig_datetime
else:
needs_update = True
if needs_update:
logger.info(
f"Update ElecPriceEnergyCharts is needed, last in history: {self.highest_orig_datetime}"
)
# Set start_date try to take data from 5 weeks back for prediction
start_date = to_datetime(
self.start_datetime - to_duration(f"{past_days} days"), as_string="YYYY-MM-DD"
)
# Get Energy-Charts electricity price data
energy_charts_data = self._request_forecast(
start_date=start_date, force_update=force_update
) # type: ignore
# Parse and store data
series_data = self._parse_data(energy_charts_data)
self.highest_orig_datetime = series_data.index.max()
self.key_from_series("elecprice_marketprice_wh", series_data)
else:
logger.info(
f"No Update ElecPriceEnergyCharts is needed, last in history: {self.highest_orig_datetime}"
)
# Generate history array for prediction
history = self.key_to_array(
key="elecprice_marketprice_wh",
end_datetime=self.highest_orig_datetime,
fill_method="linear",
)
amount_datasets = len(self.records)
if not self.highest_orig_datetime: # mypy fix
error_msg = f"Highest original datetime not available: {self.highest_orig_datetime}"
logger.error(error_msg)
raise ValueError(error_msg)
# some of our data is already in the future, so we need to predict less. If we got less data we increase the prediction hours
needed_hours = int(
self.config.prediction.hours
- ((self.highest_orig_datetime - self.start_datetime).total_seconds() // 3600)
)
if needed_hours <= 0:
logger.warning(
f"No prediction needed. needed_hours={needed_hours}, hours={self.config.prediction.hours},highest_orig_datetime {self.highest_orig_datetime}, start_datetime {self.start_datetime}"
) # this might keep data longer than self.start_datetime + self.config.prediction.hours in the records
return
if amount_datasets > 800: # we do the full ets with seasons of 1 week
prediction = self._predict_ets(history, seasonal_periods=168, hours=needed_hours)
elif amount_datasets > 168: # not enough data to do seasons of 1 week, but enough for 1 day
prediction = self._predict_ets(history, seasonal_periods=24, hours=needed_hours)
elif amount_datasets > 0: # not enough data for ets, do median
prediction = self._predict_median(history, hours=needed_hours)
else:
logger.error("No data available for prediction")
raise ValueError("No data available")
# write predictions into the records, update if exist.
prediction_series = pd.Series(
data=prediction,
index=[
self.highest_orig_datetime + to_duration(f"{i + 1} hours")
for i in range(len(prediction))
],
)
self.key_from_series("elecprice_marketprice_wh", prediction_series)

View File

@@ -9,13 +9,15 @@ format, enabling consistent access to forecasted and historical elecprice attrib
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
from loguru import logger
from pydantic import Field, field_validator from pydantic import Field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider from akkudoktoreos.prediction.elecpriceabc import ElecPriceProvider
from akkudoktoreos.prediction.predictionabc import PredictionImportProvider from akkudoktoreos.prediction.predictionabc import PredictionImportProvider
logger = get_logger(__name__)
class ElecPriceImportCommonSettings(SettingsBaseModel): class ElecPriceImportCommonSettings(SettingsBaseModel):
"""Common settings for elecprice data import from file or JSON String.""" """Common settings for elecprice data import from file or JSON String."""

View File

@@ -14,7 +14,7 @@ class SelfConsumptionProbabilityInterpolator:
self.filepath = filepath self.filepath = filepath
# Load the RegularGridInterpolator # Load the RegularGridInterpolator
with open(self.filepath, "rb") as file: with open(self.filepath, "rb") as file:
self.interpolator: RegularGridInterpolator = pickle.load(file) # noqa: S301 self.interpolator: RegularGridInterpolator = pickle.load(file)
@lru_cache(maxsize=128) @lru_cache(maxsize=128)
def generate_points( def generate_points(

View File

@@ -2,23 +2,14 @@
from typing import Optional, Union from typing import Optional, Union
from pydantic import Field, field_validator from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.prediction.loadabc import LoadProvider from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.loadakkudoktor import LoadAkkudoktorCommonSettings from akkudoktoreos.prediction.loadakkudoktor import LoadAkkudoktorCommonSettings
from akkudoktoreos.prediction.loadimport import LoadImportCommonSettings from akkudoktoreos.prediction.loadimport import LoadImportCommonSettings
from akkudoktoreos.prediction.loadvrm import LoadVrmCommonSettings
from akkudoktoreos.prediction.prediction import get_prediction
prediction_eos = get_prediction() logger = get_logger(__name__)
# Valid load providers
load_providers = [
provider.provider_id()
for provider in prediction_eos.providers
if isinstance(provider, LoadProvider)
]
class LoadCommonSettings(SettingsBaseModel): class LoadCommonSettings(SettingsBaseModel):
@@ -30,14 +21,6 @@ class LoadCommonSettings(SettingsBaseModel):
examples=["LoadAkkudoktor"], examples=["LoadAkkudoktor"],
) )
provider_settings: Optional[ provider_settings: Optional[Union[LoadAkkudoktorCommonSettings, LoadImportCommonSettings]] = (
Union[LoadAkkudoktorCommonSettings, LoadVrmCommonSettings, LoadImportCommonSettings] Field(default=None, description="Provider settings", examples=[None])
] = Field(default=None, description="Provider settings", examples=[None]) )
# Validators
@field_validator("provider", mode="after")
@classmethod
def validate_provider(cls, value: Optional[str]) -> Optional[str]:
if value is None or value in load_providers:
return value
raise ValueError(f"Provider '{value}' is not a valid load provider: {load_providers}.")

View File

@@ -9,8 +9,11 @@ from typing import List, Optional
from pydantic import Field from pydantic import Field
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord
logger = get_logger(__name__)
class LoadDataRecord(PredictionRecord): class LoadDataRecord(PredictionRecord):
"""Represents a load data record containing various load attributes at a specific datetime.""" """Represents a load data record containing various load attributes at a specific datetime."""

View File

@@ -3,13 +3,15 @@
from typing import Optional from typing import Optional
import numpy as np import numpy as np
from loguru import logger
from pydantic import Field from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.loadabc import LoadProvider from akkudoktoreos.prediction.loadabc import LoadProvider
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration
logger = get_logger(__name__)
class LoadAkkudoktorCommonSettings(SettingsBaseModel): class LoadAkkudoktorCommonSettings(SettingsBaseModel):
"""Common settings for load data import from file.""" """Common settings for load data import from file."""
@@ -120,11 +122,10 @@ class LoadAkkudoktor(LoadProvider):
} }
if date.day_of_week < 5: if date.day_of_week < 5:
# Monday to Friday (0..4) # Monday to Friday (0..4)
value_adjusted = hourly_stats[0] + weekday_adjust[date.hour] values["load_mean_adjusted"] = hourly_stats[0] + weekday_adjust[date.hour]
else: else:
# Saturday, Sunday (5, 6) # Saturday, Sunday (5, 6)
value_adjusted = hourly_stats[0] + weekend_adjust[date.hour] values["load_mean_adjusted"] = hourly_stats[0] + weekend_adjust[date.hour]
values["load_mean_adjusted"] = max(0, value_adjusted)
self.update_value(date, values) self.update_value(date, values)
date += to_duration("1 hour") date += to_duration("1 hour")
# We are working on fresh data (no cache), report update time # We are working on fresh data (no cache), report update time

View File

@@ -9,13 +9,15 @@ format, enabling consistent access to forecasted and historical load attributes.
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
from loguru import logger
from pydantic import Field, field_validator from pydantic import Field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.loadabc import LoadProvider from akkudoktoreos.prediction.loadabc import LoadProvider
from akkudoktoreos.prediction.predictionabc import PredictionImportProvider from akkudoktoreos.prediction.predictionabc import PredictionImportProvider
logger = get_logger(__name__)
class LoadImportCommonSettings(SettingsBaseModel): class LoadImportCommonSettings(SettingsBaseModel):
"""Common settings for load data import from file or JSON string.""" """Common settings for load data import from file or JSON string."""

View File

@@ -1,109 +0,0 @@
"""Retrieves load forecast data from VRM API."""
from typing import Any, Optional, Union
import requests
from loguru import logger
from pendulum import DateTime
from pydantic import Field, ValidationError
from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.loadabc import LoadProvider
from akkudoktoreos.utils.datetimeutil import to_datetime
class VrmForecastRecords(PydanticBaseModel):
vrm_consumption_fc: list[tuple[int, float]]
solar_yield_forecast: list[tuple[int, float]]
class VrmForecastResponse(PydanticBaseModel):
success: bool
records: VrmForecastRecords
totals: dict
class LoadVrmCommonSettings(SettingsBaseModel):
"""Common settings for VRM API."""
load_vrm_token: str = Field(
default="your-token", description="Token for Connecting VRM API", examples=["your-token"]
)
load_vrm_idsite: int = Field(default=12345, description="VRM-Installation-ID", examples=[12345])
class LoadVrm(LoadProvider):
"""Fetch Load forecast data from VRM API."""
@classmethod
def provider_id(cls) -> str:
return "LoadVrm"
@classmethod
def _validate_data(cls, json_str: Union[bytes, Any]) -> VrmForecastResponse:
"""Validate the VRM API load forecast response."""
try:
return VrmForecastResponse.model_validate_json(json_str)
except ValidationError as e:
error_msg = "\n".join(
f"Field: {' -> '.join(str(x) for x in err['loc'])}\n"
f"Error: {err['msg']}\nType: {err['type']}"
for err in e.errors()
)
logger.error(f"VRM-API schema validation failed:\n{error_msg}")
raise ValueError(error_msg)
def _request_forecast(self, start_ts: int, end_ts: int) -> VrmForecastResponse:
"""Fetch forecast data from Victron VRM API."""
base_url = "https://vrmapi.victronenergy.com/v2/installations"
installation_id = self.config.load.provider_settings.load_vrm_idsite
api_token = self.config.load.provider_settings.load_vrm_token
url = f"{base_url}/{installation_id}/stats?type=forecast&start={start_ts}&end={end_ts}&interval=hours"
headers = {"X-Authorization": f"Token {api_token}", "Content-Type": "application/json"}
logger.debug(f"Requesting VRM load forecast: {url}")
try:
response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
except requests.RequestException as e:
logger.error(f"Error during VRM API request: {e}")
raise RuntimeError("Failed to fetch load forecast from VRM API") from e
self.update_datetime = to_datetime(in_timezone=self.config.general.timezone)
return self._validate_data(response.content)
def _ts_to_datetime(self, timestamp: int) -> DateTime:
"""Convert UNIX ms timestamp to timezone-aware datetime."""
return to_datetime(timestamp / 1000, in_timezone=self.config.general.timezone)
def _update_data(self, force_update: Optional[bool] = False) -> None:
"""Fetch and store VRM load forecast as load_mean and related values."""
start_date = self.start_datetime.start_of("day")
end_date = self.start_datetime.add(hours=self.config.prediction.hours)
start_ts = int(start_date.timestamp())
end_ts = int(end_date.timestamp())
logger.info(f"Updating Load forecast from VRM: {start_date} to {end_date}")
vrm_forecast_data = self._request_forecast(start_ts, end_ts)
load_mean_data = []
for timestamp, value in vrm_forecast_data.records.vrm_consumption_fc:
date = self._ts_to_datetime(timestamp)
rounded_value = round(value, 2)
self.update_value(
date,
{"load_mean": rounded_value, "load_std": 0.0, "load_mean_adjusted": rounded_value},
)
load_mean_data.append((date, rounded_value))
logger.debug(f"Updated load_mean with {len(load_mean_data)} entries.")
self.update_datetime = to_datetime(in_timezone=self.config.general.timezone)
if __name__ == "__main__":
lv = LoadVrm()
lv._update_data()

View File

@@ -32,15 +32,12 @@ from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.prediction.elecpriceakkudoktor import ElecPriceAkkudoktor from akkudoktoreos.prediction.elecpriceakkudoktor import ElecPriceAkkudoktor
from akkudoktoreos.prediction.elecpriceenergycharts import ElecPriceEnergyCharts
from akkudoktoreos.prediction.elecpriceimport import ElecPriceImport from akkudoktoreos.prediction.elecpriceimport import ElecPriceImport
from akkudoktoreos.prediction.loadakkudoktor import LoadAkkudoktor from akkudoktoreos.prediction.loadakkudoktor import LoadAkkudoktor
from akkudoktoreos.prediction.loadimport import LoadImport from akkudoktoreos.prediction.loadimport import LoadImport
from akkudoktoreos.prediction.loadvrm import LoadVrm
from akkudoktoreos.prediction.predictionabc import PredictionContainer from akkudoktoreos.prediction.predictionabc import PredictionContainer
from akkudoktoreos.prediction.pvforecastakkudoktor import PVForecastAkkudoktor from akkudoktoreos.prediction.pvforecastakkudoktor import PVForecastAkkudoktor
from akkudoktoreos.prediction.pvforecastimport import PVForecastImport from akkudoktoreos.prediction.pvforecastimport import PVForecastImport
from akkudoktoreos.prediction.pvforecastvrm import PVForecastVrm
from akkudoktoreos.prediction.weatherbrightsky import WeatherBrightSky from akkudoktoreos.prediction.weatherbrightsky import WeatherBrightSky
from akkudoktoreos.prediction.weatherclearoutside import WeatherClearOutside from akkudoktoreos.prediction.weatherclearoutside import WeatherClearOutside
from akkudoktoreos.prediction.weatherimport import WeatherImport from akkudoktoreos.prediction.weatherimport import WeatherImport
@@ -86,13 +83,10 @@ class Prediction(PredictionContainer):
providers: List[ providers: List[
Union[ Union[
ElecPriceAkkudoktor, ElecPriceAkkudoktor,
ElecPriceEnergyCharts,
ElecPriceImport, ElecPriceImport,
LoadAkkudoktor, LoadAkkudoktor,
LoadVrm,
LoadImport, LoadImport,
PVForecastAkkudoktor, PVForecastAkkudoktor,
PVForecastVrm,
PVForecastImport, PVForecastImport,
WeatherBrightSky, WeatherBrightSky,
WeatherClearOutside, WeatherClearOutside,
@@ -103,13 +97,10 @@ class Prediction(PredictionContainer):
# Initialize forecast providers, all are singletons. # Initialize forecast providers, all are singletons.
elecprice_akkudoktor = ElecPriceAkkudoktor() elecprice_akkudoktor = ElecPriceAkkudoktor()
elecprice_energy_charts = ElecPriceEnergyCharts()
elecprice_import = ElecPriceImport() elecprice_import = ElecPriceImport()
load_akkudoktor = LoadAkkudoktor() load_akkudoktor = LoadAkkudoktor()
load_vrm = LoadVrm()
load_import = LoadImport() load_import = LoadImport()
pvforecast_akkudoktor = PVForecastAkkudoktor() pvforecast_akkudoktor = PVForecastAkkudoktor()
pvforecast_vrm = PVForecastVrm()
pvforecast_import = PVForecastImport() pvforecast_import = PVForecastImport()
weather_brightsky = WeatherBrightSky() weather_brightsky = WeatherBrightSky()
weather_clearoutside = WeatherClearOutside() weather_clearoutside = WeatherClearOutside()
@@ -123,13 +114,10 @@ def get_prediction() -> Prediction:
prediction = Prediction( prediction = Prediction(
providers=[ providers=[
elecprice_akkudoktor, elecprice_akkudoktor,
elecprice_energy_charts,
elecprice_import, elecprice_import,
load_akkudoktor, load_akkudoktor,
load_vrm,
load_import, load_import,
pvforecast_akkudoktor, pvforecast_akkudoktor,
pvforecast_vrm,
pvforecast_import, pvforecast_import,
weather_brightsky, weather_brightsky,
weather_clearoutside, weather_clearoutside,

View File

@@ -10,7 +10,6 @@ and manipulation of configuration and prediction data in a clear, scalable, and
from typing import List, Optional from typing import List, Optional
from loguru import logger
from pendulum import DateTime from pendulum import DateTime
from pydantic import Field, computed_field from pydantic import Field, computed_field
@@ -23,8 +22,11 @@ from akkudoktoreos.core.dataabc import (
DataRecord, DataRecord,
DataSequence, DataSequence,
) )
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.utils.datetimeutil import to_duration from akkudoktoreos.utils.datetimeutil import to_duration
logger = get_logger(__name__)
class PredictionBase(DataBase, MeasurementMixin): class PredictionBase(DataBase, MeasurementMixin):
"""Base class for handling prediction data. """Base class for handling prediction data.

View File

@@ -1,24 +1,15 @@
"""PV forecast module for PV power predictions.""" """PV forecast module for PV power predictions."""
from typing import Any, List, Optional, Self, Union from typing import Any, ClassVar, List, Optional, Self
from pydantic import Field, computed_field, field_validator, model_validator from pydantic import Field, computed_field, field_validator, model_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.prediction.prediction import get_prediction from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.pvforecastabc import PVForecastProvider
from akkudoktoreos.prediction.pvforecastimport import PVForecastImportCommonSettings from akkudoktoreos.prediction.pvforecastimport import PVForecastImportCommonSettings
from akkudoktoreos.prediction.pvforecastvrm import PVforecastVrmCommonSettings
from akkudoktoreos.utils.docs import get_model_structure_from_examples from akkudoktoreos.utils.docs import get_model_structure_from_examples
prediction_eos = get_prediction() logger = get_logger(__name__)
# Valid PV forecast providers
pvforecast_providers = [
provider.provider_id()
for provider in prediction_eos.providers
if isinstance(provider, PVForecastProvider)
]
class PVForecastPlaneSetting(SettingsBaseModel): class PVForecastPlaneSetting(SettingsBaseModel):
@@ -26,18 +17,14 @@ class PVForecastPlaneSetting(SettingsBaseModel):
# latitude: Optional[float] = Field(default=None, description="Latitude in decimal degrees, between -90 and 90, north is positive (ISO 19115) (°)") # latitude: Optional[float] = Field(default=None, description="Latitude in decimal degrees, between -90 and 90, north is positive (ISO 19115) (°)")
surface_tilt: Optional[float] = Field( surface_tilt: Optional[float] = Field(
default=30.0, default=None,
ge=0.0,
le=90.0,
description="Tilt angle from horizontal plane. Ignored for two-axis tracking.", description="Tilt angle from horizontal plane. Ignored for two-axis tracking.",
examples=[10.0, 20.0], examples=[10.0, 20.0],
) )
surface_azimuth: Optional[float] = Field( surface_azimuth: Optional[float] = Field(
default=180.0, default=None,
ge=0.0,
le=360.0,
description="Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, west=270).", description="Orientation (azimuth angle) of the (fixed) plane. Clockwise from north (north=0, east=90, south=180, west=270).",
examples=[180.0, 90.0], examples=[10.0, 20.0],
) )
userhorizon: Optional[List[float]] = Field( userhorizon: Optional[List[float]] = Field(
default=None, default=None,
@@ -84,7 +71,7 @@ class PVForecastPlaneSetting(SettingsBaseModel):
default=None, description="Model of the inverter of this plane.", examples=[None] default=None, description="Model of the inverter of this plane.", examples=[None]
) )
inverter_paco: Optional[int] = Field( inverter_paco: Optional[int] = Field(
default=None, description="AC power rating of the inverter [W].", examples=[6000, 4000] default=None, description="AC power rating of the inverter. [W]", examples=[6000, 4000]
) )
modules_per_string: Optional[int] = Field( modules_per_string: Optional[int] = Field(
default=None, default=None,
@@ -135,31 +122,25 @@ class PVForecastCommonSettings(SettingsBaseModel):
examples=["PVForecastAkkudoktor"], examples=["PVForecastAkkudoktor"],
) )
provider_settings: Optional[
Union[PVForecastImportCommonSettings, PVforecastVrmCommonSettings]
] = Field(default=None, description="Provider settings", examples=[None])
planes: Optional[list[PVForecastPlaneSetting]] = Field( planes: Optional[list[PVForecastPlaneSetting]] = Field(
default=None, default=None,
description="Plane configuration.", description="Plane configuration.",
examples=[get_model_structure_from_examples(PVForecastPlaneSetting, True)], examples=[get_model_structure_from_examples(PVForecastPlaneSetting, True)],
) )
max_planes: Optional[int] = Field( max_planes: ClassVar[int] = 6 # Maximum number of planes that can be set
default=0,
ge=0,
description="Maximum number of planes that can be set",
)
# Validators @field_validator("planes")
@field_validator("provider", mode="after") def validate_planes(
@classmethod cls, planes: Optional[list[PVForecastPlaneSetting]]
def validate_provider(cls, value: Optional[str]) -> Optional[str]: ) -> Optional[list[PVForecastPlaneSetting]]:
if value is None or value in pvforecast_providers: if planes is not None and len(planes) > cls.max_planes:
return value raise ValueError(f"Maximum number of supported planes: {cls.max_planes}.")
raise ValueError( return planes
f"Provider '{value}' is not a valid PV forecast provider: {pvforecast_providers}."
) provider_settings: Optional[PVForecastImportCommonSettings] = Field(
default=None, description="Provider settings", examples=[None]
)
## Computed fields ## Computed fields
@computed_field # type: ignore[prop-decorator] @computed_field # type: ignore[prop-decorator]

View File

@@ -7,11 +7,13 @@ Notes:
from abc import abstractmethod from abc import abstractmethod
from typing import List, Optional from typing import List, Optional
from loguru import logger
from pydantic import Field from pydantic import Field
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord
logger = get_logger(__name__)
class PVForecastDataRecord(PredictionRecord): class PVForecastDataRecord(PredictionRecord):
"""Represents a pvforecast data record containing various pvforecast attributes at a specific datetime.""" """Represents a pvforecast data record containing various pvforecast attributes at a specific datetime."""

View File

@@ -27,14 +27,14 @@ Example:
"planes": [ "planes": [
{ {
"peakpower": 5.0, "peakpower": 5.0,
"surface_azimuth": 170, "surface_azimuth": -10,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [20, 27, 22, 20], "userhorizon": [20, 27, 22, 20],
"inverter_paco": 10000, "inverter_paco": 10000,
}, },
{ {
"peakpower": 4.8, "peakpower": 4.8,
"surface_azimuth": 90, "surface_azimuth": -90,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [30, 30, 30, 50], "userhorizon": [30, 30, 30, 50],
"inverter_paco": 10000, "inverter_paco": 10000,
@@ -78,10 +78,10 @@ Methods:
from typing import Any, List, Optional, Union from typing import Any, List, Optional, Union
import requests import requests
from loguru import logger from pydantic import Field, ValidationError, computed_field
from pydantic import Field, ValidationError, computed_field, field_validator
from akkudoktoreos.core.cache import cache_in_file from akkudoktoreos.core.cache import cache_in_file
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticBaseModel from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.pvforecastabc import ( from akkudoktoreos.prediction.pvforecastabc import (
PVForecastDataRecord, PVForecastDataRecord,
@@ -89,11 +89,13 @@ from akkudoktoreos.prediction.pvforecastabc import (
) )
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime
logger = get_logger(__name__)
class AkkudoktorForecastHorizon(PydanticBaseModel): class AkkudoktorForecastHorizon(PydanticBaseModel):
altitude: int altitude: int
azimuthFrom: float azimuthFrom: int
azimuthTo: float azimuthTo: int
class AkkudoktorForecastMeta(PydanticBaseModel): class AkkudoktorForecastMeta(PydanticBaseModel):
@@ -112,30 +114,6 @@ class AkkudoktorForecastMeta(PydanticBaseModel):
horizont: List[List[AkkudoktorForecastHorizon]] horizont: List[List[AkkudoktorForecastHorizon]]
horizontString: List[str] horizontString: List[str]
@field_validator("power", "azimuth", "tilt", "powerInverter", mode="before")
@classmethod
def ensure_list(cls, v: Any) -> List[int]:
return v if isinstance(v, list) else [v]
@field_validator("horizont", mode="before")
@classmethod
def normalize_horizont(cls, v: Any) -> List[List[AkkudoktorForecastHorizon]]:
if isinstance(v, list):
# Case: flat list of dicts
if v and isinstance(v[0], dict):
return [v]
# Already in correct nested form
if v and isinstance(v[0], list):
return v
return v
@field_validator("horizontString", mode="before")
@classmethod
def parse_horizont_string(cls, v: Any) -> List[str]:
if isinstance(v, str):
return [s.strip() for s in v.split(",")]
return v
class AkkudoktorForecastValue(PydanticBaseModel): class AkkudoktorForecastValue(PydanticBaseModel):
datetime: str datetime: str
@@ -243,18 +221,13 @@ class PVForecastAkkudoktor(PVForecastProvider):
for i in range(len(self.config.pvforecast.planes)): for i in range(len(self.config.pvforecast.planes)):
query_params.append(f"power={int(self.config.pvforecast.planes_peakpower[i] * 1000)}") query_params.append(f"power={int(self.config.pvforecast.planes_peakpower[i] * 1000)}")
# EOS orientation of of pv modules in azimuth in degree: query_params.append(f"azimuth={int(self.config.pvforecast.planes_azimuth[i])}")
# north=0, east=90, south=180, west=270
# Akkudoktor orientation of pv modules in azimuth in degree:
# north=+-180, east=-90, south=0, west=90
azimuth_akkudoktor = int(self.config.pvforecast.planes_azimuth[i]) - 180
query_params.append(f"azimuth={azimuth_akkudoktor}")
query_params.append(f"tilt={int(self.config.pvforecast.planes_tilt[i])}") query_params.append(f"tilt={int(self.config.pvforecast.planes_tilt[i])}")
query_params.append( query_params.append(
f"powerInverter={int(self.config.pvforecast.planes_inverter_paco[i])}" f"powerInverter={int(self.config.pvforecast.planes_inverter_paco[i])}"
) )
horizon_values = ",".join( horizon_values = ",".join(
str(round(h)) for h in self.config.pvforecast.planes_userhorizon[i] str(int(h)) for h in self.config.pvforecast.planes_userhorizon[i]
) )
query_params.append(f"horizont={horizon_values}") query_params.append(f"horizont={horizon_values}")
@@ -289,7 +262,7 @@ class PVForecastAkkudoktor(PVForecastProvider):
Raises: Raises:
ValueError: If the API response does not include expected `meta` data. ValueError: If the API response does not include expected `meta` data.
""" """
response = requests.get(self._url(), timeout=10) response = requests.get(self._url())
response.raise_for_status() # Raise an error for bad responses response.raise_for_status() # Raise an error for bad responses
logger.debug(f"Response from {self._url()}: {response}") logger.debug(f"Response from {self._url()}: {response}")
akkudoktor_data = self._validate_data(response.content) akkudoktor_data = self._validate_data(response.content)
@@ -330,8 +303,7 @@ class PVForecastAkkudoktor(PVForecastProvider):
logger.error(f"Akkudoktor schema change: {error_msg}") logger.error(f"Akkudoktor schema change: {error_msg}")
raise ValueError(error_msg) raise ValueError(error_msg)
if not self.start_datetime: assert self.start_datetime # mypy fix
raise ValueError(f"Start DateTime not set: {self.start_datetime}")
# Iterate over forecast data points # Iterate over forecast data points
for forecast_values in zip(*akkudoktor_data.values): for forecast_values in zip(*akkudoktor_data.values):
@@ -418,28 +390,28 @@ if __name__ == "__main__":
"planes": [ "planes": [
{ {
"peakpower": 5.0, "peakpower": 5.0,
"surface_azimuth": 170, "surface_azimuth": -10,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [20, 27, 22, 20], "userhorizon": [20, 27, 22, 20],
"inverter_paco": 10000, "inverter_paco": 10000,
}, },
{ {
"peakpower": 4.8, "peakpower": 4.8,
"surface_azimuth": 90, "surface_azimuth": -90,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [30, 30, 30, 50], "userhorizon": [30, 30, 30, 50],
"inverter_paco": 10000, "inverter_paco": 10000,
}, },
{ {
"peakpower": 1.4, "peakpower": 1.4,
"surface_azimuth": 140, "surface_azimuth": -40,
"surface_tilt": 60, "surface_tilt": 60,
"userhorizon": [60, 30, 0, 30], "userhorizon": [60, 30, 0, 30],
"inverter_paco": 2000, "inverter_paco": 2000,
}, },
{ {
"peakpower": 1.6, "peakpower": 1.6,
"surface_azimuth": 185, "surface_azimuth": 5,
"surface_tilt": 45, "surface_tilt": 45,
"userhorizon": [45, 25, 30, 60], "userhorizon": [45, 25, 30, 60],
"inverter_paco": 1400, "inverter_paco": 1400,

View File

@@ -9,13 +9,15 @@ format, enabling consistent access to forecasted and historical pvforecast attri
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
from loguru import logger
from pydantic import Field, field_validator from pydantic import Field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.predictionabc import PredictionImportProvider from akkudoktoreos.prediction.predictionabc import PredictionImportProvider
from akkudoktoreos.prediction.pvforecastabc import PVForecastProvider from akkudoktoreos.prediction.pvforecastabc import PVForecastProvider
logger = get_logger(__name__)
class PVForecastImportCommonSettings(SettingsBaseModel): class PVForecastImportCommonSettings(SettingsBaseModel):
"""Common settings for pvforecast data import from file or JSON string.""" """Common settings for pvforecast data import from file or JSON string."""

View File

@@ -1,110 +0,0 @@
"""Retrieves pvforecast data from VRM API."""
from typing import Any, Optional, Union
import requests
from loguru import logger
from pendulum import DateTime
from pydantic import Field, ValidationError
from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.pvforecastabc import PVForecastProvider
from akkudoktoreos.utils.datetimeutil import to_datetime
class VrmForecastRecords(PydanticBaseModel):
vrm_consumption_fc: list[tuple[int, float]]
solar_yield_forecast: list[tuple[int, float]]
class VrmForecastResponse(PydanticBaseModel):
success: bool
records: VrmForecastRecords
totals: dict
class PVforecastVrmCommonSettings(SettingsBaseModel):
"""Common settings for VRM API."""
pvforecast_vrm_token: str = Field(
default="your-token", description="Token for Connecting VRM API", examples=["your-token"]
)
pvforecast_vrm_idsite: int = Field(
default=12345, description="VRM-Installation-ID", examples=[12345]
)
class PVForecastVrm(PVForecastProvider):
"""Fetch and process PV forecast data from VRM API."""
@classmethod
def provider_id(cls) -> str:
"""Return the unique identifier for the PV-Forecast-Provider."""
return "PVForecastVrm"
@classmethod
def _validate_data(cls, json_str: Union[bytes, Any]) -> VrmForecastResponse:
"""Validate the VRM forecast response data against the expected schema."""
try:
return VrmForecastResponse.model_validate_json(json_str)
except ValidationError as e:
error_msg = "\n".join(
f"Field: {' -> '.join(str(x) for x in err['loc'])}\n"
f"Error: {err['msg']}\nType: {err['type']}"
for err in e.errors()
)
logger.error(f"VRM-API schema change:\n{error_msg}")
raise ValueError(error_msg)
def _request_forecast(self, start_ts: int, end_ts: int) -> VrmForecastResponse:
"""Fetch forecast data from Victron VRM API."""
source = "https://vrmapi.victronenergy.com/v2/installations"
id_site = self.config.pvforecast.provider_settings.pvforecast_vrm_idsite
api_token = self.config.pvforecast.provider_settings.pvforecast_vrm_token
headers = {"X-Authorization": f"Token {api_token}", "Content-Type": "application/json"}
url = f"{source}/{id_site}/stats?type=forecast&start={start_ts}&end={end_ts}&interval=hours"
logger.debug(f"Requesting VRM forecast: {url}")
try:
response = requests.get(url, headers=headers, timeout=30)
response.raise_for_status()
except requests.RequestException as e:
logger.error(f"Failed to fetch pvforecast: {e}")
raise RuntimeError("Failed to fetch pvforecast from VRM API") from e
self.update_datetime = to_datetime(in_timezone=self.config.general.timezone)
return self._validate_data(response.content)
def _ts_to_datetime(self, timestamp: int) -> DateTime:
"""Convert UNIX ms timestamp to timezone-aware datetime."""
return to_datetime(timestamp / 1000, in_timezone=self.config.general.timezone)
def _update_data(self, force_update: Optional[bool] = False) -> None:
"""Update forecast data in the PVForecastDataRecord format."""
start_date = self.start_datetime.start_of("day")
end_date = self.start_datetime.add(hours=self.config.prediction.hours)
start_ts = int(start_date.timestamp())
end_ts = int(end_date.timestamp())
logger.info(f"Updating PV forecast from VRM: {start_date} to {end_date}")
vrm_forecast_data = self._request_forecast(start_ts, end_ts)
pv_forecast = []
for timestamp, value in vrm_forecast_data.records.solar_yield_forecast:
date = self._ts_to_datetime(timestamp)
dc_power = round(value, 2)
ac_power = round(dc_power * 0.96, 2)
self.update_value(
date, {"pvforecast_dc_power": dc_power, "pvforecast_ac_power": ac_power}
)
pv_forecast.append((date, dc_power))
logger.debug(f"Updated pvforecast_dc_power with {len(pv_forecast)} entries.")
self.update_datetime = to_datetime(in_timezone=self.config.general.timezone)
# Example usage
if __name__ == "__main__":
pv = PVForecastVrm()
pv._update_data()

View File

@@ -2,22 +2,11 @@
from typing import Optional from typing import Optional
from pydantic import Field, field_validator from pydantic import Field
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.prediction.prediction import get_prediction
from akkudoktoreos.prediction.weatherabc import WeatherProvider
from akkudoktoreos.prediction.weatherimport import WeatherImportCommonSettings from akkudoktoreos.prediction.weatherimport import WeatherImportCommonSettings
prediction_eos = get_prediction()
# Valid weather providers
weather_providers = [
provider.provider_id()
for provider in prediction_eos.providers
if isinstance(provider, WeatherProvider)
]
class WeatherCommonSettings(SettingsBaseModel): class WeatherCommonSettings(SettingsBaseModel):
"""Weather Forecast Configuration.""" """Weather Forecast Configuration."""
@@ -31,13 +20,3 @@ class WeatherCommonSettings(SettingsBaseModel):
provider_settings: Optional[WeatherImportCommonSettings] = Field( provider_settings: Optional[WeatherImportCommonSettings] = Field(
default=None, description="Provider settings", examples=[None] default=None, description="Provider settings", examples=[None]
) )
# Validators
@field_validator("provider", mode="after")
@classmethod
def validate_provider(cls, value: Optional[str]) -> Optional[str]:
if value is None or value in weather_providers:
return value
raise ValueError(
f"Provider '{value}' is not a valid weather provider: {weather_providers}."
)

View File

@@ -14,8 +14,11 @@ import pandas as pd
import pvlib import pvlib
from pydantic import Field from pydantic import Field
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord from akkudoktoreos.prediction.predictionabc import PredictionProvider, PredictionRecord
logger = get_logger(__name__)
class WeatherDataRecord(PredictionRecord): class WeatherDataRecord(PredictionRecord):
"""Represents a weather data record containing various weather attributes at a specific datetime. """Represents a weather data record containing various weather attributes at a specific datetime.

View File

@@ -7,21 +7,23 @@ format, enabling consistent access to forecasted and historical weather attribut
""" """
import json import json
from typing import Dict, List, Optional, Tuple, Union from typing import Dict, List, Optional, Tuple
import numpy as np
import pandas as pd import pandas as pd
import pvlib import pvlib
import requests import requests
from loguru import logger
from akkudoktoreos.core.cache import cache_in_file from akkudoktoreos.core.cache import cache_in_file
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.weatherabc import WeatherDataRecord, WeatherProvider from akkudoktoreos.prediction.weatherabc import WeatherDataRecord, WeatherProvider
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import to_datetime
WheaterDataBrightSkyMapping: List[Tuple[str, Optional[str], Optional[Union[str, float]]]] = [ logger = get_logger(__name__)
WheaterDataBrightSkyMapping: List[Tuple[str, Optional[str], Optional[float]]] = [
# brightsky_key, description, corr_factor # brightsky_key, description, corr_factor
("timestamp", "DateTime", "to datetime in timezone"), ("timestamp", "DateTime", None),
("precipitation", "Precipitation Amount (mm)", 1), ("precipitation", "Precipitation Amount (mm)", 1),
("pressure_msl", "Pressure (mb)", 1), ("pressure_msl", "Pressure (mb)", 1),
("sunshine", None, None), ("sunshine", None, None),
@@ -94,11 +96,10 @@ class WeatherBrightSky(WeatherProvider):
ValueError: If the API response does not include expected `weather` data. ValueError: If the API response does not include expected `weather` data.
""" """
source = "https://api.brightsky.dev" source = "https://api.brightsky.dev"
date = to_datetime(self.start_datetime, as_string=True) date = to_datetime(self.start_datetime, as_string="YYYY-MM-DD")
last_date = to_datetime(self.end_datetime, as_string=True) last_date = to_datetime(self.end_datetime, as_string="YYYY-MM-DD")
response = requests.get( response = requests.get(
f"{source}/weather?lat={self.config.general.latitude}&lon={self.config.general.longitude}&date={date}&last_date={last_date}&tz={self.config.general.timezone}", f"{source}/weather?lat={self.config.general.latitude}&lon={self.config.general.longitude}&date={date}&last_date={last_date}&tz={self.config.general.timezone}"
timeout=10,
) )
response.raise_for_status() # Raise an error for bad responses response.raise_for_status() # Raise an error for bad responses
logger.debug(f"Response from {source}: {response}") logger.debug(f"Response from {source}: {response}")
@@ -132,8 +133,7 @@ class WeatherBrightSky(WeatherProvider):
error_msg = f"No WeatherDataRecord key for '{description}'" error_msg = f"No WeatherDataRecord key for '{description}'"
logger.error(error_msg) logger.error(error_msg)
raise ValueError(error_msg) raise ValueError(error_msg)
series = self.key_to_series(key) return self.key_to_series(key)
return series
def _description_from_series(self, description: str, data: pd.Series) -> None: def _description_from_series(self, description: str, data: pd.Series) -> None:
"""Update a weather data with a pandas Series based on its description. """Update a weather data with a pandas Series based on its description.
@@ -170,7 +170,7 @@ class WeatherBrightSky(WeatherProvider):
brightsky_data = self._request_forecast(force_update=force_update) # type: ignore brightsky_data = self._request_forecast(force_update=force_update) # type: ignore
# Get key mapping from description # Get key mapping from description
brightsky_key_mapping: Dict[str, Tuple[Optional[str], Optional[Union[str, float]]]] = {} brightsky_key_mapping: Dict[str, Tuple[Optional[str], Optional[float]]] = {}
for brightsky_key, description, corr_factor in WheaterDataBrightSkyMapping: for brightsky_key, description, corr_factor in WheaterDataBrightSkyMapping:
if description is None: if description is None:
brightsky_key_mapping[brightsky_key] = (None, None) brightsky_key_mapping[brightsky_key] = (None, None)
@@ -192,10 +192,7 @@ class WeatherBrightSky(WeatherProvider):
value = brightsky_record[brightsky_key] value = brightsky_record[brightsky_key]
corr_factor = item[1] corr_factor = item[1]
if value and corr_factor: if value and corr_factor:
if corr_factor == "to datetime in timezone": value = value * corr_factor
value = to_datetime(value, in_timezone=self.config.general.timezone)
else:
value = value * corr_factor
setattr(weather_record, key, value) setattr(weather_record, key, value)
self.insert_by_datetime(weather_record) self.insert_by_datetime(weather_record)
@@ -219,40 +216,14 @@ class WeatherBrightSky(WeatherProvider):
self._description_from_series(description, dhi) self._description_from_series(description, dhi)
# Add Preciptable Water (PWAT) with a PVLib method. # Add Preciptable Water (PWAT) with a PVLib method.
key = WeatherDataRecord.key_from_description("Temperature (°C)") description = "Temperature (°C)"
assert key # noqa: S101 temperature = self._description_to_series(description)
temperature = self.key_to_array(
key=key, description = "Relative Humidity (%)"
start_datetime=self.start_datetime, humidity = self._description_to_series(description)
end_datetime=self.end_datetime,
interval=to_duration("1 hour"),
)
if any(x is None or isinstance(x, float) and np.isnan(x) for x in temperature):
# We can not calculate PWAT
debug_msg = f"Innvalid temperature '{temperature}'"
logger.debug(debug_msg)
return
key = WeatherDataRecord.key_from_description("Relative Humidity (%)")
assert key # noqa: S101
humidity = self.key_to_array(
key=key,
start_datetime=self.start_datetime,
end_datetime=self.end_datetime,
interval=to_duration("1 hour"),
)
if any(x is None or isinstance(x, float) and np.isnan(x) for x in humidity):
# We can not calculate PWAT
debug_msg = f"Innvalid humidity '{humidity}'"
logger.debug(debug_msg)
return
data = pvlib.atmosphere.gueymard94_pw(temperature, humidity)
pwat = pd.Series( pwat = pd.Series(
data=data, data=pvlib.atmosphere.gueymard94_pw(temperature, humidity), index=temperature.index
index=pd.DatetimeIndex(
pd.date_range(
start=self.start_datetime, end=self.end_datetime, freq="1h", inclusive="left"
)
),
) )
description = "Preciptable Water (cm)" description = "Preciptable Water (cm)"
self._description_from_series(description, pwat) self._description_from_series(description, pwat)

View File

@@ -18,12 +18,15 @@ from typing import Dict, List, Optional, Tuple
import pandas as pd import pandas as pd
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from loguru import logger
from akkudoktoreos.core.cache import cache_in_file from akkudoktoreos.core.cache import cache_in_file
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.weatherabc import WeatherDataRecord, WeatherProvider from akkudoktoreos.prediction.weatherabc import WeatherDataRecord, WeatherProvider
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration, to_timezone from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration, to_timezone
logger = get_logger(__name__)
WheaterDataClearOutsideMapping: List[Tuple[str, Optional[str], Optional[float]]] = [ WheaterDataClearOutsideMapping: List[Tuple[str, Optional[str], Optional[float]]] = [
# clearoutside_key, description, corr_factor # clearoutside_key, description, corr_factor
("DateTime", "DateTime", None), ("DateTime", "DateTime", None),
@@ -85,12 +88,12 @@ class WeatherClearOutside(WeatherProvider):
"""Requests weather forecast from ClearOutside. """Requests weather forecast from ClearOutside.
Returns: Returns:
response: Weather forecast request response from ClearOutside. response: Weather forecast request reponse from ClearOutside.
""" """
source = "https://clearoutside.com/forecast" source = "https://clearoutside.com/forecast"
latitude = round(self.config.general.latitude, 2) latitude = round(self.config.general.latitude, 2)
longitude = round(self.config.general.longitude, 2) longitude = round(self.config.general.longitude, 2)
response = requests.get(f"{source}/{latitude}/{longitude}?desktop=true", timeout=10) response = requests.get(f"{source}/{latitude}/{longitude}?desktop=true")
response.raise_for_status() # Raise an error for bad responses response.raise_for_status() # Raise an error for bad responses
logger.debug(f"Response from {source}: {response}") logger.debug(f"Response from {source}: {response}")
# We are working on fresh data (no cache), report update time # We are working on fresh data (no cache), report update time

View File

@@ -9,13 +9,15 @@ format, enabling consistent access to forecasted and historical weather attribut
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
from loguru import logger
from pydantic import Field, field_validator from pydantic import Field, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.prediction.predictionabc import PredictionImportProvider from akkudoktoreos.prediction.predictionabc import PredictionImportProvider
from akkudoktoreos.prediction.weatherabc import WeatherProvider from akkudoktoreos.prediction.weatherabc import WeatherProvider
logger = get_logger(__name__)
class WeatherImportCommonSettings(SettingsBaseModel): class WeatherImportCommonSettings(SettingsBaseModel):
"""Common settings for weather data import from file or JSON string.""" """Common settings for weather data import from file or JSON string."""

View File

@@ -1,297 +0,0 @@
"""Admin UI components for EOS Dashboard.
This module provides functions to generate administrative UI components
for the EOS dashboard.
"""
import json
from pathlib import Path
from typing import Any, Optional, Union
import requests
from fasthtml.common import Select
from loguru import logger
from monsterui.foundations import stringify
from monsterui.franken import ( # Select, TODO: Select from FrankenUI does not work - using Select from FastHTML instead
H3,
Button,
ButtonT,
Card,
Details,
Div,
DivHStacked,
DividerLine,
Grid,
Input,
Options,
P,
Summary,
UkIcon,
)
from platformdirs import user_config_dir
from akkudoktoreos.server.dash.components import Error, Success
from akkudoktoreos.server.dash.configuration import get_nested_value
from akkudoktoreos.utils.datetimeutil import to_datetime
# Directory to export files to, or to import files from
export_import_directory = Path(user_config_dir("net.akkudoktor.eosdash", "akkudoktor"))
def AdminButton(*c: Any, cls: Optional[Union[str, tuple]] = None, **kwargs: Any) -> Button:
"""Creates a styled button for administrative actions.
Args:
*c (Any): Positional arguments representing the button's content.
cls (Optional[Union[str, tuple]]): Additional CSS classes for styling. Defaults to None.
**kwargs (Any): Additional keyword arguments passed to the `Button`.
Returns:
Button: A styled `Button` component for admin actions.
"""
new_cls = f"{ButtonT.primary}"
if cls:
new_cls += f" {stringify(cls)}"
kwargs["cls"] = new_cls
return Button(*c, submit=False, **kwargs)
def AdminConfig(
eos_host: str, eos_port: Union[str, int], data: Optional[dict], config: Optional[dict[str, Any]]
) -> tuple[str, Union[Card, list[Card]]]:
"""Creates a configuration management card with save-to-file functionality.
Args:
eos_host (str): The hostname of the EOS server.
eos_port (Union[str, int]): The port of the EOS server.
data (Optional[dict]): Incoming data containing action and category for processing.
Returns:
tuple[str, Union[Card, list[Card]]]: A tuple containing the configuration category label and the `Card` UI component.
"""
server = f"http://{eos_host}:{eos_port}"
eos_hostname = "EOS server"
eosdash_hostname = "EOSdash server"
category = "configuration"
# save config file
status = (None,)
config_file_path = "<unknown>"
try:
if config:
config_file_path = get_nested_value(config, ["general", "config_file_path"])
except Exception as e:
logger.debug(f"general.config_file_path: {e}")
# export config file
export_to_file_next_tag = to_datetime(as_string="YYYYMMDDHHmmss")
export_to_file_status = (None,)
# import config file
import_from_file_status = (None,)
if data and data.get("category", None) == category:
# This data is for us
if data["action"] == "save_to_file":
# Safe current configuration to file
try:
result = requests.put(f"{server}/v1/config/file", timeout=10)
result.raise_for_status()
config_file_path = result.json()["general"]["config_file_path"]
status = Success(f"Saved to '{config_file_path}' on '{eos_hostname}'")
except requests.exceptions.HTTPError as e:
detail = result.json()["detail"]
status = Error(
f"Can not save actual config to file on '{eos_hostname}': {e}, {detail}"
)
except Exception as e:
status = Error(f"Can not save actual config to file on '{eos_hostname}': {e}")
elif data["action"] == "export_to_file":
# Export current configuration to file
export_to_file_tag = data.get("export_to_file_tag", export_to_file_next_tag)
export_to_file_path = export_import_directory.joinpath(
f"eos_config_{export_to_file_tag}.json"
)
try:
if not config:
raise ValueError(f"No config from '{eos_hostname}'")
export_to_file_path.parent.mkdir(parents=True, exist_ok=True)
with export_to_file_path.open("w", encoding="utf-8", newline="\n") as fd:
json.dump(config, fd, indent=4, sort_keys=True)
export_to_file_status = Success(
f"Exported to '{export_to_file_path}' on '{eosdash_hostname}'"
)
except requests.exceptions.HTTPError as e:
detail = result.json()["detail"]
export_to_file_status = Error(
f"Can not export actual config to '{export_to_file_path}' on '{eosdash_hostname}': {e}, {detail}"
)
except Exception as e:
export_to_file_status = Error(
f"Can not export actual config to '{export_to_file_path}' on '{eosdash_hostname}': {e}"
)
elif data["action"] == "import_from_file":
import_file_name = data.get("import_file_name", None)
import_from_file_pathes = list(
export_import_directory.glob("*.json")
) # expand generator object
import_file_path = None
for f in import_from_file_pathes:
if f.name == import_file_name:
import_file_path = f
if import_file_path:
try:
with import_file_path.open("r", encoding="utf-8", newline=None) as fd:
import_config = json.load(fd)
result = requests.put(f"{server}/v1/config", json=import_config, timeout=10)
result.raise_for_status()
import_from_file_status = Success(
f"Config imported from '{import_file_path}' on '{eosdash_hostname}'"
)
except requests.exceptions.HTTPError as e:
detail = result.json()["detail"]
import_from_file_status = Error(
f"Can not import config from '{import_file_name}' on '{eosdash_hostname}' {e}, {detail}"
)
except Exception as e:
import_from_file_status = Error(
f"Can not import config from '{import_file_name}' on '{eosdash_hostname}' {e}"
)
else:
import_from_file_status = Error(
f"Can not import config from '{import_file_name}', not found in '{export_import_directory}' on '{eosdash_hostname}'"
)
# Update for display, in case we added a new file before
import_from_file_names = [f.name for f in list(export_import_directory.glob("*.json"))]
return (
category,
[
Card(
Details(
Summary(
Grid(
DivHStacked(
UkIcon(icon="play"),
AdminButton(
"Save to file",
hx_post="/eosdash/admin",
hx_target="#page-content",
hx_swap="innerHTML",
hx_vals='{"category": "configuration", "action": "save_to_file"}',
),
P(f"'{config_file_path}' on '{eos_hostname}'"),
),
status,
),
cls="list-none",
),
P(f"Safe actual configuration to '{config_file_path}' on '{eos_hostname}'."),
),
),
Card(
Details(
Summary(
Grid(
DivHStacked(
UkIcon(icon="play"),
AdminButton(
"Export to file",
hx_post="/eosdash/admin",
hx_target="#page-content",
hx_swap="innerHTML",
hx_vals='js:{"category": "configuration", "action": "export_to_file", "export_to_file_tag": document.querySelector("[name=\'chosen_export_file_tag\']").value }',
),
P("'eos_config_"),
Input(
id="export_file_tag",
name="chosen_export_file_tag",
value=export_to_file_next_tag,
),
P(".json'"),
),
export_to_file_status,
),
cls="list-none",
),
P(
f"Export actual configuration to 'eos_config_{export_to_file_next_tag}.json' on '{eosdash_hostname}'."
),
),
),
Card(
Details(
Summary(
Grid(
DivHStacked(
UkIcon(icon="play"),
AdminButton(
"Import from file",
hx_post="/eosdash/admin",
hx_target="#page-content",
hx_swap="innerHTML",
hx_vals='js:{ "category": "configuration", "action": "import_from_file", "import_file_name": document.querySelector("[name=\'selected_import_file_name\']").value }',
),
Select(
*Options(*import_from_file_names),
id="import_file_name",
name="selected_import_file_name", # Name of hidden input field with selected value
placeholder="Select file",
),
),
import_from_file_status,
),
cls="list-none",
),
P(f"Import configuration from config file on '{eosdash_hostname}'."),
),
),
],
)
def Admin(eos_host: str, eos_port: Union[str, int], data: Optional[dict] = None) -> Div:
"""Generates the administrative dashboard layout.
This includes configuration management and other administrative tools.
Args:
eos_host (str): The hostname of the EOS server.
eos_port (Union[str, int]): The port of the EOS server.
data (Optional[dict], optional): Incoming data to trigger admin actions. Defaults to None.
Returns:
Div: A `Div` component containing the assembled admin interface.
"""
# Get current configuration from server
server = f"http://{eos_host}:{eos_port}"
try:
result = requests.get(f"{server}/v1/config", timeout=10)
result.raise_for_status()
config = result.json()
except requests.exceptions.HTTPError as e:
config = {}
detail = result.json()["detail"]
warning_msg = f"Can not retrieve configuration from {server}: {e}, {detail}"
logger.warning(warning_msg)
return Error(warning_msg)
except Exception as e:
warning_msg = f"Can not retrieve configuration from {server}: {e}"
logger.warning(warning_msg)
return Error(warning_msg)
rows = []
last_category = ""
for category, admin in [
AdminConfig(eos_host, eos_port, data, config),
]:
if category != last_category:
rows.append(H3(category))
rows.append(DividerLine())
last_category = category
if isinstance(admin, list):
for card in admin:
rows.append(card)
else:
rows.append(admin)
return Div(*rows, cls="space-y-4")

View File

@@ -8,19 +8,19 @@ from bokeh.models import Plot
from monsterui.franken import H4, Card, NotStr, Script from monsterui.franken import H4, Card, NotStr, Script
BokehJS = [ BokehJS = [
Script(src="https://cdn.bokeh.org/bokeh/release/bokeh-3.7.0.min.js", crossorigin="anonymous"), Script(src="https://cdn.bokeh.org/bokeh/release/bokeh-3.6.3.min.js", crossorigin="anonymous"),
Script( Script(
src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.7.0.min.js", src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-3.6.3.min.js",
crossorigin="anonymous", crossorigin="anonymous",
), ),
Script( Script(
src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.7.0.min.js", crossorigin="anonymous" src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-3.6.3.min.js", crossorigin="anonymous"
), ),
Script( Script(
src="https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.7.0.min.js", crossorigin="anonymous" src="https://cdn.bokeh.org/bokeh/release/bokeh-gl-3.6.3.min.js", crossorigin="anonymous"
), ),
Script( Script(
src="https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-3.7.0.min.js", src="https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-3.6.3.min.js",
crossorigin="anonymous", crossorigin="anonymous",
), ),
] ]

View File

@@ -1,13 +1,10 @@
from typing import Any, Optional, Union from typing import Any, Optional, Union
from fasthtml.common import H1, Div, Li from fasthtml.common import H1, Div, Li
from monsterui.daisy import (
Alert, # from mdit_py_plugins import plugin1, plugin2
AlertT,
)
from monsterui.foundations import stringify from monsterui.foundations import stringify
from monsterui.franken import ( from monsterui.franken import (
H3,
Button, Button,
ButtonT, ButtonT,
Card, Card,
@@ -16,7 +13,6 @@ from monsterui.franken import (
Details, Details,
DivLAligned, DivLAligned,
DivRAligned, DivRAligned,
Form,
Grid, Grid,
Input, Input,
P, P,
@@ -73,69 +69,9 @@ def ScrollArea(
) )
def Success(*c: Any) -> Alert:
return Alert(
DivLAligned(
UkIcon("check"),
P(*c),
),
cls=AlertT.success,
)
def Error(*c: Any) -> Alert:
return Alert(
DivLAligned(
UkIcon("triangle-alert"),
P(*c),
),
cls=AlertT.error,
)
def ConfigCard( def ConfigCard(
config_name: str, config_name: str, config_type: str, read_only: str, value: str, default: str, description: str
config_type: str,
read_only: str,
value: str,
default: str,
description: str,
deprecated: Optional[Union[str, bool]],
update_error: Optional[str],
update_value: Optional[str],
update_open: Optional[bool],
) -> Card: ) -> Card:
"""Creates a styled configuration card for displaying configuration details.
This function generates a configuration card that is displayed in the UI with
various sections such as configuration name, type, description, default value,
current value, and error details. It supports both read-only and editable modes.
Args:
config_name (str): The name of the configuration.
config_type (str): The type of the configuration.
read_only (str): Indicates if the configuration is read-only ("rw" for read-write,
any other value indicates read-only).
value (str): The current value of the configuration.
default (str): The default value of the configuration.
description (str): A description of the configuration.
deprecated (Optional[Union[str, bool]]): The deprecated marker of the configuration.
update_error (Optional[str]): The error message, if any, during the update process.
update_value (Optional[str]): The value to be updated, if different from the current value.
update_open (Optional[bool]): A flag indicating whether the update section of the card
should be initially expanded.
Returns:
Card: A styled Card component containing the configuration details.
"""
config_id = config_name.replace(".", "-")
if not update_value:
update_value = value
if not update_open:
update_open = False
if deprecated:
if isinstance(deprecated, bool):
deprecated = "Deprecated"
return Card( return Card(
Details( Details(
Summary( Summary(
@@ -149,53 +85,24 @@ def ConfigCard(
P(read_only), P(read_only),
), ),
), ),
P(value), Input(value=value) if read_only == "rw" else P(value),
), ),
# cls="flex cursor-pointer list-none items-center gap-4",
cls="list-none", cls="list-none",
), ),
Grid( Grid(
P(description), P(description),
P(config_type), P(config_type),
) ),
if not deprecated
else None,
Grid( Grid(
P(deprecated), DivRAligned(
P("DEPRECATED!"), P("default") if read_only == "rw" else P(""),
)
if deprecated
else None,
# Default
Grid(
DivRAligned(P("default")),
P(default),
)
if read_only == "rw" and not deprecated
else None,
# Set value
Grid(
DivRAligned(P("update")),
Grid(
Form(
Input(value=config_name, type="hidden", id="key"),
Input(value=update_value, type="text", id="value"),
hx_put="/eosdash/configuration",
hx_target="#page-content",
hx_swap="innerHTML",
),
), ),
P(default) if read_only == "rw" else P(""),
) )
if read_only == "rw" and not deprecated if read_only == "rw"
else None,
# Last error
Grid(
DivRAligned(P("update error")),
P(update_error),
)
if update_error
else None, else None,
cls="space-y-4 gap-4", cls="space-y-4 gap-4",
open=update_open,
), ),
cls="w-full", cls="w-full",
) )
@@ -266,7 +173,7 @@ def DashboardTabs(dashboard_items: dict[str, str]) -> Card:
dash_items = [ dash_items = [
Li( Li(
DashboardTrigger( DashboardTrigger(
H3(menu), menu,
hx_get=f"{path}", hx_get=f"{path}",
hx_target="#page-content", hx_target="#page-content",
hx_swap="innerHTML", hx_swap="innerHTML",

View File

@@ -1,37 +1,19 @@
import json
from typing import Any, Dict, List, Optional, Sequence, TypeVar, Union from typing import Any, Dict, List, Optional, Sequence, TypeVar, Union
import requests import requests
from loguru import logger from monsterui.franken import Div, DividerLine, P, Table, Tbody, Td, Th, Thead, Tr
from monsterui.franken import (
H3,
H4,
Card,
Details,
Div,
DividerLine,
DivLAligned,
DivRAligned,
Form,
Grid,
Input,
P,
Summary,
UkIcon,
)
from pydantic.fields import ComputedFieldInfo, FieldInfo from pydantic.fields import ComputedFieldInfo, FieldInfo
from pydantic_core import PydanticUndefined from pydantic_core import PydanticUndefined
from akkudoktoreos.config.config import ConfigEOS from akkudoktoreos.config.config import get_config
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticBaseModel from akkudoktoreos.core.pydantic import PydanticBaseModel
from akkudoktoreos.prediction.pvforecast import PVForecastPlaneSetting
from akkudoktoreos.server.dash.components import ConfigCard from akkudoktoreos.server.dash.components import ConfigCard
T = TypeVar("T") logger = get_logger(__name__)
config_eos = get_config()
# Latest configuration update results T = TypeVar("T")
# Dictionary of config names and associated dictionary with keys "value", "result", "error", "open".
config_update_latest: dict[str, dict[str, Optional[Union[str, bool]]]] = {}
def get_nested_value( def get_nested_value(
@@ -66,10 +48,10 @@ def get_nested_value(
# Traverse the structure # Traverse the structure
current = dictionary current = dictionary
for key in keys: for key in keys:
if isinstance(current, dict): if isinstance(current, dict) and isinstance(key, str):
current = current[str(key)] current = current[key]
elif isinstance(current, list): elif isinstance(current, list) and isinstance(key, int):
current = current[int(key)] current = current[key]
else: else:
raise KeyError(f"Invalid key or index: {key}") raise KeyError(f"Invalid key or index: {key}")
return current return current
@@ -119,36 +101,25 @@ def resolve_nested_types(field_type: Any, parent_types: list[str]) -> list[tuple
return resolved_types return resolved_types
def configuration( def configuration(values: dict) -> list[dict]:
model: type[PydanticBaseModel], values: dict, values_prefix: list[str] = []
) -> list[dict]:
"""Generate configuration details based on provided values and model metadata. """Generate configuration details based on provided values and model metadata.
Args: Args:
model (type[PydanticBaseModel]): The Pydantic model to extract configuration from.
values (dict): A dictionary containing the current configuration values. values (dict): A dictionary containing the current configuration values.
values_prefix (list[str]): A list of parent type names that prefixes the model values in the values.
Returns: Returns:
list[dict]: A sorted list of configuration details, each represented as a dictionary. List[dict]: A sorted list of configuration details, each represented as a dictionary.
""" """
configs = [] configs = []
inner_types: set[type[PydanticBaseModel]] = set() inner_types: set[type[PydanticBaseModel]] = set()
for field_name, field_info in list(model.model_fields.items()) + list( for field_name, field_info in list(config_eos.model_fields.items()) + list(
model.model_computed_fields.items() config_eos.model_computed_fields.items()
): ):
def extract_nested_models( def extract_nested_models(
subfield_info: Union[ComputedFieldInfo, FieldInfo], parent_types: list[str] subfield_info: Union[ComputedFieldInfo, FieldInfo], parent_types: list[str]
) -> None: ) -> None:
"""Extract nested models from the given subfield information.
Args:
subfield_info (Union[ComputedFieldInfo, FieldInfo]): Field metadata from Pydantic.
parent_types (list[str]): A list of parent type names for hierarchical representation.
"""
nonlocal values, values_prefix
regular_field = isinstance(subfield_info, FieldInfo) regular_field = isinstance(subfield_info, FieldInfo)
subtype = subfield_info.annotation if regular_field else subfield_info.return_type subtype = subfield_info.annotation if regular_field else subfield_info.return_type
@@ -164,18 +135,13 @@ def configuration(
if found_basic: if found_basic:
continue continue
config: dict[str, Optional[Any]] = {} config = {}
config["name"] = ".".join(values_prefix + parent_types) config["name"] = ".".join(parent_types)
config["value"] = json.dumps( config["value"] = str(get_nested_value(values, parent_types, "<unknown>"))
get_nested_value(values, values_prefix + parent_types, "<unknown>") config["default"] = str(get_default_value(subfield_info, regular_field))
)
config["default"] = json.dumps(get_default_value(subfield_info, regular_field))
config["description"] = ( config["description"] = (
subfield_info.description if subfield_info.description else "" subfield_info.description if subfield_info.description else ""
) )
config["deprecated"] = (
subfield_info.deprecated if subfield_info.deprecated else None
)
if isinstance(subfield_info, ComputedFieldInfo): if isinstance(subfield_info, ComputedFieldInfo):
config["read-only"] = "ro" config["read-only"] = "ro"
type_description = str(subfield_info.return_type) type_description = str(subfield_info.return_type)
@@ -185,8 +151,8 @@ def configuration(
config["type"] = ( config["type"] = (
type_description.replace("typing.", "") type_description.replace("typing.", "")
.replace("pathlib.", "") .replace("pathlib.", "")
.replace("[", "[ ")
.replace("NoneType", "None") .replace("NoneType", "None")
.replace("<class 'float'>", "float")
) )
configs.append(config) configs.append(config)
found_basic = True found_basic = True
@@ -205,345 +171,105 @@ def configuration(
return sorted(configs, key=lambda x: x["name"]) return sorted(configs, key=lambda x: x["name"])
def get_configuration(eos_host: str, eos_port: Union[str, int]) -> list[dict]: def get_configuration(eos_host: Optional[str], eos_port: Optional[Union[str, int]]) -> list[dict]:
"""Fetch and process configuration data from the specified EOS server. """Fetch and process configuration data from the specified EOS server.
Args: Args:
eos_host (str): The hostname of the EOS server. eos_host (Optional[str]): The hostname of the server.
eos_port (Union[str, int]): The port of the EOS server. eos_port (Optional[Union[str, int]]): The port of the server.
Returns: Returns:
List[dict]: A list of processed configuration entries. List[dict]: A list of processed configuration entries.
""" """
if eos_host is None:
eos_host = config_eos.server.host
if eos_port is None:
eos_port = config_eos.server.port
server = f"http://{eos_host}:{eos_port}" server = f"http://{eos_host}:{eos_port}"
# Get current configuration from server # Get current configuration from server
try: try:
result = requests.get(f"{server}/v1/config", timeout=10) result = requests.get(f"{server}/v1/config")
result.raise_for_status() result.raise_for_status()
config = result.json()
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
config = {}
detail = result.json()["detail"] detail = result.json()["detail"]
warning_msg = f"Can not retrieve configuration from {server}: {e}, {detail}" warning_msg = f"Can not retrieve configuration from {server}: {e}, {detail}"
logger.warning(warning_msg) logger.warning(warning_msg)
return configuration({})
config = result.json()
return configuration(ConfigEOS, config) return configuration(config)
def ConfigPlanesCard( def Configuration(eos_host: Optional[str], eos_port: Optional[Union[str, int]]) -> Div:
config_name: str,
config_type: str,
read_only: str,
value: str,
default: str,
description: str,
max_planes: int,
update_error: Optional[str],
update_value: Optional[str],
update_open: Optional[bool],
) -> Card:
"""Creates a styled configuration card for PV planes.
This function generates a configuration card that is displayed in the UI with
various sections such as configuration name, type, description, default value,
current value, and error details. It supports both read-only and editable modes.
Args:
config_name (str): The name of the PV planes configuration.
config_type (str): The type of the PV planes configuration.
read_only (str): Indicates if the PV planes configuration is read-only ("rw" for read-write,
any other value indicates read-only).
value (str): The current value of the PV planes configuration.
default (str): The default value of the PV planes configuration.
description (str): A description of the PV planes configuration.
max_planes (int): Maximum number of planes that can be set
update_error (Optional[str]): The error message, if any, during the update process.
update_value (Optional[str]): The value to be updated, if different from the current value.
update_open (Optional[bool]): A flag indicating whether the update section of the card
should be initially expanded.
Returns:
Card: A styled Card component containing the PV planes configuration details.
"""
config_id = config_name.replace(".", "-")
# Remember overall planes update status
planes_update_error = update_error
planes_update_value = update_value
if not planes_update_value:
planes_update_value = value
planes_update_open = update_open
if not planes_update_open:
planes_update_open = False
# Create EOS planes configuration
eos_planes = json.loads(value)
eos_planes_config = {
"pvforecast": {
"planes": eos_planes,
},
}
# Create cards for all planes
rows = []
for i in range(0, max_planes):
plane_config = configuration(
PVForecastPlaneSetting(),
eos_planes_config,
values_prefix=["pvforecast", "planes", str(i)],
)
plane_rows = []
plane_update_open = False
if eos_planes and len(eos_planes) > i:
plane_value = json.dumps(eos_planes[i])
else:
plane_value = json.dumps(None)
for config in plane_config:
update_error = config_update_latest.get(config["name"], {}).get("error") # type: ignore
update_value = config_update_latest.get(config["name"], {}).get("value") # type: ignore
update_open = config_update_latest.get(config["name"], {}).get("open") # type: ignore
if update_open:
planes_update_open = True
plane_update_open = True
# Make mypy happy - should never trigger
if (
not isinstance(update_error, (str, type(None)))
or not isinstance(update_value, (str, type(None)))
or not isinstance(update_open, (bool, type(None)))
):
error_msg = "update_error or update_value or update_open of wrong type."
logger.error(error_msg)
raise TypeError(error_msg)
plane_rows.append(
ConfigCard(
config["name"],
config["type"],
config["read-only"],
config["value"],
config["default"],
config["description"],
config["deprecated"],
update_error,
update_value,
update_open,
)
)
rows.append(
Card(
Details(
Summary(
Grid(
Grid(
DivLAligned(
UkIcon(icon="play"),
H4(f"pvforecast.planes.{i}"),
),
DivRAligned(
P(read_only),
),
),
P(plane_value),
),
cls="list-none",
),
*plane_rows,
cls="space-y-4 gap-4",
open=plane_update_open,
),
cls="w-full",
)
)
return Card(
Details(
Summary(
Grid(
Grid(
DivLAligned(
UkIcon(icon="play"),
P(config_name),
),
DivRAligned(
P(read_only),
),
),
P(value),
),
cls="list-none",
),
Grid(
P(description),
P(config_type),
),
# Default
Grid(
DivRAligned(P("default")),
P(default),
)
if read_only == "rw"
else None,
# Set value
Grid(
DivRAligned(P("update")),
Grid(
Form(
Input(value=config_name, type="hidden", id="key"),
Input(value=planes_update_value, type="text", id="value"),
hx_put="/eosdash/configuration",
hx_target="#page-content",
hx_swap="innerHTML",
),
),
)
if read_only == "rw"
else None,
# Last error
Grid(
DivRAligned(P("update error")),
P(planes_update_error),
)
if planes_update_error
else None,
# Now come the single element configs
*rows,
cls="space-y-4 gap-4",
open=planes_update_open,
),
cls="w-full",
)
def Configuration(
eos_host: str, eos_port: Union[str, int], configuration: Optional[list[dict]] = None
) -> Div:
"""Create a visual representation of the configuration. """Create a visual representation of the configuration.
Args: Args:
eos_host (str): The hostname of the EOS server. eos_host (Optional[str]): The hostname of the EOS server.
eos_port (Union[str, int]): The port of the EOS server. eos_port (Optional[Union[str, int]]): The port of the EOS server.
configuration (Optional[list[dict]]): Optional configuration. If not provided it will be
retrievd from EOS.
Returns: Returns:
rows: Rows of configuration details. Table: A `monsterui.franken.Table` component displaying configuration details.
""" """
if not configuration: flds = "Name", "Type", "RO/RW", "Value", "Default", "Description"
configuration = get_configuration(eos_host, eos_port)
rows = [] rows = []
last_category = "" last_category = ""
# find some special configuration values for config in get_configuration(eos_host, eos_port):
max_planes = 0
for config in configuration:
if config["name"] == "pvforecast.max_planes":
try:
max_planes = int(config["value"])
except:
max_planes = 0
# build visual representation
for config in configuration:
category = config["name"].split(".")[0] category = config["name"].split(".")[0]
if category != last_category: if category != last_category:
rows.append(H3(category)) rows.append(P(category))
rows.append(DividerLine()) rows.append(DividerLine())
last_category = category last_category = category
update_error = config_update_latest.get(config["name"], {}).get("error") rows.append(
update_value = config_update_latest.get(config["name"], {}).get("value") ConfigCard(
update_open = config_update_latest.get(config["name"], {}).get("open") config["name"],
# Make mypy happy - should never trigger config["type"],
if ( config["read-only"],
not isinstance(update_error, (str, type(None))) config["value"],
or not isinstance(update_value, (str, type(None))) config["default"],
or not isinstance(update_open, (bool, type(None))) config["description"],
):
error_msg = "update_error or update_value or update_open of wrong type."
logger.error(error_msg)
raise TypeError(error_msg)
if (
config["type"]
== "Optional[list[akkudoktoreos.prediction.pvforecast.PVForecastPlaneSetting]]"
and not config["deprecated"]
):
# Special configuration for PV planes
rows.append(
ConfigPlanesCard(
config["name"],
config["type"],
config["read-only"],
config["value"],
config["default"],
config["description"],
max_planes,
update_error,
update_value,
update_open,
)
)
else:
rows.append(
ConfigCard(
config["name"],
config["type"],
config["read-only"],
config["value"],
config["default"],
config["description"],
config["deprecated"],
update_error,
update_value,
update_open,
)
) )
)
return Div(*rows, cls="space-y-4") return Div(*rows, cls="space-y-4")
def ConfigKeyUpdate(eos_host: str, eos_port: Union[str, int], key: str, value: str) -> P: def ConfigurationOrg(eos_host: Optional[str], eos_port: Optional[Union[str, int]]) -> Table:
"""Update configuration key and create a visual representation of the configuration. """Create a visual representation of the configuration.
Args: Args:
eos_host (str): The hostname of the EOS server. eos_host (Optional[str]): The hostname of the EOS server.
eos_port (Union[str, int]): The port of the EOS server. eos_port (Optional[Union[str, int]]): The port of the EOS server.
key (str): configuration key in dot notation
value (str): configuration value as json string
Returns: Returns:
rows: Rows of configuration details. Table: A `monsterui.franken.Table` component displaying configuration details.
""" """
server = f"http://{eos_host}:{eos_port}" flds = "Name", "Type", "RO/RW", "Value", "Default", "Description"
path = key.replace(".", "/") rows = [
try: Tr(
data = json.loads(value) Td(
except: config["name"],
if value in ("None", "none", "Null", "null"): cls="max-w-64 text-wrap break-all",
data = None ),
else: Td(
data = value config["type"],
cls="max-w-48 text-wrap break-all",
error = None ),
config = None Td(
try: config["read-only"],
response = requests.put(f"{server}/v1/config/{path}", json=data, timeout=10) cls="max-w-24 text-wrap break-all",
response.raise_for_status() ),
config = response.json() Td(
except requests.exceptions.HTTPError as err: config["value"],
try: cls="max-w-md text-wrap break-all",
# Try to get 'detail' from the JSON response ),
detail = response.json().get( Td(config["default"], cls="max-w-48 text-wrap break-all"),
"detail", f"No error details for data '{data}' '{response.text}'" Td(
) config["description"],
except ValueError: cls="max-w-prose text-wrap",
# Response is not JSON ),
detail = f"No error details for data '{data}' '{response.text}'" cls="",
error = f"Can not set {key} on {server}: {err}, {detail}" )
# Mark all updates as closed for config in get_configuration(eos_host, eos_port)
for k in config_update_latest: ]
config_update_latest[k]["open"] = False head = Thead(*map(Th, flds), cls="text-left")
# Remember this update as latest one return Table(head, Tbody(*rows), cls="w-full uk-table uk-table-divider uk-table-striped")
config_update_latest[key] = {
"error": error,
"result": config,
"value": value,
"open": True,
}
if error or config is None:
# Reread configuration to be shure we display actual data
return Configuration(eos_host, eos_port)
# Use configuration already provided
return Configuration(eos_host, eos_port, configuration(ConfigEOS, config))

View File

@@ -24,7 +24,7 @@
"planes": [ "planes": [
{ {
"peakpower": 5.0, "peakpower": 5.0,
"surface_azimuth": 170, "surface_azimuth": -10,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [ "userhorizon": [
20, 20,
@@ -36,7 +36,7 @@
}, },
{ {
"peakpower": 4.8, "peakpower": 4.8,
"surface_azimuth": 90, "surface_azimuth": -90,
"surface_tilt": 7, "surface_tilt": 7,
"userhorizon": [ "userhorizon": [
30, 30,
@@ -48,7 +48,7 @@
}, },
{ {
"peakpower": 1.4, "peakpower": 1.4,
"surface_azimuth": 140, "surface_azimuth": -40,
"surface_tilt": 60, "surface_tilt": 60,
"userhorizon": [ "userhorizon": [
60, 60,
@@ -60,7 +60,7 @@
}, },
{ {
"peakpower": 1.6, "peakpower": 1.6,
"surface_azimuth": 185, "surface_azimuth": 5,
"surface_tilt": 45, "surface_tilt": 45,
"userhorizon": [ "userhorizon": [
45, 45,
@@ -75,9 +75,9 @@
}, },
"server": { "server": {
"startup_eosdash": true, "startup_eosdash": true,
"host": "127.0.0.1", "host": "0.0.0.0",
"port": 8503, "port": 8503,
"eosdash_host": "127.0.0.1", "eosdash_host": "0.0.0.0",
"eosdash_port": 8504 "eosdash_port": 8504
}, },
"weather": { "weather": {

View File

@@ -4,10 +4,11 @@ from typing import Union
import pandas as pd import pandas as pd
import requests import requests
from bokeh.models import ColumnDataSource, LinearAxis, Range1d from bokeh.models import ColumnDataSource, Range1d
from bokeh.plotting import figure from bokeh.plotting import figure
from monsterui.franken import FT, Grid, P from monsterui.franken import FT, Grid, P
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.pydantic import PydanticDateTimeDataFrame from akkudoktoreos.core.pydantic import PydanticDateTimeDataFrame
from akkudoktoreos.server.dash.bokeh import Bokeh from akkudoktoreos.server.dash.bokeh import Bokeh
@@ -16,6 +17,8 @@ FILE_DEMOCONFIG = DIR_DEMODATA.joinpath("democonfig.json")
if not FILE_DEMOCONFIG.exists(): if not FILE_DEMOCONFIG.exists():
raise ValueError(f"File does not exist: {FILE_DEMOCONFIG}") raise ValueError(f"File does not exist: {FILE_DEMOCONFIG}")
logger = get_logger(__name__)
# bar width for 1 hour bars (time given in millseconds) # bar width for 1 hour bars (time given in millseconds)
BAR_WIDTH_1HOUR = 1000 * 60 * 60 BAR_WIDTH_1HOUR = 1000 * 60 * 60
@@ -73,37 +76,25 @@ def DemoElectricityPriceForecast(predictions: pd.DataFrame, config: dict) -> FT:
return Bokeh(plot) return Bokeh(plot)
def DemoWeatherTempAirHumidity(predictions: pd.DataFrame, config: dict) -> FT: def DemoWeatherTempAir(predictions: pd.DataFrame, config: dict) -> FT:
source = ColumnDataSource(predictions) source = ColumnDataSource(predictions)
provider = config["weather"]["provider"] provider = config["weather"]["provider"]
plot = figure( plot = figure(
x_axis_type="datetime", x_axis_type="datetime",
title=f"Air Temperature and Humidity Prediction ({provider})", y_range=Range1d(
predictions["weather_temp_air"].min() - 1.0, predictions["weather_temp_air"].max() + 1.0
),
title=f"Air Temperature Prediction ({provider})",
x_axis_label="Datetime", x_axis_label="Datetime",
y_axis_label="Temperature [°C]", y_axis_label="Temperature [°C]",
sizing_mode="stretch_width", sizing_mode="stretch_width",
height=400, height=400,
) )
# Add secondary y-axis for humidity
plot.extra_y_ranges["humidity"] = Range1d(start=-5, end=105)
y2_axis = LinearAxis(y_range_name="humidity", axis_label="Relative Humidity [%]")
y2_axis.axis_label_text_color = "green"
plot.add_layout(y2_axis, "left")
plot.line( plot.line(
"date_time", "weather_temp_air", source=source, legend_label="Air Temperature", color="blue" "date_time", "weather_temp_air", source=source, legend_label="Air Temperature", color="blue"
) )
plot.line(
"date_time",
"weather_relative_humidity",
source=source,
legend_label="Relative Humidity [%]",
color="green",
y_range_name="humidity",
)
return Bokeh(plot) return Bokeh(plot)
@@ -144,61 +135,12 @@ def DemoWeatherIrradiance(predictions: pd.DataFrame, config: dict) -> FT:
return Bokeh(plot) return Bokeh(plot)
def DemoLoad(predictions: pd.DataFrame, config: dict) -> FT:
source = ColumnDataSource(predictions)
provider = config["load"]["provider"]
if provider == "LoadAkkudoktor":
year_energy = config["load"]["provider_settings"]["loadakkudoktor_year_energy"]
provider = f"{provider}, {year_energy} kWh"
plot = figure(
x_axis_type="datetime",
title=f"Load Prediction ({provider})",
x_axis_label="Datetime",
y_axis_label="Load [W]",
sizing_mode="stretch_width",
height=400,
)
# Add secondary y-axis for stddev
stddev_min = predictions["load_std"].min()
stddev_max = predictions["load_std"].max()
plot.extra_y_ranges["stddev"] = Range1d(start=stddev_min - 5, end=stddev_max + 5)
y2_axis = LinearAxis(y_range_name="stddev", axis_label="Load Standard Deviation [W]")
y2_axis.axis_label_text_color = "green"
plot.add_layout(y2_axis, "left")
plot.line(
"date_time",
"load_mean",
source=source,
legend_label="Load mean value",
color="red",
)
plot.line(
"date_time",
"load_mean_adjusted",
source=source,
legend_label="Load adjusted by measurement",
color="blue",
)
plot.line(
"date_time",
"load_std",
source=source,
legend_label="Load standard deviation",
color="green",
y_range_name="stddev",
)
return Bokeh(plot)
def Demo(eos_host: str, eos_port: Union[str, int]) -> str: def Demo(eos_host: str, eos_port: Union[str, int]) -> str:
server = f"http://{eos_host}:{eos_port}" server = f"http://{eos_host}:{eos_port}"
# Get current configuration from server # Get current configuration from server
try: try:
result = requests.get(f"{server}/v1/config", timeout=10) result = requests.get(f"{server}/v1/config")
result.raise_for_status() result.raise_for_status()
except requests.exceptions.HTTPError as err: except requests.exceptions.HTTPError as err:
detail = result.json()["detail"] detail = result.json()["detail"]
@@ -212,12 +154,12 @@ def Demo(eos_host: str, eos_port: Union[str, int]) -> str:
with FILE_DEMOCONFIG.open("r", encoding="utf-8") as fd: with FILE_DEMOCONFIG.open("r", encoding="utf-8") as fd:
democonfig = json.load(fd) democonfig = json.load(fd)
try: try:
result = requests.put(f"{server}/v1/config", json=democonfig, timeout=10) result = requests.put(f"{server}/v1/config", json=democonfig)
result.raise_for_status() result.raise_for_status()
except requests.exceptions.HTTPError as err: except requests.exceptions.HTTPError as err:
detail = result.json()["detail"] detail = result.json()["detail"]
# Try to reset to original config # Try to reset to original config
requests.put(f"{server}/v1/config", json=config, timeout=10) requests.put(f"{server}/v1/config", json=config)
return P( return P(
f"Can not set demo configuration on {server}: {err}, {detail}", f"Can not set demo configuration on {server}: {err}, {detail}",
cls="text-center", cls="text-center",
@@ -225,12 +167,12 @@ def Demo(eos_host: str, eos_port: Union[str, int]) -> str:
# Update all predictions # Update all predictions
try: try:
result = requests.post(f"{server}/v1/prediction/update", timeout=10) result = requests.post(f"{server}/v1/prediction/update")
result.raise_for_status() result.raise_for_status()
except requests.exceptions.HTTPError as err: except requests.exceptions.HTTPError as err:
detail = result.json()["detail"] detail = result.json()["detail"]
# Try to reset to original config # Try to reset to original config
requests.put(f"{server}/v1/config", json=config, timeout=10) requests.put(f"{server}/v1/config", json=config)
return P( return P(
f"Can not update predictions on {server}: {err}, {detail}", f"Can not update predictions on {server}: {err}, {detail}",
cls="text-center", cls="text-center",
@@ -242,17 +184,13 @@ def Demo(eos_host: str, eos_port: Union[str, int]) -> str:
"keys": [ "keys": [
"pvforecast_ac_power", "pvforecast_ac_power",
"elecprice_marketprice_kwh", "elecprice_marketprice_kwh",
"weather_relative_humidity",
"weather_temp_air", "weather_temp_air",
"weather_ghi", "weather_ghi",
"weather_dni", "weather_dni",
"weather_dhi", "weather_dhi",
"load_mean",
"load_std",
"load_mean_adjusted",
], ],
} }
result = requests.get(f"{server}/v1/prediction/dataframe", params=params, timeout=10) result = requests.get(f"{server}/v1/prediction/dataframe", params=params)
result.raise_for_status() result.raise_for_status()
predictions = PydanticDateTimeDataFrame(**result.json()).to_dataframe() predictions = PydanticDateTimeDataFrame(**result.json()).to_dataframe()
except requests.exceptions.HTTPError as err: except requests.exceptions.HTTPError as err:
@@ -268,13 +206,12 @@ def Demo(eos_host: str, eos_port: Union[str, int]) -> str:
) )
# Reset to original config # Reset to original config
requests.put(f"{server}/v1/config", json=config, timeout=10) requests.put(f"{server}/v1/config", json=config)
return Grid( return Grid(
DemoPVForecast(predictions, democonfig), DemoPVForecast(predictions, democonfig),
DemoElectricityPriceForecast(predictions, democonfig), DemoElectricityPriceForecast(predictions, democonfig),
DemoWeatherTempAirHumidity(predictions, democonfig), DemoWeatherTempAir(predictions, democonfig),
DemoWeatherIrradiance(predictions, democonfig), DemoWeatherIrradiance(predictions, democonfig),
DemoLoad(predictions, democonfig),
cols_max=2, cols_max=2,
) )

View File

@@ -1,13 +1,14 @@
from typing import Optional, Union from typing import Optional, Union
import requests import requests
from loguru import logger
from monsterui.daisy import Loading, LoadingT from monsterui.daisy import Loading, LoadingT
from monsterui.franken import A, ButtonT, DivFullySpaced, P from monsterui.franken import A, ButtonT, DivFullySpaced, P
from requests.exceptions import RequestException from requests.exceptions import RequestException
from akkudoktoreos.config.config import get_config from akkudoktoreos.config.config import get_config
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
config_eos = get_config() config_eos = get_config()
@@ -23,7 +24,7 @@ def get_alive(eos_host: str, eos_port: Union[str, int]) -> str:
""" """
result = requests.Response() result = requests.Response()
try: try:
result = requests.get(f"http://{eos_host}:{eos_port}/v1/health", timeout=10) result = requests.get(f"http://{eos_host}:{eos_port}/v1/health")
if result.status_code == 200: if result.status_code == 200:
alive = result.json()["status"] alive = result.json()["status"]
else: else:

View File

@@ -7,7 +7,6 @@ import os
import signal import signal
import subprocess import subprocess
import sys import sys
import traceback
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from pathlib import Path from pathlib import Path
from typing import Annotated, Any, AsyncGenerator, Dict, List, Optional, Union from typing import Annotated, Any, AsyncGenerator, Dict, List, Optional, Union
@@ -25,13 +24,11 @@ from fastapi.responses import (
RedirectResponse, RedirectResponse,
Response, Response,
) )
from loguru import logger
from akkudoktoreos.config.config import ConfigEOS, SettingsEOS, get_config from akkudoktoreos.config.config import ConfigEOS, SettingsEOS, get_config
from akkudoktoreos.core.cache import CacheFileStore from akkudoktoreos.core.cache import CacheFileStore
from akkudoktoreos.core.ems import get_ems from akkudoktoreos.core.ems import get_ems
from akkudoktoreos.core.logabc import LOGGING_LEVELS from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.core.logging import read_file_log, track_logging_config
from akkudoktoreos.core.pydantic import ( from akkudoktoreos.core.pydantic import (
PydanticBaseModel, PydanticBaseModel,
PydanticDateTimeData, PydanticDateTimeData,
@@ -51,13 +48,10 @@ from akkudoktoreos.prediction.prediction import PredictionCommonSettings, get_pr
from akkudoktoreos.prediction.pvforecast import PVForecastCommonSettings from akkudoktoreos.prediction.pvforecast import PVForecastCommonSettings
from akkudoktoreos.server.rest.error import create_error_page from akkudoktoreos.server.rest.error import create_error_page
from akkudoktoreos.server.rest.tasks import repeat_every from akkudoktoreos.server.rest.tasks import repeat_every
from akkudoktoreos.server.server import ( from akkudoktoreos.server.server import get_default_host, wait_for_port_free
get_default_host,
is_valid_ip_or_hostname,
wait_for_port_free,
)
from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration from akkudoktoreos.utils.datetimeutil import to_datetime, to_duration
logger = get_logger(__name__)
config_eos = get_config() config_eos = get_config()
measurement_eos = get_measurement() measurement_eos = get_measurement()
prediction_eos = get_prediction() prediction_eos = get_prediction()
@@ -67,14 +61,6 @@ ems_eos = get_ems()
args = None args = None
# ----------------------
# Logging configuration
# ----------------------
logger.remove()
track_logging_config(config_eos, "logging", None, None)
config_eos.track_nested_value("/logging", track_logging_config)
# ---------------------- # ----------------------
# EOSdash server startup # EOSdash server startup
# ---------------------- # ----------------------
@@ -113,11 +99,6 @@ def start_eosdash(
Raises: Raises:
RuntimeError: If the EOSdash server fails to start. RuntimeError: If the EOSdash server fails to start.
""" """
if not is_valid_ip_or_hostname(host):
raise ValueError(f"Invalid EOSdash host: {host}")
if not is_valid_ip_or_hostname(eos_host):
raise ValueError(f"Invalid EOS host: {eos_host}")
eosdash_path = Path(__file__).parent.resolve().joinpath("eosdash.py") eosdash_path = Path(__file__).parent.resolve().joinpath("eosdash.py")
# Do a one time check for port free to generate warnings if not so # Do a one time check for port free to generate warnings if not so
@@ -148,7 +129,7 @@ def start_eosdash(
env["EOS_CONFIG_DIR"] = eos_config_dir env["EOS_CONFIG_DIR"] = eos_config_dir
try: try:
server_process = subprocess.Popen( # noqa: S603 server_process = subprocess.Popen(
cmd, cmd,
env=env, env=env,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -186,32 +167,18 @@ def cache_save() -> dict:
return CacheFileStore().save_store() return CacheFileStore().save_store()
def cache_cleanup_on_exception(e: Exception) -> None: @repeat_every(seconds=float(config_eos.cache.cleanup_interval))
logger.error("Cache cleanup task caught an exception: {}", e, exc_info=True)
@repeat_every(
seconds=float(config_eos.cache.cleanup_interval),
on_exception=cache_cleanup_on_exception,
)
def cache_cleanup_task() -> None: def cache_cleanup_task() -> None:
"""Repeating task to clear cache from expired cache files.""" """Repeating task to clear cache from expired cache files."""
logger.debug("Clear cache")
cache_clear() cache_clear()
def energy_management_on_exception(e: Exception) -> None:
logger.error("Energy management task caught an exception: {}", e, exc_info=True)
@repeat_every( @repeat_every(
seconds=10, seconds=10,
wait_first=config_eos.ems.startup_delay, wait_first=config_eos.ems.startup_delay,
on_exception=energy_management_on_exception,
) )
def energy_management_task() -> None: def energy_management_task() -> None:
"""Repeating task for energy management.""" """Repeating task for energy management."""
logger.debug("Check EMS run")
ems_eos.manage_energy() ems_eos.manage_energy()
@@ -242,7 +209,6 @@ async def server_shutdown_task() -> None:
os.kill(pid, signal.SIGTERM) # type: ignore[attr-defined,unused-ignore] os.kill(pid, signal.SIGTERM) # type: ignore[attr-defined,unused-ignore]
logger.info(f"🚀 EOS terminated, PID {pid}") logger.info(f"🚀 EOS terminated, PID {pid}")
sys.exit(0)
@asynccontextmanager @asynccontextmanager
@@ -273,10 +239,10 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
access_log = args.access_log access_log = args.access_log
reload = args.reload reload = args.reload
host = host if host else get_default_host()
port = port if port else 8504
eos_host = eos_host if eos_host else get_default_host() eos_host = eos_host if eos_host else get_default_host()
eos_port = eos_port if eos_port else 8503 eos_port = eos_port if eos_port else 8503
host = host if host else eos_host
port = port if port else 8504
eos_dir = str(config_eos.general.data_folder_path) eos_dir = str(config_eos.general.data_folder_path)
eos_config_dir = str(config_eos.general.config_folder_path) eos_config_dir = str(config_eos.general.config_folder_path)
@@ -403,7 +369,7 @@ async def fastapi_admin_server_restart_post() -> dict:
env["EOS_DIR"] = str(config_eos.general.data_folder_path) env["EOS_DIR"] = str(config_eos.general.data_folder_path)
env["EOS_CONFIG_DIR"] = str(config_eos.general.config_folder_path) env["EOS_CONFIG_DIR"] = str(config_eos.general.config_folder_path)
new_process = subprocess.Popen( # noqa: S603 new_process = subprocess.Popen(
[ [
sys.executable, sys.executable,
] ]
@@ -519,9 +485,7 @@ def fastapi_config_put_key(
path: str = FastapiPath( path: str = FastapiPath(
..., description="The nested path to the configuration key (e.g., general/latitude)." ..., description="The nested path to the configuration key (e.g., general/latitude)."
), ),
value: Optional[Any] = Body( value: Any = Body(..., description="The value to assign to the specified configuration path."),
None, description="The value to assign to the specified configuration path (can be None)."
),
) -> ConfigEOS: ) -> ConfigEOS:
"""Update a nested key or index in the config model. """Update a nested key or index in the config model.
@@ -533,13 +497,13 @@ def fastapi_config_put_key(
configuration (ConfigEOS): The current configuration after the update. configuration (ConfigEOS): The current configuration after the update.
""" """
try: try:
config_eos.set_nested_value(path, value) config_eos.set_config_value(path, value)
except IndexError as e:
raise HTTPException(status_code=400, detail=str(e))
except KeyError as e:
raise HTTPException(status_code=404, detail=str(e))
except Exception as e: except Exception as e:
trace = "".join(traceback.TracebackException.from_exception(e).format()) raise HTTPException(status_code=400, detail=str(e))
raise HTTPException(
status_code=400,
detail=f"Error on update of configuration '{path}','{value}': {e}\n{trace}",
)
return config_eos return config_eos
@@ -559,7 +523,7 @@ def fastapi_config_get_key(
value (Any): The value of the selected nested key. value (Any): The value of the selected nested key.
""" """
try: try:
return config_eos.get_nested_value(path) return config_eos.get_config_value(path)
except IndexError as e: except IndexError as e:
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
except KeyError as e: except KeyError as e:
@@ -568,52 +532,6 @@ def fastapi_config_get_key(
raise HTTPException(status_code=400, detail=str(e)) raise HTTPException(status_code=400, detail=str(e))
@app.get("/v1/logging/log", tags=["logging"])
async def fastapi_logging_get_log(
limit: int = Query(100, description="Maximum number of log entries to return."),
level: Optional[str] = Query(None, description="Filter by log level (e.g., INFO, ERROR)."),
contains: Optional[str] = Query(None, description="Filter logs containing this substring."),
regex: Optional[str] = Query(None, description="Filter logs by matching regex in message."),
from_time: Optional[str] = Query(
None, description="Start time (ISO format) for filtering logs."
),
to_time: Optional[str] = Query(None, description="End time (ISO format) for filtering logs."),
tail: bool = Query(False, description="If True, returns the most recent lines (tail mode)."),
) -> JSONResponse:
"""Get structured log entries from the EOS log file.
Filters and returns log entries based on the specified query parameters. The log
file is expected to contain newline-delimited JSON entries.
Args:
limit (int): Maximum number of entries to return.
level (Optional[str]): Filter logs by severity level (e.g., DEBUG, INFO).
contains (Optional[str]): Return only logs that include this string in the message.
regex (Optional[str]): Return logs that match this regular expression in the message.
from_time (Optional[str]): ISO 8601 timestamp to filter logs not older than this.
to_time (Optional[str]): ISO 8601 timestamp to filter logs not newer than this.
tail (bool): If True, fetch the most recent log entries (like `tail`).
Returns:
JSONResponse: A JSON list of log entries.
"""
log_path = config_eos.logging.file_path
try:
logs = read_file_log(
log_path=log_path,
limit=limit,
level=level,
contains=contains,
regex=regex,
from_time=from_time,
to_time=to_time,
tail=tail,
)
return JSONResponse(content=logs)
except Exception as e:
return JSONResponse(content={"error": str(e)}, status_code=500)
@app.get("/v1/measurement/keys", tags=["measurement"]) @app.get("/v1/measurement/keys", tags=["measurement"])
def fastapi_measurement_keys_get() -> list[str]: def fastapi_measurement_keys_get() -> list[str]:
"""Get a list of available measurement keys.""" """Get a list of available measurement keys."""
@@ -926,11 +844,7 @@ def fastapi_prediction_update(
try: try:
prediction_eos.update_data(force_update=force_update, force_enable=force_enable) prediction_eos.update_data(force_update=force_update, force_enable=force_enable)
except Exception as e: except Exception as e:
trace = "".join(traceback.TracebackException.from_exception(e).format()) raise HTTPException(status_code=400, detail=f"Error on prediction update: {e}")
raise HTTPException(
status_code=400,
detail=f"Error on prediction update: {e}\n{trace}",
)
return Response() return Response()
@@ -954,9 +868,7 @@ def fastapi_prediction_update_provider(
try: try:
provider.update_data(force_update=force_update, force_enable=force_enable) provider.update_data(force_update=force_update, force_enable=force_enable)
except Exception as e: except Exception as e:
raise HTTPException( raise HTTPException(status_code=400, detail=f"Error on update of provider: {e}")
status_code=400, detail=f"Error on update of provider '{provider_id}': {e}"
)
return Response() return Response()
@@ -1226,14 +1138,9 @@ def fastapi_optimize(
# Perform optimization simulation # Perform optimization simulation
opt_class = optimization_problem(verbose=bool(config_eos.server.verbose)) opt_class = optimization_problem(verbose=bool(config_eos.server.verbose))
try: result = opt_class.optimierung_ems(parameters=parameters, start_hour=start_hour, **extra_args)
result = opt_class.optimierung_ems( # print(result)
parameters=parameters, start_hour=start_hour, **extra_args return result
)
# print(result)
return result
except Exception as e:
raise HTTPException(status_code=400, detail=f"Optimize error: {e}.")
@app.get("/visualization_results.pdf", response_class=PdfResponse, tags=["optimize"]) @app.get("/visualization_results.pdf", response_class=PdfResponse, tags=["optimize"])
@@ -1287,7 +1194,7 @@ def redirect(request: Request, path: str) -> Union[HTMLResponse, RedirectRespons
if port is None: if port is None:
port = 8504 port = 8504
# Make hostname Windows friendly # Make hostname Windows friendly
if host == "0.0.0.0" and os.name == "nt": # noqa: S104 if host == "0.0.0.0" and os.name == "nt":
host = "localhost" host = "localhost"
url = f"http://{host}:{port}/" url = f"http://{host}:{port}/"
error_page = create_error_page( error_page = create_error_page(
@@ -1304,7 +1211,7 @@ Did you want to connect to <a href="{url}" class="back-button">EOSdash</a>?
# Make hostname Windows friendly # Make hostname Windows friendly
host = str(config_eos.server.eosdash_host) host = str(config_eos.server.eosdash_host)
if host == "0.0.0.0" and os.name == "nt": # noqa: S104 if host == "0.0.0.0" and os.name == "nt":
host = "localhost" host = "localhost"
if host and config_eos.server.eosdash_port: if host and config_eos.server.eosdash_port:
# Redirect to EOSdash server # Redirect to EOSdash server
@@ -1315,46 +1222,45 @@ Did you want to connect to <a href="{url}" class="back-button">EOSdash</a>?
return RedirectResponse(url="/docs", status_code=303) return RedirectResponse(url="/docs", status_code=303)
def run_eos(host: str, port: int, log_level: str, reload: bool) -> None: def run_eos(host: str, port: int, log_level: str, access_log: bool, reload: bool) -> None:
"""Run the EOS server with the specified configurations. """Run the EOS server with the specified configurations.
Starts the EOS server using the Uvicorn ASGI server. Logs an error and exits if This function starts the EOS server using the Uvicorn ASGI server. It accepts
binding to the host and port fails. arguments for the host, port, log level, access log, and reload options. The
log level is converted to lowercase to ensure compatibility with Uvicorn's
expected log level format. If an error occurs while attempting to bind the
server to the specified host and port, an error message is logged and the
application exits.
Args: Parameters:
host (str): Hostname to bind the server to. host (str): The hostname to bind the server to.
port (int): Port number to bind the server to. port (int): The port number to bind the server to.
log_level (str): Log level for the server. One of log_level (str): The log level for the server. Options include "critical", "error",
"critical", "error", "warning", "info", "debug", or "trace". "warning", "info", "debug", and "trace".
reload (bool): Enable or disable auto-reload. True for development. access_log (bool): Whether to enable or disable the access log. Set to True to enable.
reload (bool): Whether to enable or disable auto-reload. Set to True for development.
Returns: Returns:
None None
""" """
# Make hostname Windows friendly # Make hostname Windows friendly
if host == "0.0.0.0" and os.name == "nt": # noqa: S104 if host == "0.0.0.0" and os.name == "nt":
host = "localhost" host = "localhost"
# Setup console logging level using nested value
# - triggers logging configuration by track_logging_config
if log_level.upper() in LOGGING_LEVELS:
config_eos.set_nested_value("logging/console_level", log_level.upper())
# Wait for EOS port to be free - e.g. in case of restart # Wait for EOS port to be free - e.g. in case of restart
wait_for_port_free(port, timeout=120, waiting_app_name="EOS") wait_for_port_free(port, timeout=120, waiting_app_name="EOS")
try: try:
# Let uvicorn run the fastAPI app
uvicorn.run( uvicorn.run(
"akkudoktoreos.server.eos:app", "akkudoktoreos.server.eos:app",
host=host, host=host,
port=port, port=port,
log_level="info", log_level=log_level.lower(), # Convert log_level to lowercase
access_log=True, access_log=access_log,
reload=reload, reload=reload,
) )
except Exception as e: except Exception as e:
logger.exception("Failed to start uvicorn server.") logger.error(f"Could not bind to host {host}:{port}. Error: {e}")
raise e raise e
@@ -1369,7 +1275,7 @@ def main() -> None:
Command-line Arguments: Command-line Arguments:
--host (str): Host for the EOS server (default: value from config). --host (str): Host for the EOS server (default: value from config).
--port (int): Port for the EOS server (default: value from config). --port (int): Port for the EOS server (default: value from config).
--log_level (str): Log level for the server console. Options: "critical", "error", "warning", "info", "debug", "trace" (default: "info"). --log_level (str): Log level for the server. Options: "critical", "error", "warning", "info", "debug", "trace" (default: "info").
--access_log (bool): Enable or disable access log. Options: True or False (default: False). --access_log (bool): Enable or disable access log. Options: True or False (default: False).
--reload (bool): Enable or disable auto-reload. Useful for development. Options: True or False (default: False). --reload (bool): Enable or disable auto-reload. Useful for development. Options: True or False (default: False).
""" """
@@ -1393,8 +1299,14 @@ def main() -> None:
parser.add_argument( parser.add_argument(
"--log_level", "--log_level",
type=str, type=str,
default="none", default="info",
help='Log level for the server console. Options: "critical", "error", "warning", "info", "debug", "trace" (default: "none")', help='Log level for the server. Options: "critical", "error", "warning", "info", "debug", "trace" (default: "info")',
)
parser.add_argument(
"--access_log",
type=bool,
default=False,
help="Enable or disable access log. Options: True or False (default: True)",
) )
parser.add_argument( parser.add_argument(
"--reload", "--reload",
@@ -1409,7 +1321,7 @@ def main() -> None:
port = args.port if args.port else 8503 port = args.port if args.port else 8503
try: try:
run_eos(host, port, args.log_level, args.reload) run_eos(host, port, args.log_level, args.access_log, args.reload)
except: except:
sys.exit(1) sys.exit(1)

View File

@@ -8,21 +8,23 @@ from typing import Optional
import psutil import psutil
import uvicorn import uvicorn
from fasthtml.common import FileResponse, JSONResponse from fasthtml.common import FileResponse, JSONResponse
from loguru import logger
from monsterui.core import FastHTML, Theme from monsterui.core import FastHTML, Theme
from akkudoktoreos.config.config import get_config from akkudoktoreos.config.config import get_config
from akkudoktoreos.core.logging import get_logger
# Pages
from akkudoktoreos.server.dash.admin import Admin
from akkudoktoreos.server.dash.bokeh import BokehJS from akkudoktoreos.server.dash.bokeh import BokehJS
from akkudoktoreos.server.dash.components import Page from akkudoktoreos.server.dash.components import Page
from akkudoktoreos.server.dash.configuration import ConfigKeyUpdate, Configuration
# Pages
from akkudoktoreos.server.dash.configuration import Configuration
from akkudoktoreos.server.dash.demo import Demo from akkudoktoreos.server.dash.demo import Demo
from akkudoktoreos.server.dash.footer import Footer from akkudoktoreos.server.dash.footer import Footer
from akkudoktoreos.server.dash.hello import Hello from akkudoktoreos.server.dash.hello import Hello
from akkudoktoreos.server.server import get_default_host, wait_for_port_free from akkudoktoreos.server.server import get_default_host, wait_for_port_free
# from akkudoktoreos.server.dash.altair import AltairJS
logger = get_logger(__name__)
config_eos = get_config() config_eos = get_config()
# The favicon for EOSdash # The favicon for EOSdash
@@ -35,7 +37,8 @@ args: Optional[argparse.Namespace] = None
# Get frankenui and tailwind headers via CDN using Theme.green.headers() # Get frankenui and tailwind headers via CDN using Theme.green.headers()
# Add Bokeh headers # Add altair headers
# hdrs=(Theme.green.headers(highlightjs=True), AltairJS,)
hdrs = ( hdrs = (
Theme.green.headers(highlightjs=True), Theme.green.headers(highlightjs=True),
BokehJS, BokehJS,
@@ -91,7 +94,6 @@ def get_eosdash(): # type: ignore
"EOSdash": "/eosdash/hello", "EOSdash": "/eosdash/hello",
"Config": "/eosdash/configuration", "Config": "/eosdash/configuration",
"Demo": "/eosdash/demo", "Demo": "/eosdash/demo",
"Admin": "/eosdash/admin",
}, },
Hello(), Hello(),
Footer(*eos_server()), Footer(*eos_server()),
@@ -119,21 +121,6 @@ def get_eosdash_hello(): # type: ignore
return Hello() return Hello()
@app.get("/eosdash/admin")
def get_eosdash_admin(): # type: ignore
"""Serves the EOSdash Admin page.
Returns:
Admin: The Admin page component.
"""
return Admin(*eos_server())
@app.post("/eosdash/admin")
def post_eosdash_admin(data: dict): # type: ignore
return Admin(*eos_server(), data)
@app.get("/eosdash/configuration") @app.get("/eosdash/configuration")
def get_eosdash_configuration(): # type: ignore def get_eosdash_configuration(): # type: ignore
"""Serves the EOSdash Configuration page. """Serves the EOSdash Configuration page.
@@ -144,11 +131,6 @@ def get_eosdash_configuration(): # type: ignore
return Configuration(*eos_server()) return Configuration(*eos_server())
@app.put("/eosdash/configuration")
def put_eosdash_configuration(data: dict): # type: ignore
return ConfigKeyUpdate(*eos_server(), data["key"], data["value"])
@app.get("/eosdash/demo") @app.get("/eosdash/demo")
def get_eosdash_demo(): # type: ignore def get_eosdash_demo(): # type: ignore
"""Serves the EOSdash Demo page. """Serves the EOSdash Demo page.
@@ -241,7 +223,7 @@ def run_eosdash() -> None:
reload = False reload = False
# Make hostname Windows friendly # Make hostname Windows friendly
if eosdash_host == "0.0.0.0" and os.name == "nt": # noqa: S104 if eosdash_host == "0.0.0.0" and os.name == "nt":
eosdash_host = "localhost" eosdash_host = "localhost"
# Wait for EOSdash port to be free - e.g. in case of restart # Wait for EOSdash port to be free - e.g. in case of restart

View File

@@ -1,5 +1,3 @@
import html
ERROR_PAGE_TEMPLATE = """ ERROR_PAGE_TEMPLATE = """
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@@ -88,6 +86,6 @@ def create_error_page(
return ( return (
ERROR_PAGE_TEMPLATE.replace("STATUS_CODE", status_code) ERROR_PAGE_TEMPLATE.replace("STATUS_CODE", status_code)
.replace("ERROR_TITLE", error_title) .replace("ERROR_TITLE", error_title)
.replace("ERROR_MESSAGE", html.escape(error_message)) .replace("ERROR_MESSAGE", error_message)
.replace("ERROR_DETAILS", html.escape(error_details)) .replace("ERROR_DETAILS", error_details)
) )

View File

@@ -1,51 +1,22 @@
"""Server Module.""" """Server Module."""
import ipaddress import os
import re
import time import time
from typing import Optional, Union from typing import Optional, Union
import psutil import psutil
from loguru import logger
from pydantic import Field, IPvAnyAddress, field_validator from pydantic import Field, IPvAnyAddress, field_validator
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
def get_default_host() -> str: def get_default_host() -> str:
"""Default host for EOS.""" if os.name == "nt":
return "127.0.0.1" return "127.0.0.1"
return "0.0.0.0"
def is_valid_ip_or_hostname(value: str) -> bool:
"""Validate whether a string is a valid IP address (IPv4 or IPv6) or hostname.
This function first attempts to interpret the input as an IP address using the
standard library `ipaddress` module. If that fails, it checks whether the input
is a valid hostname according to RFC 1123, which allows domain names consisting
of alphanumeric characters and hyphens, with specific length and structure rules.
Args:
value (str): The input string to validate.
Returns:
bool: True if the input is a valid IP address or hostname, False otherwise.
"""
try:
ipaddress.ip_address(value)
return True
except ValueError:
pass
if len(value) > 253:
return False
hostname_regex = re.compile(
r"^(?=.{1,253}$)(?!-)[A-Z\d-]{1,63}(?<!-)"
r"(?:\.(?!-)[A-Z\d-]{1,63}(?<!-))*\.?$",
re.IGNORECASE,
)
return bool(hostname_regex.fullmatch(value))
def wait_for_port_free(port: int, timeout: int = 0, waiting_app_name: str = "App") -> bool: def wait_for_port_free(port: int, timeout: int = 0, waiting_app_name: str = "App") -> bool:
@@ -139,8 +110,6 @@ class ServerCommonSettings(SettingsBaseModel):
cls, value: Optional[Union[str, IPvAnyAddress]] cls, value: Optional[Union[str, IPvAnyAddress]]
) -> Optional[Union[str, IPvAnyAddress]]: ) -> Optional[Union[str, IPvAnyAddress]]:
if isinstance(value, str): if isinstance(value, str):
if not is_valid_ip_or_hostname(value):
raise ValueError(f"Invalid host: {value}")
if value.lower() in ("localhost", "loopback"): if value.lower() in ("localhost", "loopback"):
value = "127.0.0.1" value = "127.0.0.1"
return value return value

View File

@@ -27,12 +27,13 @@ from datetime import date, datetime, timedelta
from typing import Any, List, Literal, Optional, Tuple, Union, overload from typing import Any, List, Literal, Optional, Tuple, Union, overload
import pendulum import pendulum
from loguru import logger
from pendulum import Date, DateTime, Duration from pendulum import Date, DateTime, Duration
from pendulum.tz.timezone import Timezone from pendulum.tz.timezone import Timezone
from timezonefinder import TimezoneFinder from timezonefinder import TimezoneFinder
MAX_DURATION_STRING_LENGTH = 350 from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
@overload @overload
@@ -152,22 +153,22 @@ def to_datetime(
fmt_tz = f"{fmt} z" fmt_tz = f"{fmt} z"
dt_tz = f"{date_input} {in_timezone}" dt_tz = f"{date_input} {in_timezone}"
dt = pendulum.from_format(dt_tz, fmt_tz) dt = pendulum.from_format(dt_tz, fmt_tz)
logger.trace( logger.debug(
f"Str Fmt converted: {dt}, tz={dt.tz} from {date_input}, tz={in_timezone}" f"Str Fmt converted: {dt}, tz={dt.tz} from {date_input}, tz={in_timezone}"
) )
break break
except ValueError as e: except ValueError as e:
logger.trace(f"{date_input}, {fmt}, {e}") logger.debug(f"{date_input}, {fmt}, {e}")
dt = None dt = None
else: else:
# DateTime input with timezone info # DateTime input with timezone info
try: try:
dt = pendulum.parse(date_input) dt = pendulum.parse(date_input)
logger.trace( logger.debug(
f"Pendulum Fmt converted: {dt}, tz={dt.tz} from {date_input}, tz={in_timezone}" f"Pendulum Fmt converted: {dt}, tz={dt.tz} from {date_input}, tz={in_timezone}"
) )
except pendulum.parsing.exceptions.ParserError as e: except pendulum.parsing.exceptions.ParserError as e:
logger.trace(f"Date string {date_input} does not match any Pendulum formats: {e}") logger.debug(f"Date string {date_input} does not match any Pendulum formats: {e}")
dt = None dt = None
if dt is None: if dt is None:
# Some special values # Some special values
@@ -179,7 +180,7 @@ def to_datetime(
timestamp = float(date_input) timestamp = float(date_input)
dt = pendulum.from_timestamp(timestamp, tz="UTC") dt = pendulum.from_timestamp(timestamp, tz="UTC")
except (ValueError, TypeError) as e: except (ValueError, TypeError) as e:
logger.trace(f"Date string {date_input} does not match timestamp format: {e}") logger.debug(f"Date string {date_input} does not match timestamp format: {e}")
dt = None dt = None
if dt is None: if dt is None:
raise ValueError(f"Date string {date_input} does not match any known formats.") raise ValueError(f"Date string {date_input} does not match any known formats.")
@@ -200,7 +201,7 @@ def to_datetime(
# Represent in target timezone # Represent in target timezone
dt_in_tz = dt.in_timezone(in_timezone) dt_in_tz = dt.in_timezone(in_timezone)
logger.trace( logger.debug(
f"\nTimezone adapted to: {in_timezone}\nfrom: {dt} tz={dt.timezone}\nto: {dt_in_tz} tz={dt_in_tz.tz}" f"\nTimezone adapted to: {in_timezone}\nfrom: {dt} tz={dt.timezone}\nto: {dt_in_tz} tz={dt_in_tz.tz}"
) )
dt = dt_in_tz dt = dt_in_tz
@@ -275,7 +276,7 @@ def to_duration(
duration = pendulum.parse(input_value) duration = pendulum.parse(input_value)
return duration - duration.start_of("day") return duration - duration.start_of("day")
except pendulum.parsing.exceptions.ParserError as e: except pendulum.parsing.exceptions.ParserError as e:
logger.trace(f"Invalid Pendulum time string format '{input_value}': {e}") logger.debug(f"Invalid Pendulum time string format '{input_value}': {e}")
# Handle strings like "2 days 5 hours 30 minutes" # Handle strings like "2 days 5 hours 30 minutes"
total_seconds = 0 total_seconds = 0
@@ -286,11 +287,6 @@ def to_duration(
"second": 1, "second": 1,
} }
# Mitigate ReDoS vulnerability (#494) by checking input string length.
if len(input_value) > MAX_DURATION_STRING_LENGTH:
raise ValueError(
f"Input string exceeds maximum allowed length ({MAX_DURATION_STRING_LENGTH})."
)
# Regular expression to match time components like '2 days', '5 hours', etc. # Regular expression to match time components like '2 days', '5 hours', etc.
matches = re.findall(r"(\d+)\s*(days?|hours?|minutes?|seconds?)", input_value) matches = re.findall(r"(\d+)\s*(days?|hours?|minutes?|seconds?)", input_value)

View File

@@ -35,8 +35,6 @@ def get_model_structure_from_examples(
example_data: list[dict[str, Any]] = [{} for _ in range(example_max_length)] example_data: list[dict[str, Any]] = [{} for _ in range(example_max_length)]
for field_name, field_info in model_class.model_fields.items(): for field_name, field_info in model_class.model_fields.items():
if field_info.deprecated:
continue
for example_ix in range(example_max_length): for example_ix in range(example_max_length):
example_data[example_ix][field_name] = get_example_or_default( example_data[example_ix][field_name] = get_example_or_default(
field_name, field_info, example_ix field_name, field_info, example_ix

View File

@@ -4,6 +4,9 @@ from typing import Any
import numpy as np import numpy as np
from akkudoktoreos.config.configabc import SettingsBaseModel from akkudoktoreos.config.configabc import SettingsBaseModel
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
class UtilsCommonSettings(SettingsBaseModel): class UtilsCommonSettings(SettingsBaseModel):

View File

@@ -12,18 +12,16 @@ import pendulum
from matplotlib.backends.backend_pdf import PdfPages from matplotlib.backends.backend_pdf import PdfPages
from akkudoktoreos.core.coreabc import ConfigMixin from akkudoktoreos.core.coreabc import ConfigMixin
from akkudoktoreos.core.ems import EnergyManagement from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.optimization.genetic import OptimizationParameters from akkudoktoreos.optimization.genetic import OptimizationParameters
from akkudoktoreos.utils.datetimeutil import to_datetime from akkudoktoreos.utils.datetimeutil import to_datetime
logger = get_logger(__name__)
matplotlib.use( matplotlib.use(
"Agg" "Agg"
) # non-interactive backend that can only write to files, backend needed to stay in main thread. ) # non-interactive backend that can only write to files, backend needed to stay in main thread.
debug_visualize: bool = False
class VisualizationReport(ConfigMixin): class VisualizationReport(ConfigMixin):
def __init__( def __init__(
self, self,
@@ -164,18 +162,14 @@ class VisualizationReport(ConfigMixin):
# Format the time axis # Format the time axis
plt.gca().xaxis.set_major_formatter( plt.gca().xaxis.set_major_formatter(
mdates.DateFormatter("%Y-%m-%d", tz=self.config.general.timezone) mdates.DateFormatter("%Y-%m-%d")
) # Show date and time ) # Show date and time
plt.gca().xaxis.set_major_locator( plt.gca().xaxis.set_major_locator(
mdates.DayLocator(interval=1, tz=self.config.general.timezone) mdates.DayLocator(interval=1, tz=None)
) # Major ticks every day ) # Major ticks every day
plt.gca().xaxis.set_minor_locator( plt.gca().xaxis.set_minor_locator(mdates.HourLocator(interval=3, tz=None))
mdates.HourLocator(interval=2, tz=self.config.general.timezone)
)
# Minor ticks every 6 hours # Minor ticks every 6 hours
plt.gca().xaxis.set_minor_formatter( plt.gca().xaxis.set_minor_formatter(mdates.DateFormatter("%H"))
mdates.DateFormatter("%H", tz=self.config.general.timezone)
)
# plt.gcf().autofmt_xdate(rotation=45, which="major") # plt.gcf().autofmt_xdate(rotation=45, which="major")
# Auto-format the x-axis for readability # Auto-format the x-axis for readability
@@ -208,10 +202,8 @@ class VisualizationReport(ConfigMixin):
hours_since_start = [(t - timestamps[0]).total_seconds() / 3600 for t in timestamps] hours_since_start = [(t - timestamps[0]).total_seconds() / 3600 for t in timestamps]
# ax2.set_xticks(timestamps[::48]) # Set ticks every 12 hours # ax2.set_xticks(timestamps[::48]) # Set ticks every 12 hours
# ax2.set_xticklabels([f"{int(h)}" for h in hours_since_start[::48]]) # ax2.set_xticklabels([f"{int(h)}" for h in hours_since_start[::48]])
# ax2.set_xticks(timestamps[:: len(timestamps) // 24]) # Select 10 evenly spaced ticks ax2.set_xticks(timestamps[:: len(timestamps) // 24]) # Select 10 evenly spaced ticks
ax2.set_xticks(timestamps[:: len(timestamps) // 12]) # Select 10 evenly spaced ticks ax2.set_xticklabels([f"{int(h)}" for h in hours_since_start[:: len(timestamps) // 24]])
# ax2.set_xticklabels([f"{int(h)}" for h in hours_since_start[:: len(timestamps) // 24]])
ax2.set_xticklabels([f"{int(h)}" for h in hours_since_start[:: len(timestamps) // 12]])
if x2label: if x2label:
ax2.set_xlabel(x2label) ax2.set_xlabel(x2label)
@@ -439,17 +431,15 @@ def prepare_visualize(
parameters: OptimizationParameters, parameters: OptimizationParameters,
results: dict, results: dict,
filename: str = "visualization_results.pdf", filename: str = "visualization_results.pdf",
start_hour: int = 0, start_hour: Optional[int] = 0,
) -> None: ) -> None:
global debug_visualize
report = VisualizationReport(filename) report = VisualizationReport(filename)
next_full_hour_date = EnergyManagement.set_start_datetime() next_full_hour_date = pendulum.now(report.config.general.timezone).start_of("hour").add(hours=1)
# Group 1: # Group 1:
report.create_line_chart_date( report.create_line_chart_date(
next_full_hour_date, next_full_hour_date, # start_date
[ [
parameters.ems.gesamtlast[start_hour:], parameters.ems.gesamtlast,
], ],
title="Load Profile", title="Load Profile",
# xlabel="Hours", # not enough space # xlabel="Hours", # not enough space
@@ -457,9 +447,9 @@ def prepare_visualize(
labels=["Total Load (Wh)"], labels=["Total Load (Wh)"],
) )
report.create_line_chart_date( report.create_line_chart_date(
next_full_hour_date, next_full_hour_date, # start_date
[ [
parameters.ems.pv_prognose_wh[start_hour:], parameters.ems.pv_prognose_wh,
], ],
title="PV Forecast", title="PV Forecast",
# xlabel="Hours", # not enough space # xlabel="Hours", # not enough space
@@ -467,15 +457,8 @@ def prepare_visualize(
) )
report.create_line_chart_date( report.create_line_chart_date(
next_full_hour_date, next_full_hour_date, # start_date
[ [np.full(len(parameters.ems.gesamtlast), parameters.ems.einspeiseverguetung_euro_pro_wh)],
np.full(
len(parameters.ems.gesamtlast) - start_hour,
parameters.ems.einspeiseverguetung_euro_pro_wh[start_hour:]
if isinstance(parameters.ems.einspeiseverguetung_euro_pro_wh, list)
else parameters.ems.einspeiseverguetung_euro_pro_wh,
)
],
title="Remuneration", title="Remuneration",
# xlabel="Hours", # not enough space # xlabel="Hours", # not enough space
ylabel="€/Wh", ylabel="€/Wh",
@@ -483,9 +466,9 @@ def prepare_visualize(
) )
if parameters.temperature_forecast: if parameters.temperature_forecast:
report.create_line_chart_date( report.create_line_chart_date(
next_full_hour_date, next_full_hour_date, # start_date
[ [
parameters.temperature_forecast[start_hour:], parameters.temperature_forecast,
], ],
title="Temperature Forecast", title="Temperature Forecast",
# xlabel="Hours", # not enough space # xlabel="Hours", # not enough space
@@ -534,35 +517,21 @@ def prepare_visualize(
) )
report.create_line_chart_date( report.create_line_chart_date(
next_full_hour_date, # start_date next_full_hour_date, # start_date
[parameters.ems.strompreis_euro_pro_wh[start_hour:]], [parameters.ems.strompreis_euro_pro_wh],
# title="Electricity Price", # not enough space title="Electricity Price",
# xlabel="Date", # not enough space # xlabel="Date", # not enough space
ylabel="Electricity Price (€/Wh)", ylabel="Electricity Price (€/Wh)",
x2label=None, # not enough space x2label=None, # not enough space
) )
labels = list(
item
for sublist in zip(
list(str(i) for i in range(0, 23, 2)), list(str(" ") for i in range(0, 23, 2))
)
for item in sublist
)
labels = labels[start_hour:] + labels
report.create_bar_chart( report.create_bar_chart(
labels, list(str(i) for i in range(len(results["ac_charge"]))),
[ [results["ac_charge"], results["dc_charge"], results["discharge_allowed"]],
results["ac_charge"][start_hour:],
results["dc_charge"][start_hour:],
results["discharge_allowed"][start_hour:],
],
title="AC/DC Charging and Discharge Overview", title="AC/DC Charging and Discharge Overview",
ylabel="Relative Power (0-1) / Discharge (0 or 1)", ylabel="Relative Power (0-1) / Discharge (0 or 1)",
label_names=["AC Charging (relative)", "DC Charging (relative)", "Discharge Allowed"], label_names=["AC Charging (relative)", "DC Charging (relative)", "Discharge Allowed"],
colors=["blue", "green", "red"], colors=["blue", "green", "red"],
bottom=3, bottom=3,
xlabels=labels,
) )
report.finalize_group() report.finalize_group()
@@ -645,7 +614,7 @@ def prepare_visualize(
if filtered_balance.size > 0 or filtered_losses.size > 0: if filtered_balance.size > 0 or filtered_losses.size > 0:
report.finalize_group() report.finalize_group()
if debug_visualize or results["fixed_seed"]: if logger.level == "DEBUG" or results["fixed_seed"]:
report.create_line_chart( report.create_line_chart(
0, 0,
[ [
@@ -670,8 +639,6 @@ def prepare_visualize(
def generate_example_report(filename: str = "example_report.pdf") -> None: def generate_example_report(filename: str = "example_report.pdf") -> None:
"""Generate example visualization report.""" """Generate example visualization report."""
global debug_visualize
report = VisualizationReport(filename, "test") report = VisualizationReport(filename, "test")
x_hours = 0 # Define x-axis start values (e.g., hours) x_hours = 0 # Define x-axis start values (e.g., hours)
@@ -743,9 +710,9 @@ def generate_example_report(filename: str = "example_report.pdf") -> None:
report.finalize_group() # Finalize the third group of charts report.finalize_group() # Finalize the third group of charts
debug_visualize = True # set level for example report logger.setLevel("DEBUG") # set level for example report
if debug_visualize: if logger.level == "DEBUG":
report.create_line_chart( report.create_line_chart(
x_hours, x_hours,
[np.array([0.2, 0.25, 0.3, 0.35])], [np.array([0.2, 0.25, 0.3, 0.35])],

View File

@@ -16,75 +16,35 @@ import pendulum
import psutil import psutil
import pytest import pytest
import requests import requests
from _pytest.logging import LogCaptureFixture
from loguru import logger
from xprocess import ProcessStarter, XProcess from xprocess import ProcessStarter, XProcess
from akkudoktoreos.config.config import ConfigEOS, get_config from akkudoktoreos.config.config import ConfigEOS, get_config
from akkudoktoreos.core.logging import get_logger
from akkudoktoreos.server.server import get_default_host from akkudoktoreos.server.server import get_default_host
# ----------------------------------------------- logger = get_logger(__name__)
# Adapt pytest logging handling to Loguru logging
# -----------------------------------------------
@pytest.fixture
def caplog(caplog: LogCaptureFixture):
"""Propagate Loguru logs to the pytest caplog handler."""
handler_id = logger.add(
caplog.handler,
format="{message}",
level=0,
filter=lambda record: record["level"].no >= caplog.handler.level,
enqueue=False, # Set to 'True' if your test is spawning child processes.
)
yield caplog
try:
logger.remove(handler_id)
except:
# May already be deleted
pass
@pytest.fixture
def reportlog(pytestconfig):
"""Propagate Loguru logs to the pytest terminal reporter."""
logging_plugin = pytestconfig.pluginmanager.getplugin("logging-plugin")
handler_id = logger.add(logging_plugin.report_handler, format="{message}")
yield
try:
logger.remove(handler_id)
except:
# May already be deleted
pass
@pytest.fixture(autouse=True)
def propagate_logs():
"""Deal with the pytest --log-cli-level command-line flag.
This option controls the standard logging logs, not loguru ones.
For this reason, we first install a PropagateHandler for compatibility.
"""
class PropagateHandler(logging.Handler):
def emit(self, record):
if logging.getLogger(record.name).isEnabledFor(record.levelno):
logging.getLogger(record.name).handle(record)
logger.remove()
logger.add(PropagateHandler(), format="{message}")
yield
@pytest.fixture() @pytest.fixture()
def disable_debug_logging(scope="session", autouse=True): def disable_debug_logging(scope="session", autouse=True):
"""Automatically disable debug logging for all tests.""" """Automatically disable debug logging for all tests."""
logger.remove() # Remove all loggers original_levels = {}
logger.add(sys.stderr, level="INFO") # Only show INFO and above root_logger = logging.getLogger()
original_levels[root_logger] = root_logger.level
root_logger.setLevel(logging.INFO)
for logger_name, logger in logging.root.manager.loggerDict.items():
if isinstance(logger, logging.Logger):
original_levels[logger] = logger.level
if logger.level <= logging.DEBUG:
logger.setLevel(logging.INFO)
yield
for logger, level in original_levels.items():
logger.setLevel(level)
# -----------------------------------------------
# Provide pytest options for specific test setups
# -----------------------------------------------
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption( parser.addoption(
@@ -184,7 +144,6 @@ def cfg_non_existent(request):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def user_cwd(config_default_dirs): def user_cwd(config_default_dirs):
"""Patch cwd provided by module pathlib.Path.cwd."""
with patch( with patch(
"pathlib.Path.cwd", "pathlib.Path.cwd",
return_value=config_default_dirs[1], return_value=config_default_dirs[1],
@@ -194,7 +153,6 @@ def user_cwd(config_default_dirs):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def user_config_dir(config_default_dirs): def user_config_dir(config_default_dirs):
"""Patch user_config_dir provided by module platformdirs."""
with patch( with patch(
"akkudoktoreos.config.config.user_config_dir", "akkudoktoreos.config.config.user_config_dir",
return_value=str(config_default_dirs[0]), return_value=str(config_default_dirs[0]),
@@ -204,7 +162,6 @@ def user_config_dir(config_default_dirs):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def user_data_dir(config_default_dirs): def user_data_dir(config_default_dirs):
"""Patch user_data_dir provided by module platformdirs."""
with patch( with patch(
"akkudoktoreos.config.config.user_data_dir", "akkudoktoreos.config.config.user_data_dir",
return_value=str(config_default_dirs[-1] / "data"), return_value=str(config_default_dirs[-1] / "data"),
@@ -232,18 +189,14 @@ def config_eos(
config_file_cwd = config_default_dirs[1] / ConfigEOS.CONFIG_FILE_NAME config_file_cwd = config_default_dirs[1] / ConfigEOS.CONFIG_FILE_NAME
assert not config_file.exists() assert not config_file.exists()
assert not config_file_cwd.exists() assert not config_file_cwd.exists()
config_eos = get_config() config_eos = get_config()
config_eos.reset_settings() config_eos.reset_settings()
assert config_file == config_eos.general.config_file_path assert config_file == config_eos.general.config_file_path
assert config_file.exists() assert config_file.exists()
assert not config_file_cwd.exists() assert not config_file_cwd.exists()
# Check user data directory pathes (config_default_dirs[-1] == data_default_dir_user)
assert config_default_dirs[-1] / "data" == config_eos.general.data_folder_path assert config_default_dirs[-1] / "data" == config_eos.general.data_folder_path
assert config_default_dirs[-1] / "data/cache" == config_eos.cache.path() assert config_default_dirs[-1] / "data/cache" == config_eos.cache.path()
assert config_default_dirs[-1] / "data/output" == config_eos.general.data_output_path assert config_default_dirs[-1] / "data/output" == config_eos.general.data_output_path
assert config_default_dirs[-1] / "data/output/eos.log" == config_eos.logging.file_path
return config_eos return config_eos
@@ -476,7 +429,7 @@ def server(xprocess, config_eos, config_default_dirs) -> Generator[str, None, No
Provides URL of the server. Provides URL of the server.
""" """
# create url/port info to the server # create url/port info to the server
url = "http://127.0.0.1:8503" url = "http://0.0.0.0:8503"
class Starter(ProcessStarter): class Starter(ProcessStarter):
# Set environment before any subprocess run, to keep custom config dir # Set environment before any subprocess run, to keep custom config dir

View File

@@ -4,10 +4,12 @@ from typing import Union
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
from loguru import logger
from pydantic import ValidationError from pydantic import ValidationError
from akkudoktoreos.config.config import ConfigEOS, GeneralSettings from akkudoktoreos.config.config import ConfigEOS, GeneralSettings
from akkudoktoreos.core.logging import get_logger
logger = get_logger(__name__)
# overwrite config_mixin fixture from conftest # overwrite config_mixin fixture from conftest
@@ -302,7 +304,7 @@ def test_config_common_settings_timezone_none_when_coordinates_missing():
[0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0], [0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0],
) )
], ],
TypeError, ValueError,
), ),
# Invalid index (out of bound) # Invalid index (out of bound)
( (
@@ -314,7 +316,7 @@ def test_config_common_settings_timezone_none_when_coordinates_missing():
[0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0], [0.0, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0],
) )
], ],
TypeError, IndexError,
), ),
# Invalid index (no number) # Invalid index (no number)
( (
@@ -344,27 +346,14 @@ def test_config_common_settings_timezone_none_when_coordinates_missing():
) )
def test_set_nested_key(path, value, expected, exception, config_eos): def test_set_nested_key(path, value, expected, exception, config_eos):
if not exception: if not exception:
config_eos.set_nested_value(path, value) config_eos.set_config_value(path, value)
for expected_path, expected_value in expected: for expected_path, expected_value in expected:
actual_value = eval(f"config_eos.{expected_path}") assert eval(f"config_eos.{expected_path}") == expected_value
assert actual_value == expected_value, (
f"Expected {expected_value} at {expected_path}, but got {actual_value}"
)
else: else:
try: with pytest.raises(exception):
config_eos.set_nested_value(path, value) config_eos.set_config_value(path, value)
for expected_path, expected_value in expected: for expected_path, expected_value in expected:
actual_value = eval(f"config_eos.{expected_path}") assert eval(f"config_eos.{expected_path}") == expected_value
assert actual_value == expected_value, (
f"Expected {expected_value} at {expected_path}, but got {actual_value}"
)
pytest.fail(
f"Expected exception {exception} but none was raised. Set '{expected_path}' to '{actual_value}'"
)
except Exception as e:
assert isinstance(e, exception), (
f"Expected exception {exception}, but got {type(e)}: {e}"
)
@pytest.mark.parametrize( @pytest.mark.parametrize(
@@ -389,10 +378,10 @@ def test_set_nested_key(path, value, expected, exception, config_eos):
) )
def test_get_nested_key(path, expected_value, exception, config_eos): def test_get_nested_key(path, expected_value, exception, config_eos):
if not exception: if not exception:
assert config_eos.get_nested_value(path) == expected_value assert config_eos.get_config_value(path) == expected_value
else: else:
with pytest.raises(exception): with pytest.raises(exception):
config_eos.get_nested_value(path) config_eos.get_config_value(path)
def test_merge_settings_from_dict_invalid(config_eos): def test_merge_settings_from_dict_invalid(config_eos):

Some files were not shown because too many files have changed in this diff Show More