Migrate configuration to Pydantic#484
Conversation
|
I am unsure why CodeQL fails, since I did not touch the relevant |
0636ac7 to
95c6cc4
Compare
First time same fail for my. You can dismiss the alerts. |
|
I will investigate your work but not in a short time.
Is that OK for you? What is the best way to test? |
|
This is absolutely fine! |
7945bbe to
749cb9a
Compare
559bfd5 to
ebffd98
Compare
|
I have added even more help and ui-section/ui-group hints to the schema. Those ui hints can be ignored for now and serve as example for future usage. The help and validation hints will need to be looked at more carefully to make sure they make sense to an end user. |
01b735e to
25a160d
Compare
611f880 to
1ba895f
Compare
…documentation Replaces manual JSON configuration parsing with Pydantic models for type safety, validation, and automatic documentation generation. Includes migration system for backward compatibility. Changes: - Pydantic models for all configuration sections (battery, solar, EV, pricing, etc.) - Automatic migration from legacy configuration format - JSON Schema generation for IDE autocomplete - Auto-generated SETTINGS.md documentation - GitHub Actions workflow for documentation validation - Developer guide for extending configuration Technical details: - Zero hardcoded knowledge: all documentation generated from Pydantic JSON schema - Validation ensures all model fields have descriptions - Fallback mode preserves existing functionality if validation fails - Supports secrets management via !secret syntax
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
This is a more explicit pattern Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
This will add in the self-describing of the code and also in the upcoming GUI. Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
We use the newly provided json extensions to generate better documentation. Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
- Updated SETTINGS.md - Updated config_schema.json Generated by GitHub Actions workflow
Signed-off-by: Jeroen Simonetti <jeroen@simonetti.nl>
2c97a28 to
3c06188
Compare
|
To give you an idea where this could lead, I made https://github.com/jsimonetti/dao-config-test The UI is completely finished and will take very little effort to make it merge ready. So it can follow this PR closely. It is based on models that are slightly different then the models from this PR, so I included them in the repo. However, the changes are minimal and should be made soon anyway, before the config version 1 is finalised. The entire ui is 100% autogenerated from the pydantic models in the source code. No human interaction is required and no other files are used. Currently this PR is missing the latest configuration changes (in particular the ml_prediction model is absent). This should be a relatively simple change once we agree on the overal direction. |
|
I will give it a look soon. |
Pydantic Configuration Migration
Overview
This PR migrates the configuration system from manual JSON parsing to Pydantic models with automatic validation, type safety, and documentation generation.
Why Pydantic
The previous implementation used manual dictionary access with scattered validation logic throughout the codebase. Pydantic centralizes validation in model definitions, catches configuration errors at startup rather than runtime and automatically generates documentation from the same source. This eliminates the maintenance burden of keeping documentation synchronized with code, reduces bugs from typos or missing fields, and makes the configuration API self-documenting.
This migration also establishes the foundation for future improvements. The versioned configuration models enable proper migration paths when configuration changes are needed. The next migration can be to change the config to be compatible with jsonforms usage of json schema (migrating from
database hatodatabase_ha. This migration can be handled completely by the config module without user intervention.Review Guidance
Please review with caution - errors and issues are expected. This is a substantial refactoring of the configuration layer. The author will handle all issues until the maintainer is satisfied to merge.
Documentation Effort
Best effort has been made to document all configuration models by:
Some field descriptions may be incomplete or imprecise. Corrections and improvements are welcome.
What Changed
New Features
New Files
dao/prog/config/- Pydantic model definitions organized by categorydao/prog/config/versions/v0.py- Main configuration modeldao/prog/config/migrations/- Migration systemdao/prog/config/generate_docs.py- Documentation generatorSETTINGS.md- Auto-generated configuration referenceconfig_schema.json- JSON Schema for IDE supportDEVELOPER_GUIDE.md- Guide for extending configuration.github/workflows/generate-docs.yml- CI for documentation validationModified Files
dao/prog/da_config.py- Updated to use Pydantic modelsdao/requirements.txt- Added pydantic dependency.gitignore- Ignore Python cache filesTesting
The implementation preserves backward compatibility:
Known Limitations
Future Work
Author commitment: I will address all issues, bugs, and feedback until this is merge-ready.
Assorted list of things to change to the config after this merge, before we add save logic:
(This is just a TODO list from the author, no actions are required by the maintainer)
str | booleanpopulate_by_nameto make saving the field name instead of alias (possibly with spaces)