From 4eb7be0709edd01e2aa168c4f80d744dfad5e6ac Mon Sep 17 00:00:00 2001 From: Philipp Waller <1090452+philippwaller@users.noreply.github.com> Date: Thu, 24 Sep 2026 18:22:28 +0200 Subject: [PATCH] Fix KNX telegram history timezone (#183052) --- homeassistant/components/knx/telegrams.py | 2 +- tests/components/knx/test_telegrams.py | 24 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/knx/telegrams.py b/homeassistant/components/knx/telegrams.py index 0afcea0ed37f..d4c838ee066d 100644 --- a/homeassistant/components/knx/telegrams.py +++ b/homeassistant/components/knx/telegrams.py @@ -491,7 +491,7 @@ class Telegrams: dpt_name, unit = self._resolve_dpt(m.dpt_main, m.dpt_sub) return TelegramDict( - timestamp=m.timestamp.isoformat(), + timestamp=dt_util.as_local(m.timestamp).isoformat(), source=m.source, destination=m.destination, direction=m.direction, diff --git a/tests/components/knx/test_telegrams.py b/tests/components/knx/test_telegrams.py index 311afc6ab4db..4047abfe4ce2 100644 --- a/tests/components/knx/test_telegrams.py +++ b/tests/components/knx/test_telegrams.py @@ -118,11 +118,31 @@ async def test_store_telegram_history( assert result.telegrams[1].destination == "2/2/2" +@pytest.mark.parametrize( + ("time_zone", "timestamp"), + [ + pytest.param("UTC", "2024-07-01T12:00:00.123456+00:00", id="utc"), + pytest.param( + "Europe/Berlin", "2024-07-01T14:00:00.123456+02:00", id="berlin-summer" + ), + pytest.param( + "Europe/Berlin", "2024-01-01T13:00:00.123456+01:00", id="berlin-winter" + ), + pytest.param( + "America/New_York", "2024-07-01T08:00:00.123456-04:00", id="new-york" + ), + ], +) async def test_store_telegram_history_sqlite( hass: HomeAssistant, knx: KNXTestKit, + freezer: FrozenDateTimeFactory, + time_zone: str, + timestamp: str, ) -> None: - """Test storing telegram history in SQLite.""" + """Test SQLite history preserves the live timestamp, including its time zone.""" + await hass.config.async_set_time_zone(time_zone) + freezer.move_to(timestamp) await knx.setup_integration(real_telegram_store=True) telegrams_module = hass.data[KNX_MODULE_KEY].telegrams @@ -137,6 +157,8 @@ async def test_store_telegram_history_sqlite( ) assert len(result.telegrams) == 1 assert result.telegrams[0].destination == "1/3/4" + assert telegrams_module.last_ga_telegrams["1/3/4"]["timestamp"] == timestamp + assert telegrams_module.model_to_dict(result.telegrams[0])["timestamp"] == timestamp @pytest.mark.parametrize(