Fix KNX telegram history migration for data of 2026.3 and earlier#175396
Conversation
|
Hey there @Julius2342, @farmio, @marvin-w, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a setup-blocking KeyError: 'data_secure' in the KNX integration (issue #175346) that occurs when migrating pre-existing telegram history from the legacy JSON store to the newer SQLite store. The data_secure field was introduced in 2026.3, so telegram history saved by older Home Assistant versions lacks that key (and potentially other newer fields). The fix makes dict_to_model tolerant of missing fields by falling back to sensible defaults, and adds a regression test.
Changes:
dict_to_modelnow usest.get(...)with defaults for the newer/optional fields (value,payload,dpt_main,dpt_sub,source_name,destination_name,data_secure) instead of hard key access, so legacy entries migrate gracefully.- Core fields that have always existed (
timestamp,source,destination,direction,telegramtype) still use direct key access. - Adds
test_migrate_telegrams_json_missing_keysto verify that a legacy telegram missing the newer fields migrates with the expected default values.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| homeassistant/components/knx/telegrams.py | Uses .get() with defaults for optional/newer telegram fields during model conversion to avoid KeyError on legacy JSON migration. |
| tests/components/knx/test_telegrams_migration.py | Adds a regression test that migrates a legacy telegram missing newer fields and asserts default values are applied. |
The change is minimal, targeted, and correct: dict_to_model is invoked both for live telegrams (which always contain every key via telegram_to_dict) and for migration (legacy dicts that may lack newer keys), and the .get() defaults do not alter the live path. The defaults align with the model typing and the reporter's suggested fix, and the new test covers the reported scenario. I found no blocking issues.
| dpt_sub=t.get("dpt_sub"), | ||
| source_name=t.get("source_name", ""), | ||
| destination_name=t.get("destination_name", ""), | ||
| data_secure=t.get("data_secure", False), |
There was a problem hiding this comment.
This is only missing when migrating data from old HA instances. The other keys should always be there.
Since this is quite a hot path in our code (every telegram received will go throughdict_to_model() on reception) I think we should not be too wary with that.
As an alternative that only effects migration, not live data, we could do in migrate_telegrams()
stored_telegrams = [self.dict_to_model({"data_secure": False} | t) for t in json_data]
# or
stored_telegrams = []
for t in json_data:
t.setdefault("data_secure": False) # was added in 2026.3
stored_telegrams.append(self.dict_to_model(t))
farmio
left a comment
There was a problem hiding this comment.
Anyway, the fix will work and I don't know if there is any reasonable speed penalty for those get() calls. So fine for me, we have a test case and can always do speed improvements later.
|
I think you have a fair point, probably less for speed reasons but in the production code, it could mask other issues. All other migrations should be handeled by telegram store. Let's see if we can move the change. |
59cc9f0 to
3f9d28f
Compare
3f9d28f to
17a049d
Compare
17a049d to
82c334b
Compare
| # Legacy JSON data from older HA instances might miss fields added later | ||
| # (e.g., data_secure was added in 2026.3, value, payload, dpt_main/sub, names might also be missing) |
| with pytest.raises(KeyError, match="dpt_main"): | ||
| telegrams_module.dict_to_model(incomplete_dict) |
82c334b to
9ed406d
Compare
Breaking change
None
Proposed change
When migrating old KNX telegram data from JSON -> SQLite DB, fall back to default values. We introduced data_secure field in 2026.3 and this change guards against missing fields like this.
Type of change
Migration fix.
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
n/a
If the code communicates with devices, web services, or third-party tools:
n/a
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: