Skip to content

Migrate configuration to Pydantic#484

Draft
jsimonetti wants to merge 19 commits intocorneel27:mainfrom
jsimonetti:pydantic_configuration
Draft

Migrate configuration to Pydantic#484
jsimonetti wants to merge 19 commits intocorneel27:mainfrom
jsimonetti:pydantic_configuration

Conversation

@jsimonetti
Copy link
Collaborator

@jsimonetti jsimonetti commented Dec 11, 2025

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 ha to database_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:

  • Scraping existing code for field usage and validation logic
  • Extracting information from DOCS.md
  • Inferring field purposes from context and variable names
  • Adding descriptions to all fields where possible

Some field descriptions may be incomplete or imprecise. Corrections and improvements are welcome.

What Changed

New Features

  • Type Safety: All configuration fields are now typed and validated
  • JSON Schema: Auto-generated schema for IDE autocomplete (config_schema.json)
  • Documentation: Auto-generated SETTINGS.md from model metadata
  • Migrations: Automatic migration from legacy configuration format
  • Validation: GitHub Actions ensures all fields have documentation
  • Fallback Mode: If validation fails, falls back to legacy behavior

New Files

  • dao/prog/config/ - Pydantic model definitions organized by category
  • dao/prog/config/versions/v0.py - Main configuration model
  • dao/prog/config/migrations/ - Migration system
  • dao/prog/config/generate_docs.py - Documentation generator
  • SETTINGS.md - Auto-generated configuration reference
  • config_schema.json - JSON Schema for IDE support
  • DEVELOPER_GUIDE.md - Guide for extending configuration
  • .github/workflows/generate-docs.yml - CI for documentation validation

Modified Files

  • dao/prog/da_config.py - Updated to use Pydantic models
  • dao/requirements.txt - Added pydantic dependency
  • .gitignore - Ignore Python cache files

Testing

The implementation preserves backward compatibility:

  • Existing configurations should work without changes
  • New type validation catches errors early
  • Fallback mode prevents breaking existing deployments

Known Limitations

  • Some field descriptions may need refinement
  • Migration from very old configurations may need testing
  • Not all edge cases may be covered

Future Work

  • Improve field descriptions based on maintainer feedback
  • Add more comprehensive validation rules
  • Extend documentation with examples

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)

  • Migrate scheduler freeform setting to a SchedulerConfig model
  • Migrate Boolean values to be actual booleans instead of str | boolean
  • Add json schema validation logic and checks
  • Change model and field populate_by_name to make saving the field name instead of alias (possibly with spaces)
  • Create wiki pages from the configuration models similar to SETTINGS.md, but push them to the wiki instead.
  • Alter calls from DAO in to settings to make full use of the pydantic model, instead of the limited compatibility layer
  • Save options.json back to disk after migration

@jsimonetti jsimonetti requested a review from corneel27 December 11, 2025 13:39
@jsimonetti jsimonetti self-assigned this Dec 11, 2025
@jsimonetti jsimonetti changed the title Migrate configuration to Pydantic with validation and auto-generated … Migrate configuration to Pydantic Dec 11, 2025
@jsimonetti
Copy link
Collaborator Author

I am unsure why CodeQL fails, since I did not touch the relevant db_manager.py file.

@jsimonetti jsimonetti force-pushed the pydantic_configuration branch from 0636ac7 to 95c6cc4 Compare December 11, 2025 14:29
@corneel27
Copy link
Owner

I am unsure why CodeQL fails, since I did not touch the relevant db_manager.py file.

First time same fail for my.
I investigated the message and I come to the conclusion: "false alarm"
The password will never printed to the logfile and the logfile stays locale.

You can dismiss the alerts.

@corneel27
Copy link
Owner

I will investigate your work but not in a short time.
My "roadmap" is as follows:

  1. Finish the rework for boiler/heatpump (this week, week 50)
  2. Make a test-version of the implementation of your solar prediction module (next week, week 51)
  3. Implement off user definable formules for the tariffs for consumption and production (the weeks of Christmas and New Year, weeks 52/2025 and week 1/2026)
  4. This migration (week 2/2026)

Is that OK for you?

What is the best way to test?
Can I make a local branche of https://github.com/jsimonetti/day-ahead/tree/pydantic_configuration?

@jsimonetti
Copy link
Collaborator Author

@jsimonetti jsimonetti force-pushed the pydantic_configuration branch 14 times, most recently from 7945bbe to 749cb9a Compare December 19, 2025 10:33
@jsimonetti jsimonetti force-pushed the pydantic_configuration branch 4 times, most recently from 559bfd5 to ebffd98 Compare December 20, 2025 13:59
@jsimonetti
Copy link
Collaborator Author

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.
Perhaps we can ask a third person to review those parts, so you can focus on the technical implementation part.

@jsimonetti jsimonetti force-pushed the pydantic_configuration branch 3 times, most recently from 01b735e to 25a160d Compare December 20, 2025 14:40
@jsimonetti jsimonetti force-pushed the pydantic_configuration branch 2 times, most recently from 611f880 to 1ba895f Compare December 20, 2025 15:11
jsimonetti and others added 19 commits January 21, 2026 10:25
…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>
@jsimonetti jsimonetti force-pushed the pydantic_configuration branch from 2c97a28 to 3c06188 Compare January 21, 2026 09:25
@jsimonetti
Copy link
Collaborator Author

To give you an idea where this could lead, I made https://github.com/jsimonetti/dao-config-test
This is just the config ui (nothing else) based on the pydantic models.

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.
Hopefully this will shed some light on the posibilities of this.

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.

@corneel27
Copy link
Owner

I will give it a look soon.
A first sight on the documentation: I think we must do it fast.
Please give me a week or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants