mirror of
https://github.com/home-assistant/core.git
synced 2026-07-10 00:05:13 +01:00
Fix KNX telegram history migration for data of 2026.3 and earlier (#175396)
This commit is contained in:
committed by
Franck Nijhof
parent
aa58b373f8
commit
4be7cbc4e1
@@ -339,7 +339,20 @@ class Telegrams:
|
||||
)
|
||||
return
|
||||
|
||||
stored_telegrams = [self.dict_to_model(t) for t in json_data]
|
||||
# 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)
|
||||
default_migration_values = {
|
||||
"value": None,
|
||||
"payload": None,
|
||||
"dpt_main": None,
|
||||
"dpt_sub": None,
|
||||
"source_name": "",
|
||||
"destination_name": "",
|
||||
"data_secure": False,
|
||||
}
|
||||
stored_telegrams = [
|
||||
self.dict_to_model(default_migration_values | t) for t in json_data
|
||||
]
|
||||
try:
|
||||
if stored_telegrams:
|
||||
await self.store.store_many(stored_telegrams)
|
||||
|
||||
@@ -86,3 +86,47 @@ async def test_migrate_telegrams_json_to_sqlite(
|
||||
|
||||
# Verify legacy file removal
|
||||
assert not os.path.exists(legacy_path)
|
||||
|
||||
|
||||
async def test_migrate_telegrams_json_missing_keys(
|
||||
hass: HomeAssistant,
|
||||
knx: KNXTestKit,
|
||||
) -> None:
|
||||
"""Test migrating telegrams from legacy JSON missing fields (e.g., data_secure)."""
|
||||
store = Store[dict[str, Any]](hass, version=1, key="knx/telegrams_history.json")
|
||||
legacy_path = store.path
|
||||
|
||||
legacy_data = [
|
||||
{
|
||||
"destination": "3/2/100",
|
||||
"source": "1.0.6",
|
||||
"direction": "Incoming",
|
||||
"payload": [0],
|
||||
"telegramtype": "GroupValueWrite",
|
||||
"timestamp": "2026-05-07T23:34:45.127015+02:00",
|
||||
"value": 0,
|
||||
}
|
||||
]
|
||||
|
||||
await store.async_save(legacy_data)
|
||||
|
||||
await knx.setup_integration()
|
||||
telegrams_module = hass.data[KNX_MODULE_KEY].telegrams
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify migration
|
||||
assert telegrams_module.store is not None
|
||||
result = await telegrams_module.store.query(TelegramQuery(order_descending=False))
|
||||
assert len(result.telegrams) == 1
|
||||
|
||||
# Check that data_secure defaults to False/None as appropriate, and others default to None or empty string
|
||||
assert result.telegrams[0].destination == "3/2/100"
|
||||
assert result.telegrams[0].data_secure is False
|
||||
assert result.telegrams[0].source_name == ""
|
||||
assert result.telegrams[0].destination_name == ""
|
||||
assert result.telegrams[0].dpt_main is None
|
||||
assert result.telegrams[0].dpt_sub is None
|
||||
|
||||
# Verify legacy file removal
|
||||
assert not os.path.exists(legacy_path)
|
||||
|
||||
Reference in New Issue
Block a user