From 7681eadd97aba421ff35506393a2cd48d2b8322d Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Mon, 19 Oct 2026 22:02:09 +0200 Subject: [PATCH] Lock the KNX telegram database while a backup copies it (#187309) Co-authored-by: Claude Fable 5.1 --- homeassistant/components/knx/backup.py | 57 +++++++++++ tests/components/knx/test_backup.py | 133 +++++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 homeassistant/components/knx/backup.py create mode 100644 tests/components/knx/test_backup.py diff --git a/homeassistant/components/knx/backup.py b/homeassistant/components/knx/backup.py new file mode 100644 index 000000000000..60d3b798ce02 --- /dev/null +++ b/homeassistant/components/knx/backup.py @@ -0,0 +1,57 @@ +"""Backup platform for the KNX integration.""" + +import asyncio +import logging + +from knx_telegram_store import ( + BufferedPostgresStore, + BufferedSqliteStore, + KnxTelegramStoreException, +) + +from homeassistant.core import HomeAssistant + +from .const import KNX_MODULE_KEY, KNX_TELEGRAM_BACKEND_SQLITE + +_LOGGER = logging.getLogger(__name__) + +# Bounds the wait for a running eviction or VACUUM to finish before the lock is taken +LOCK_TIMEOUT = 30 + + +def _sqlite_store( + hass: HomeAssistant, +) -> BufferedSqliteStore | BufferedPostgresStore | None: + """Return the telegram store whose file is part of the backup, if any.""" + if (knx_module := hass.data.get(KNX_MODULE_KEY)) is None: + return None + telegrams = knx_module.telegrams + if telegrams.backend != KNX_TELEGRAM_BACKEND_SQLITE: + return None + return telegrams.store + + +async def async_pre_backup(hass: HomeAssistant) -> None: + """Checkpoint the telegram database and hold off writes while it is copied.""" + if (store := _sqlite_store(hass)) is None: + return + try: + async with asyncio.timeout(LOCK_TIMEOUT): + await store.begin_backup() + except (KnxTelegramStoreException, TimeoutError) as err: + _LOGGER.warning( + "Could not prepare the KNX telegram database for the backup, " + "its copy may be inconsistent: %s", + err, + ) + + +async def async_post_backup(hass: HomeAssistant) -> None: + """Resume writing to the telegram database.""" + if (store := _sqlite_store(hass)) is None: + return + try: + await store.end_backup() + except KnxTelegramStoreException as err: + # pre-backup did not take the lock, or the store was replaced meanwhile + _LOGGER.debug("KNX telegram database was not locked for the backup: %s", err) diff --git a/tests/components/knx/test_backup.py b/tests/components/knx/test_backup.py new file mode 100644 index 000000000000..b498b2f070f4 --- /dev/null +++ b/tests/components/knx/test_backup.py @@ -0,0 +1,133 @@ +"""Test the KNX backup platform.""" + +import asyncio +from unittest.mock import AsyncMock, patch + +from knx_telegram_store import KnxTelegramStoreException, TelegramQuery +import pytest + +from homeassistant.components.backup import DATA_MANAGER +from homeassistant.components.knx.backup import async_post_backup, async_pre_backup +from homeassistant.components.knx.const import ( + CONF_KNX_TELEGRAM_DB_BACKEND, + CONF_KNX_TELEGRAM_DB_POSTGRES_DSN, + KNX_MODULE_KEY, + KNX_TELEGRAM_BACKEND_POSTGRES, +) +from homeassistant.core import HomeAssistant + +from .conftest import KNXTestKit + +from tests.components.backup.common import setup_backup_integration + + +async def test_backup_platform_is_registered( + hass: HomeAssistant, knx: KNXTestKit +) -> None: + """Test the backup integration picks up the KNX backup platform.""" + await knx.setup_integration() + await setup_backup_integration(hass) + + assert "knx" in hass.data[DATA_MANAGER].platforms + + +async def _stored_count(hass: HomeAssistant) -> int: + store = hass.data[KNX_MODULE_KEY].telegrams.store + assert store is not None + result = await store.query(TelegramQuery()) + return len(result.telegrams) + + +async def test_backup_holds_writes_until_post_backup( + hass: HomeAssistant, knx: KNXTestKit +) -> None: + """Test pre-backup flushes and then keeps telegrams in the buffer until post-backup.""" + await knx.setup_integration(real_telegram_store=True) + store = hass.data[KNX_MODULE_KEY].telegrams.store + assert store is not None + + await knx.receive_write("1/3/4", True) + await hass.async_block_till_done() + assert await _stored_count(hass) == 0 + + await async_pre_backup(hass) + assert await _stored_count(hass) == 1 + + await knx.receive_write("1/3/4", False) + await hass.async_block_till_done() + await store.flush() + assert await _stored_count(hass) == 1 + + await async_post_backup(hass) + await store.flush() + assert await _stored_count(hass) == 2 + + +async def test_pre_backup_store_error_is_logged( + hass: HomeAssistant, knx: KNXTestKit, caplog: pytest.LogCaptureFixture +) -> None: + """Test a store that cannot be locked does not abort the backup.""" + await knx.setup_integration() + store = hass.data[KNX_MODULE_KEY].telegrams.store + assert store is not None + + with patch.object( + store, "begin_backup", AsyncMock(side_effect=KnxTelegramStoreException("busy")) + ): + await async_pre_backup(hass) + assert "Could not prepare the KNX telegram database" in caplog.text + + await async_post_backup(hass) + + +async def test_pre_backup_timeout_is_logged( + hass: HomeAssistant, knx: KNXTestKit, caplog: pytest.LogCaptureFixture +) -> None: + """Test a lock that cannot be taken in time does not abort the backup.""" + await knx.setup_integration() + store = hass.data[KNX_MODULE_KEY].telegrams.store + assert store is not None + + async def _never() -> None: + await asyncio.Event().wait() + + with ( + patch.object(store, "begin_backup", _never), + patch("homeassistant.components.knx.backup.LOCK_TIMEOUT", 0), + ): + await async_pre_backup(hass) + assert "Could not prepare the KNX telegram database" in caplog.text + + +async def test_backup_is_noop_for_postgres_backend( + hass: HomeAssistant, knx: KNXTestKit +) -> None: + """Test a PostgreSQL store is left alone, its data is not part of the backup.""" + knx.mock_config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry( + knx.mock_config_entry, + options=knx.mock_config_entry.options + | { + CONF_KNX_TELEGRAM_DB_BACKEND: KNX_TELEGRAM_BACKEND_POSTGRES, + CONF_KNX_TELEGRAM_DB_POSTGRES_DSN: "postgresql://user:secret@db.local/knx", + }, + ) + mock_store = AsyncMock() + mock_store.needs_migration.return_value = False + with patch( + "homeassistant.components.knx.telegrams.BufferedPostgresStore", + return_value=mock_store, + ): + await knx.setup_integration(add_entry_to_hass=False, real_telegram_store=True) + + await async_pre_backup(hass) + await async_post_backup(hass) + + mock_store.begin_backup.assert_not_called() + mock_store.end_backup.assert_not_called() + + +async def test_backup_is_noop_without_knx(hass: HomeAssistant) -> None: + """Test the hooks do nothing when KNX is not set up.""" + await async_pre_backup(hass) + await async_post_backup(hass)