Deprecate dt_util.utc_to_timestamp (#131787)

This commit is contained in:
Erik Montnemery 2024-11-28 17:00:20 +01:00 committed by GitHub
parent 0389800e2a
commit bbce183faf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 87 additions and 80 deletions

View File

@ -447,7 +447,7 @@ def _get_states_for_entities_stmt(
) )
# We got an include-list of entities, accelerate the query by filtering already # We got an include-list of entities, accelerate the query by filtering already
# in the inner query. # in the inner query.
utc_point_in_time_ts = dt_util.utc_to_timestamp(utc_point_in_time) utc_point_in_time_ts = utc_point_in_time.timestamp()
stmt += lambda q: q.join( stmt += lambda q: q.join(
( (
most_recent_states_for_entities_by_date := ( most_recent_states_for_entities_by_date := (
@ -518,7 +518,7 @@ def _get_single_entity_states_stmt(
stmt, join_attributes = _lambda_stmt_and_join_attributes( stmt, join_attributes = _lambda_stmt_and_join_attributes(
no_attributes, include_last_changed=True no_attributes, include_last_changed=True
) )
utc_point_in_time_ts = dt_util.utc_to_timestamp(utc_point_in_time) utc_point_in_time_ts = utc_point_in_time.timestamp()
stmt += ( stmt += (
lambda q: q.filter( lambda q: q.filter(
States.last_updated_ts < utc_point_in_time_ts, States.last_updated_ts < utc_point_in_time_ts,

View File

@ -250,7 +250,7 @@ def get_significant_states_with_session(
oldest_ts := _get_oldest_possible_ts(hass, start_time) oldest_ts := _get_oldest_possible_ts(hass, start_time)
): ):
include_start_time_state = False include_start_time_state = False
start_time_ts = dt_util.utc_to_timestamp(start_time) start_time_ts = start_time.timestamp()
end_time_ts = datetime_to_timestamp_or_none(end_time) end_time_ts = datetime_to_timestamp_or_none(end_time)
single_metadata_id = metadata_ids[0] if len(metadata_ids) == 1 else None single_metadata_id = metadata_ids[0] if len(metadata_ids) == 1 else None
stmt = lambda_stmt( stmt = lambda_stmt(
@ -415,7 +415,7 @@ def state_changes_during_period(
oldest_ts := _get_oldest_possible_ts(hass, start_time) oldest_ts := _get_oldest_possible_ts(hass, start_time)
): ):
include_start_time_state = False include_start_time_state = False
start_time_ts = dt_util.utc_to_timestamp(start_time) start_time_ts = start_time.timestamp()
end_time_ts = datetime_to_timestamp_or_none(end_time) end_time_ts = datetime_to_timestamp_or_none(end_time)
stmt = lambda_stmt( stmt = lambda_stmt(
lambda: _state_changed_during_period_stmt( lambda: _state_changed_during_period_stmt(

View File

@ -46,7 +46,7 @@ class LegacyLazyState(State):
self.state = self._row.state or "" self.state = self._row.state or ""
self._attributes: dict[str, Any] | None = None self._attributes: dict[str, Any] | None = None
self._last_updated_ts: float | None = self._row.last_updated_ts or ( self._last_updated_ts: float | None = self._row.last_updated_ts or (
dt_util.utc_to_timestamp(start_time) if start_time else None start_time.timestamp() if start_time else None
) )
self._last_changed_ts: float | None = ( self._last_changed_ts: float | None = (
self._row.last_changed_ts or self._last_updated_ts self._row.last_changed_ts or self._last_updated_ts
@ -146,7 +146,7 @@ def legacy_row_to_compressed_state(
COMPRESSED_STATE_ATTRIBUTES: decode_attributes_from_row_legacy(row, attr_cache), COMPRESSED_STATE_ATTRIBUTES: decode_attributes_from_row_legacy(row, attr_cache),
} }
if start_time: if start_time:
comp_state[COMPRESSED_STATE_LAST_UPDATED] = dt_util.utc_to_timestamp(start_time) comp_state[COMPRESSED_STATE_LAST_UPDATED] = start_time.timestamp()
else: else:
row_last_updated_ts: float = row.last_updated_ts row_last_updated_ts: float = row.last_updated_ts
comp_state[COMPRESSED_STATE_LAST_UPDATED] = row_last_updated_ts comp_state[COMPRESSED_STATE_LAST_UPDATED] = row_last_updated_ts

View File

@ -13,6 +13,8 @@ import zoneinfo
from aiozoneinfo import async_get_time_zone as _async_get_time_zone from aiozoneinfo import async_get_time_zone as _async_get_time_zone
import ciso8601 import ciso8601
from homeassistant.helpers.deprecation import deprecated_function
DATE_STR_FORMAT = "%Y-%m-%d" DATE_STR_FORMAT = "%Y-%m-%d"
UTC = dt.UTC UTC = dt.UTC
DEFAULT_TIME_ZONE: dt.tzinfo = dt.UTC DEFAULT_TIME_ZONE: dt.tzinfo = dt.UTC
@ -170,6 +172,7 @@ utc_from_timestamp = partial(dt.datetime.fromtimestamp, tz=UTC)
"""Return a UTC time from a timestamp.""" """Return a UTC time from a timestamp."""
@deprecated_function("datetime.timestamp", breaks_in_ha_version="2026.1")
def utc_to_timestamp(utc_dt: dt.datetime) -> float: def utc_to_timestamp(utc_dt: dt.datetime) -> float:
"""Fast conversion of a datetime in UTC to a timestamp.""" """Fast conversion of a datetime in UTC to a timestamp."""
# Taken from # Taken from

View File

@ -491,7 +491,7 @@ _MONOTONIC_RESOLUTION = time.get_clock_info("monotonic").resolution
def _async_fire_time_changed( def _async_fire_time_changed(
hass: HomeAssistant, utc_datetime: datetime | None, fire_all: bool hass: HomeAssistant, utc_datetime: datetime | None, fire_all: bool
) -> None: ) -> None:
timestamp = dt_util.utc_to_timestamp(utc_datetime) timestamp = utc_datetime.timestamp()
for task in list(get_scheduled_timer_handles(hass.loop)): for task in list(get_scheduled_timer_handles(hass.loop)):
if not isinstance(task, asyncio.TimerHandle): if not isinstance(task, asyncio.TimerHandle):
continue continue

View File

@ -35,7 +35,7 @@ class MockRow:
self.event_data = json.dumps(data, cls=JSONEncoder) self.event_data = json.dumps(data, cls=JSONEncoder)
self.data = data self.data = data
self.time_fired = dt_util.utcnow() self.time_fired = dt_util.utcnow()
self.time_fired_ts = dt_util.utc_to_timestamp(self.time_fired) self.time_fired_ts = self.time_fired.timestamp()
self.context_parent_id_bin = ( self.context_parent_id_bin = (
ulid_to_bytes_or_none(context.parent_id) if context else None ulid_to_bytes_or_none(context.parent_id) if context else None
) )

View File

@ -330,7 +330,7 @@ def create_state_changed_event_from_old_new(
row_id=1, row_id=1,
event_type=PSEUDO_EVENT_STATE_CHANGED, event_type=PSEUDO_EVENT_STATE_CHANGED,
event_data="{}", event_data="{}",
time_fired_ts=dt_util.utc_to_timestamp(event_time_fired), time_fired_ts=event_time_fired.timestamp(),
context_id_bin=None, context_id_bin=None,
context_user_id_bin=None, context_user_id_bin=None,
context_parent_id_bin=None, context_parent_id_bin=None,

View File

@ -254,7 +254,7 @@ class Events(Base): # type: ignore[misc,valid-type]
event_data=None, event_data=None,
origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin), origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin),
time_fired=None, time_fired=None,
time_fired_ts=dt_util.utc_to_timestamp(event.time_fired), time_fired_ts=event.time_fired.timestamp(),
context_id=event.context.id, context_id=event.context.id,
context_user_id=event.context.user_id, context_user_id=event.context.user_id,
context_parent_id=event.context.parent_id, context_parent_id=event.context.parent_id,
@ -429,16 +429,16 @@ class States(Base): # type: ignore[misc,valid-type]
# None state means the state was removed from the state machine # None state means the state was removed from the state machine
if state is None: if state is None:
dbstate.state = "" dbstate.state = ""
dbstate.last_updated_ts = dt_util.utc_to_timestamp(event.time_fired) dbstate.last_updated_ts = event.time_fired.timestamp()
dbstate.last_changed_ts = None dbstate.last_changed_ts = None
return dbstate return dbstate
dbstate.state = state.state dbstate.state = state.state
dbstate.last_updated_ts = dt_util.utc_to_timestamp(state.last_updated) dbstate.last_updated_ts = state.last_updated.timestamp()
if state.last_updated == state.last_changed: if state.last_updated == state.last_changed:
dbstate.last_changed_ts = None dbstate.last_changed_ts = None
else: else:
dbstate.last_changed_ts = dt_util.utc_to_timestamp(state.last_changed) dbstate.last_changed_ts = state.last_changed.timestamp()
return dbstate return dbstate

View File

@ -687,7 +687,7 @@ class StatisticsBase:
created=None, created=None,
created_ts=time.time(), created_ts=time.time(),
start=None, start=None,
start_ts=dt_util.utc_to_timestamp(stats["start"]), start_ts=stats["start"].timestamp(),
mean=stats.get("mean"), mean=stats.get("mean"),
min=stats.get("min"), min=stats.get("min"),
max=stats.get("max"), max=stats.get("max"),

View File

@ -697,7 +697,7 @@ class StatisticsBase:
created=None, created=None,
created_ts=time.time(), created_ts=time.time(),
start=None, start=None,
start_ts=dt_util.utc_to_timestamp(stats["start"]), start_ts=stats["start"].timestamp(),
mean=stats.get("mean"), mean=stats.get("mean"),
min=stats.get("min"), min=stats.get("min"),
max=stats.get("max"), max=stats.get("max"),

View File

@ -564,7 +564,7 @@ async def test_purge_edge_case(
event_type="EVENT_TEST_PURGE", event_type="EVENT_TEST_PURGE",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
session.add( session.add(
@ -572,8 +572,8 @@ async def test_purge_edge_case(
entity_id="test.recorder2", entity_id="test.recorder2",
state="purgeme", state="purgeme",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=1001, event_id=1001,
attributes_id=1002, attributes_id=1002,
) )
@ -635,7 +635,7 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
event_type="KEEP", event_type="KEEP",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp_keep), time_fired_ts=timestamp_keep.timestamp(),
) )
) )
session.add( session.add(
@ -643,8 +643,8 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
entity_id="test.cutoff", entity_id="test.cutoff",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp_keep), last_changed_ts=timestamp_keep.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp_keep), last_updated_ts=timestamp_keep.timestamp(),
event_id=1000, event_id=1000,
attributes_id=1000, attributes_id=1000,
) )
@ -663,7 +663,7 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
event_type="PURGE", event_type="PURGE",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp_purge), time_fired_ts=timestamp_purge.timestamp(),
) )
) )
session.add( session.add(
@ -671,8 +671,8 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
entity_id="test.cutoff", entity_id="test.cutoff",
state="purge", state="purge",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp_purge), last_changed_ts=timestamp_purge.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp_purge), last_updated_ts=timestamp_purge.timestamp(),
event_id=1000 + row, event_id=1000 + row,
attributes_id=1000 + row, attributes_id=1000 + row,
) )
@ -821,8 +821,8 @@ async def test_purge_filtered_states(
entity_id="sensor.excluded", entity_id="sensor.excluded",
state="purgeme", state="purgeme",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
) )
) )
# Add states and state_changed events that should be keeped # Add states and state_changed events that should be keeped
@ -847,8 +847,8 @@ async def test_purge_filtered_states(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=1, old_state_id=1,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -857,8 +857,8 @@ async def test_purge_filtered_states(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=2, old_state_id=2,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -866,8 +866,8 @@ async def test_purge_filtered_states(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=62, # keep old_state_id=62, # keep
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -879,7 +879,7 @@ async def test_purge_filtered_states(
event_type="EVENT_KEEP", event_type="EVENT_KEEP",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
convert_pending_states_to_meta(recorder_mock, session) convert_pending_states_to_meta(recorder_mock, session)
@ -1016,8 +1016,8 @@ async def test_purge_filtered_states_multiple_rounds(
entity_id="sensor.excluded", entity_id="sensor.excluded",
state="purgeme", state="purgeme",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
) )
) )
# Add states and state_changed events that should be keeped # Add states and state_changed events that should be keeped
@ -1042,8 +1042,8 @@ async def test_purge_filtered_states_multiple_rounds(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=1, old_state_id=1,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -1052,8 +1052,8 @@ async def test_purge_filtered_states_multiple_rounds(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=2, old_state_id=2,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -1061,8 +1061,8 @@ async def test_purge_filtered_states_multiple_rounds(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=62, # keep old_state_id=62, # keep
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -1074,7 +1074,7 @@ async def test_purge_filtered_states_multiple_rounds(
event_type="EVENT_KEEP", event_type="EVENT_KEEP",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
convert_pending_states_to_meta(recorder_mock, session) convert_pending_states_to_meta(recorder_mock, session)
@ -1228,8 +1228,8 @@ async def test_purge_without_state_attributes_filtered_states_to_empty(
entity_id="sensor.old_format", entity_id="sensor.old_format",
state=STATE_ON, state=STATE_ON,
attributes=json.dumps({"old": "not_using_state_attributes"}), attributes=json.dumps({"old": "not_using_state_attributes"}),
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=event_id, event_id=event_id,
state_attributes=None, state_attributes=None,
) )
@ -1240,7 +1240,7 @@ async def test_purge_without_state_attributes_filtered_states_to_empty(
event_type=EVENT_STATE_CHANGED, event_type=EVENT_STATE_CHANGED,
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
session.add( session.add(
@ -1249,7 +1249,7 @@ async def test_purge_without_state_attributes_filtered_states_to_empty(
event_type=EVENT_THEMES_UPDATED, event_type=EVENT_THEMES_UPDATED,
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
convert_pending_states_to_meta(recorder_mock, session) convert_pending_states_to_meta(recorder_mock, session)
@ -1304,7 +1304,7 @@ async def test_purge_filtered_events(
event_type="EVENT_PURGE", event_type="EVENT_PURGE",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
@ -1411,7 +1411,7 @@ async def test_purge_filtered_events_state_changed(
event_type="EVENT_KEEP", event_type="EVENT_KEEP",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
# Add states with linked old_state_ids that need to be handled # Add states with linked old_state_ids that need to be handled
@ -1420,8 +1420,8 @@ async def test_purge_filtered_events_state_changed(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=1, old_state_id=1,
) )
timestamp = dt_util.utcnow() - timedelta(days=4) timestamp = dt_util.utcnow() - timedelta(days=4)
@ -1429,16 +1429,16 @@ async def test_purge_filtered_events_state_changed(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=2, old_state_id=2,
) )
state_3 = States( state_3 = States(
entity_id="sensor.linked_old_state_id", entity_id="sensor.linked_old_state_id",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
old_state_id=62, # keep old_state_id=62, # keep
) )
session.add_all((state_1, state_2, state_3)) session.add_all((state_1, state_2, state_3))
@ -1448,7 +1448,7 @@ async def test_purge_filtered_events_state_changed(
event_type="excluded_event", event_type="excluded_event",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
session.add( session.add(
@ -1456,8 +1456,8 @@ async def test_purge_filtered_events_state_changed(
entity_id="sensor.old_format", entity_id="sensor.old_format",
state="remove", state="remove",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
) )
) )
convert_pending_events_to_event_types(recorder_mock, session) convert_pending_events_to_event_types(recorder_mock, session)
@ -1823,8 +1823,8 @@ def _add_state_without_event_linkage(
entity_id=entity_id, entity_id=entity_id,
state=state, state=state,
attributes=None, attributes=None,
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=None, event_id=None,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -1848,8 +1848,8 @@ def _add_state_with_state_attributes(
entity_id=entity_id, entity_id=entity_id,
state=state, state=state,
attributes=None, attributes=None,
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=event_id, event_id=event_id,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -1971,7 +1971,7 @@ async def test_purge_old_events_purges_the_event_type_ids(
Events( Events(
event_type=None, event_type=None,
event_type_id=event_type.event_type_id, event_type_id=event_type.event_type_id,
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
return recorder_mock.event_type_manager.get_many( return recorder_mock.event_type_manager.get_many(
@ -2101,7 +2101,7 @@ async def test_purge_old_states_purges_the_state_metadata_ids(
States( States(
metadata_id=metadata_id, metadata_id=metadata_id,
state="any", state="any",
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
) )
) )
return recorder_mock.states_meta_manager.get_many( return recorder_mock.states_meta_manager.get_many(

View File

@ -509,7 +509,7 @@ async def test_purge_edge_case(hass: HomeAssistant, use_sqlite: bool) -> None:
event_type="EVENT_TEST_PURGE", event_type="EVENT_TEST_PURGE",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
session.add( session.add(
@ -517,8 +517,8 @@ async def test_purge_edge_case(hass: HomeAssistant, use_sqlite: bool) -> None:
entity_id="test.recorder2", entity_id="test.recorder2",
state="purgeme", state="purgeme",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=1001, event_id=1001,
attributes_id=1002, attributes_id=1002,
) )
@ -576,7 +576,7 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
event_type="KEEP", event_type="KEEP",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp_keep), time_fired_ts=timestamp_keep.timestamp(),
) )
) )
session.add( session.add(
@ -584,8 +584,8 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
entity_id="test.cutoff", entity_id="test.cutoff",
state="keep", state="keep",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp_keep), last_changed_ts=timestamp_keep.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp_keep), last_updated_ts=timestamp_keep.timestamp(),
event_id=1000, event_id=1000,
attributes_id=1000, attributes_id=1000,
) )
@ -604,7 +604,7 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
event_type="PURGE", event_type="PURGE",
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp_purge), time_fired_ts=timestamp_purge.timestamp(),
) )
) )
session.add( session.add(
@ -612,8 +612,8 @@ async def test_purge_cutoff_date(hass: HomeAssistant, recorder_mock: Recorder) -
entity_id="test.cutoff", entity_id="test.cutoff",
state="purge", state="purge",
attributes="{}", attributes="{}",
last_changed_ts=dt_util.utc_to_timestamp(timestamp_purge), last_changed_ts=timestamp_purge.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp_purge), last_updated_ts=timestamp_purge.timestamp(),
event_id=1000 + row, event_id=1000 + row,
attributes_id=1000 + row, attributes_id=1000 + row,
) )
@ -771,7 +771,7 @@ async def _add_test_events(hass: HomeAssistant, iterations: int = 1):
event_type=event_type, event_type=event_type,
event_data=json.dumps(event_data), event_data=json.dumps(event_data),
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )
@ -808,7 +808,7 @@ async def _add_events_with_event_data(hass: HomeAssistant, iterations: int = 1):
Events( Events(
event_type=event_type, event_type=event_type,
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
event_data_rel=event_data, event_data_rel=event_data,
) )
) )
@ -910,8 +910,8 @@ def _add_state_without_event_linkage(
entity_id=entity_id, entity_id=entity_id,
state=state, state=state,
attributes=None, attributes=None,
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=None, event_id=None,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -935,8 +935,8 @@ def _add_state_and_state_changed_event(
entity_id=entity_id, entity_id=entity_id,
state=state, state=state,
attributes=None, attributes=None,
last_changed_ts=dt_util.utc_to_timestamp(timestamp), last_changed_ts=timestamp.timestamp(),
last_updated_ts=dt_util.utc_to_timestamp(timestamp), last_updated_ts=timestamp.timestamp(),
event_id=event_id, event_id=event_id,
state_attributes=state_attrs, state_attributes=state_attrs,
) )
@ -947,7 +947,7 @@ def _add_state_and_state_changed_event(
event_type=EVENT_STATE_CHANGED, event_type=EVENT_STATE_CHANGED,
event_data="{}", event_data="{}",
origin="LOCAL", origin="LOCAL",
time_fired_ts=dt_util.utc_to_timestamp(timestamp), time_fired_ts=timestamp.timestamp(),
) )
) )

View File

@ -116,10 +116,14 @@ def test_utc_from_timestamp() -> None:
) )
def test_timestamp_to_utc() -> None: def test_timestamp_to_utc(caplog: pytest.LogCaptureFixture) -> None:
"""Test we can convert a utc datetime to a timestamp.""" """Test we can convert a utc datetime to a timestamp."""
utc_now = dt_util.utcnow() utc_now = dt_util.utcnow()
assert dt_util.utc_to_timestamp(utc_now) == utc_now.timestamp() assert dt_util.utc_to_timestamp(utc_now) == utc_now.timestamp()
assert (
"utc_to_timestamp is a deprecated function which will be removed "
"in HA Core 2026.1. Use datetime.timestamp instead" in caplog.text
)
def test_as_timestamp() -> None: def test_as_timestamp() -> None: