Discard naive timestamp when restoring event entity state (#189296)

This commit is contained in:
suraj_velip
2026-10-28 23:12:36 +01:00
committed by GitHub
parent 968532286c
commit cde3969919
2 changed files with 57 additions and 2 deletions
+9 -1
View File
@@ -164,7 +164,15 @@ class EventEntity(RestoreEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_)
and state.state is not None
and (event_data := await self.async_get_last_event_data())
):
self.__last_event_triggered = dt_util.parse_datetime(state.state)
restored = dt_util.parse_datetime(state.state)
if restored is not None and restored.tzinfo is None:
_LOGGER.warning(
"%s restored timestamp without timezone: %s",
self.entity_id,
state.state,
)
restored = None
self.__last_event_triggered = restored
self.__last_event_type = event_data.last_event_type
self.__last_event_attributes = event_data.last_event_attributes
+48 -1
View File
@@ -31,7 +31,10 @@ from homeassistant.components.event import (
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.const import CONF_PLATFORM, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.entity_platform import (
AddConfigEntryEntitiesCallback,
async_get_platforms,
)
from homeassistant.helpers.restore_state import STORAGE_KEY as RESTORE_STATE_KEY
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
@@ -173,6 +176,50 @@ async def test_restore_state(hass: HomeAssistant) -> None:
assert state.attributes["hello"] == "world"
@pytest.mark.usefixtures("enable_custom_integrations", "mock_event_platform")
async def test_restore_state_naive_timestamp(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test a restored naive timestamp is discarded instead of breaking events."""
mock_restore_cache_with_extra_data(
hass,
(
(
State(
"event.doorbell",
"2021-01-01T23:59:59.123",
),
{
"last_event_type": "double_press",
"last_event_attributes": {
"hello": "world",
},
},
),
),
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
assert "restored timestamp without timezone" in caplog.text
state = hass.states.get("event.doorbell")
assert state
assert state.state == STATE_UNKNOWN
assert state.attributes[ATTR_EVENT_TYPE] == "double_press"
platform = async_get_platforms(hass, DOMAIN)[0]
entity = platform.domain_entities["event.doorbell"]
entity._trigger_event("short_press")
entity.async_write_ha_state()
await hass.async_block_till_done()
state = hass.states.get("event.doorbell")
assert state
assert dt_util.parse_datetime(state.state) is not None
@pytest.mark.usefixtures("enable_custom_integrations", "mock_event_platform")
async def test_invalid_extra_restore_state(hass: HomeAssistant) -> None:
"""Test we restore state integration."""