Skip to content

Commit

Permalink
Deprecate dt_util.utc_to_timestamp (home-assistant#131787)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Nov 28, 2024
1 parent 0389800 commit bbce183
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 80 deletions.
4 changes: 2 additions & 2 deletions homeassistant/components/recorder/history/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 := (
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/recorder/history/modern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/recorder/models/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/util/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/components/logbook/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion tests/components/logbook/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tests/components/recorder/db_schema_32.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/components/recorder/db_schema_42.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion tests/components/recorder/db_schema_43.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Loading

0 comments on commit bbce183

Please sign in to comment.