diff --git a/homeassistant/components/recorder/history/legacy.py b/homeassistant/components/recorder/history/legacy.py index 3a0fe79455b576..dc49ebb9768ad7 100644 --- a/homeassistant/components/recorder/history/legacy.py +++ b/homeassistant/components/recorder/history/legacy.py @@ -447,7 +447,7 @@ def _get_states_for_entities_stmt( ) # We got an include-list of entities, accelerate the query by filtering already # 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( ( 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( 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 += ( lambda q: q.filter( States.last_updated_ts < utc_point_in_time_ts, diff --git a/homeassistant/components/recorder/history/modern.py b/homeassistant/components/recorder/history/modern.py index 902f1b5dc24b2b..01551de1f28d65 100644 --- a/homeassistant/components/recorder/history/modern.py +++ b/homeassistant/components/recorder/history/modern.py @@ -250,7 +250,7 @@ def get_significant_states_with_session( oldest_ts := _get_oldest_possible_ts(hass, start_time) ): 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) single_metadata_id = metadata_ids[0] if len(metadata_ids) == 1 else None stmt = lambda_stmt( @@ -415,7 +415,7 @@ def state_changes_during_period( oldest_ts := _get_oldest_possible_ts(hass, start_time) ): 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) stmt = lambda_stmt( lambda: _state_changed_during_period_stmt( diff --git a/homeassistant/components/recorder/models/legacy.py b/homeassistant/components/recorder/models/legacy.py index 21a8a39ba0fc4c..a469aa49ab2545 100644 --- a/homeassistant/components/recorder/models/legacy.py +++ b/homeassistant/components/recorder/models/legacy.py @@ -46,7 +46,7 @@ def __init__( # pylint: disable=super-init-not-called self.state = self._row.state or "" self._attributes: dict[str, Any] | None = None 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._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), } 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: row_last_updated_ts: float = row.last_updated_ts comp_state[COMPRESSED_STATE_LAST_UPDATED] = row_last_updated_ts diff --git a/homeassistant/util/dt.py b/homeassistant/util/dt.py index ee2b6c762d8cbe..eb898e4b54447d 100644 --- a/homeassistant/util/dt.py +++ b/homeassistant/util/dt.py @@ -13,6 +13,8 @@ from aiozoneinfo import async_get_time_zone as _async_get_time_zone import ciso8601 +from homeassistant.helpers.deprecation import deprecated_function + DATE_STR_FORMAT = "%Y-%m-%d" UTC = dt.UTC DEFAULT_TIME_ZONE: dt.tzinfo = dt.UTC @@ -170,6 +172,7 @@ def as_local(dattim: dt.datetime) -> dt.datetime: """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: """Fast conversion of a datetime in UTC to a timestamp.""" # Taken from diff --git a/tests/common.py b/tests/common.py index 3ec3f6d844c62b..ac6f10b8c44ef8 100644 --- a/tests/common.py +++ b/tests/common.py @@ -491,7 +491,7 @@ def async_fire_time_changed( def _async_fire_time_changed( hass: HomeAssistant, utc_datetime: datetime | None, fire_all: bool ) -> None: - timestamp = dt_util.utc_to_timestamp(utc_datetime) + timestamp = utc_datetime.timestamp() for task in list(get_scheduled_timer_handles(hass.loop)): if not isinstance(task, asyncio.TimerHandle): continue diff --git a/tests/components/logbook/common.py b/tests/components/logbook/common.py index afa8b7fcde5fe8..abb118467f49d3 100644 --- a/tests/components/logbook/common.py +++ b/tests/components/logbook/common.py @@ -35,7 +35,7 @@ def __init__( self.event_data = json.dumps(data, cls=JSONEncoder) self.data = data 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 = ( ulid_to_bytes_or_none(context.parent_id) if context else None ) diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index 8ac7dde67abf24..841c8ed1247700 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -330,7 +330,7 @@ def create_state_changed_event_from_old_new( row_id=1, event_type=PSEUDO_EVENT_STATE_CHANGED, 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_user_id_bin=None, context_parent_id_bin=None, diff --git a/tests/components/recorder/db_schema_32.py b/tests/components/recorder/db_schema_32.py index 6da0272da87667..39ddb8e314838e 100644 --- a/tests/components/recorder/db_schema_32.py +++ b/tests/components/recorder/db_schema_32.py @@ -254,7 +254,7 @@ def from_event(event: Event) -> Events: event_data=None, origin_idx=EVENT_ORIGIN_TO_IDX.get(event.origin), 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_user_id=event.context.user_id, context_parent_id=event.context.parent_id, @@ -429,16 +429,16 @@ def from_event(event: Event) -> States: # None state means the state was removed from the state machine if state is None: 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 return dbstate 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: dbstate.last_changed_ts = None else: - dbstate.last_changed_ts = dt_util.utc_to_timestamp(state.last_changed) + dbstate.last_changed_ts = state.last_changed.timestamp() return dbstate diff --git a/tests/components/recorder/db_schema_42.py b/tests/components/recorder/db_schema_42.py index 99bdbb28f2c2b7..efeade46562995 100644 --- a/tests/components/recorder/db_schema_42.py +++ b/tests/components/recorder/db_schema_42.py @@ -687,7 +687,7 @@ def from_stats(cls, metadata_id: int, stats: StatisticData) -> Self: created=None, created_ts=time.time(), start=None, - start_ts=dt_util.utc_to_timestamp(stats["start"]), + start_ts=stats["start"].timestamp(), mean=stats.get("mean"), min=stats.get("min"), max=stats.get("max"), diff --git a/tests/components/recorder/db_schema_43.py b/tests/components/recorder/db_schema_43.py index 26d8ecd6856507..8e77e8782ee345 100644 --- a/tests/components/recorder/db_schema_43.py +++ b/tests/components/recorder/db_schema_43.py @@ -697,7 +697,7 @@ def from_stats(cls, metadata_id: int, stats: StatisticData) -> Self: created=None, created_ts=time.time(), start=None, - start_ts=dt_util.utc_to_timestamp(stats["start"]), + start_ts=stats["start"].timestamp(), mean=stats.get("mean"), min=stats.get("min"), max=stats.get("max"), diff --git a/tests/components/recorder/test_purge.py b/tests/components/recorder/test_purge.py index f721a260c14e05..076f6ae8baba7f 100644 --- a/tests/components/recorder/test_purge.py +++ b/tests/components/recorder/test_purge.py @@ -564,7 +564,7 @@ async def _add_db_entries(hass: HomeAssistant, timestamp: datetime) -> None: event_type="EVENT_TEST_PURGE", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) session.add( @@ -572,8 +572,8 @@ async def _add_db_entries(hass: HomeAssistant, timestamp: datetime) -> None: entity_id="test.recorder2", state="purgeme", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=1001, attributes_id=1002, ) @@ -635,7 +635,7 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N event_type="KEEP", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp_keep), + time_fired_ts=timestamp_keep.timestamp(), ) ) session.add( @@ -643,8 +643,8 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N entity_id="test.cutoff", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp_keep), - last_updated_ts=dt_util.utc_to_timestamp(timestamp_keep), + last_changed_ts=timestamp_keep.timestamp(), + last_updated_ts=timestamp_keep.timestamp(), event_id=1000, attributes_id=1000, ) @@ -663,7 +663,7 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N event_type="PURGE", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp_purge), + time_fired_ts=timestamp_purge.timestamp(), ) ) session.add( @@ -671,8 +671,8 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N entity_id="test.cutoff", state="purge", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp_purge), - last_updated_ts=dt_util.utc_to_timestamp(timestamp_purge), + last_changed_ts=timestamp_purge.timestamp(), + last_updated_ts=timestamp_purge.timestamp(), event_id=1000 + row, attributes_id=1000 + row, ) @@ -821,8 +821,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.excluded", state="purgeme", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), ) ) # Add states and state_changed events that should be keeped @@ -847,8 +847,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=1, state_attributes=state_attrs, ) @@ -857,8 +857,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=2, state_attributes=state_attrs, ) @@ -866,8 +866,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=62, # keep state_attributes=state_attrs, ) @@ -879,7 +879,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type="EVENT_KEEP", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) convert_pending_states_to_meta(recorder_mock, session) @@ -1016,8 +1016,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.excluded", state="purgeme", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), ) ) # Add states and state_changed events that should be keeped @@ -1042,8 +1042,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=1, state_attributes=state_attrs, ) @@ -1052,8 +1052,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=2, state_attributes=state_attrs, ) @@ -1061,8 +1061,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=62, # keep state_attributes=state_attrs, ) @@ -1074,7 +1074,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type="EVENT_KEEP", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) convert_pending_states_to_meta(recorder_mock, session) @@ -1228,8 +1228,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.old_format", state=STATE_ON, attributes=json.dumps({"old": "not_using_state_attributes"}), - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=event_id, state_attributes=None, ) @@ -1240,7 +1240,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type=EVENT_STATE_CHANGED, event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) session.add( @@ -1249,7 +1249,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type=EVENT_THEMES_UPDATED, event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) convert_pending_states_to_meta(recorder_mock, session) @@ -1304,7 +1304,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type="EVENT_PURGE", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) @@ -1411,7 +1411,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type="EVENT_KEEP", event_data="{}", 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 @@ -1420,8 +1420,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=1, ) timestamp = dt_util.utcnow() - timedelta(days=4) @@ -1429,16 +1429,16 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=2, ) state_3 = States( entity_id="sensor.linked_old_state_id", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), old_state_id=62, # keep ) session.add_all((state_1, state_2, state_3)) @@ -1448,7 +1448,7 @@ def _add_db_entries(hass: HomeAssistant) -> None: event_type="excluded_event", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) session.add( @@ -1456,8 +1456,8 @@ def _add_db_entries(hass: HomeAssistant) -> None: entity_id="sensor.old_format", state="remove", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), ) ) convert_pending_events_to_event_types(recorder_mock, session) @@ -1823,8 +1823,8 @@ def _add_state_without_event_linkage( entity_id=entity_id, state=state, attributes=None, - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=None, state_attributes=state_attrs, ) @@ -1848,8 +1848,8 @@ def _add_state_with_state_attributes( entity_id=entity_id, state=state, attributes=None, - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=event_id, state_attributes=state_attrs, ) @@ -1971,7 +1971,7 @@ def _insert_events(): Events( event_type=None, 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( @@ -2101,7 +2101,7 @@ def _insert_states(): States( metadata_id=metadata_id, state="any", - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_updated_ts=timestamp.timestamp(), ) ) return recorder_mock.states_meta_manager.get_many( diff --git a/tests/components/recorder/test_purge_v32_schema.py b/tests/components/recorder/test_purge_v32_schema.py index 468fd38c85591c..2bd1e7fd7f789f 100644 --- a/tests/components/recorder/test_purge_v32_schema.py +++ b/tests/components/recorder/test_purge_v32_schema.py @@ -509,7 +509,7 @@ async def _add_db_entries(hass: HomeAssistant, timestamp: datetime) -> None: event_type="EVENT_TEST_PURGE", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) session.add( @@ -517,8 +517,8 @@ async def _add_db_entries(hass: HomeAssistant, timestamp: datetime) -> None: entity_id="test.recorder2", state="purgeme", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=1001, attributes_id=1002, ) @@ -576,7 +576,7 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N event_type="KEEP", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp_keep), + time_fired_ts=timestamp_keep.timestamp(), ) ) session.add( @@ -584,8 +584,8 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N entity_id="test.cutoff", state="keep", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp_keep), - last_updated_ts=dt_util.utc_to_timestamp(timestamp_keep), + last_changed_ts=timestamp_keep.timestamp(), + last_updated_ts=timestamp_keep.timestamp(), event_id=1000, attributes_id=1000, ) @@ -604,7 +604,7 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N event_type="PURGE", event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp_purge), + time_fired_ts=timestamp_purge.timestamp(), ) ) session.add( @@ -612,8 +612,8 @@ async def _add_db_entries(hass: HomeAssistant, cutoff: datetime, rows: int) -> N entity_id="test.cutoff", state="purge", attributes="{}", - last_changed_ts=dt_util.utc_to_timestamp(timestamp_purge), - last_updated_ts=dt_util.utc_to_timestamp(timestamp_purge), + last_changed_ts=timestamp_purge.timestamp(), + last_updated_ts=timestamp_purge.timestamp(), event_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_data=json.dumps(event_data), 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( event_type=event_type, origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), event_data_rel=event_data, ) ) @@ -910,8 +910,8 @@ def _add_state_without_event_linkage( entity_id=entity_id, state=state, attributes=None, - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=None, state_attributes=state_attrs, ) @@ -935,8 +935,8 @@ def _add_state_and_state_changed_event( entity_id=entity_id, state=state, attributes=None, - last_changed_ts=dt_util.utc_to_timestamp(timestamp), - last_updated_ts=dt_util.utc_to_timestamp(timestamp), + last_changed_ts=timestamp.timestamp(), + last_updated_ts=timestamp.timestamp(), event_id=event_id, state_attributes=state_attrs, ) @@ -947,7 +947,7 @@ def _add_state_and_state_changed_event( event_type=EVENT_STATE_CHANGED, event_data="{}", origin="LOCAL", - time_fired_ts=dt_util.utc_to_timestamp(timestamp), + time_fired_ts=timestamp.timestamp(), ) ) diff --git a/tests/util/test_dt.py b/tests/util/test_dt.py index 0e8432bbb8307e..347e92d6056c38 100644 --- a/tests/util/test_dt.py +++ b/tests/util/test_dt.py @@ -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.""" utc_now = dt_util.utcnow() 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: