mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Switch utc_to_timestamp to .timestamp() where possible (#109729)
* Switch utc_to_timestamp to .timestamp() .timestamp() is faster now in newer cpython ``` from homeassistant.util.dt import utc_to_timestamp, utcnow import timeit now = utcnow() print(timeit.timeit('utc_to_timestamp(now)',globals={"now":now,"utc_to_timestamp":utc_to_timestamp})) print(timeit.timeit('now.timestamp()',globals={"now":now})) ``` utc_to_timestamp = 0.18721245788037777 timestamp = 0.11421508435159922 * compat * revert * revert * revert * revert * revert
This commit is contained in:
parent
e399bebbcd
commit
f73431ac06
@ -179,8 +179,8 @@ def _generate_stream_message(
|
|||||||
"""Generate a history stream message response."""
|
"""Generate a history stream message response."""
|
||||||
return {
|
return {
|
||||||
"states": states,
|
"states": states,
|
||||||
"start_time": dt_util.utc_to_timestamp(start_day),
|
"start_time": start_day.timestamp(),
|
||||||
"end_time": dt_util.utc_to_timestamp(end_day),
|
"end_time": end_day.timestamp(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,7 +9,6 @@ from sqlalchemy.sql.lambdas import StatementLambdaElement
|
|||||||
from homeassistant.components.recorder.filters import Filters
|
from homeassistant.components.recorder.filters import Filters
|
||||||
from homeassistant.components.recorder.models import ulid_to_bytes_or_none
|
from homeassistant.components.recorder.models import ulid_to_bytes_or_none
|
||||||
from homeassistant.helpers.json import json_dumps
|
from homeassistant.helpers.json import json_dumps
|
||||||
from homeassistant.util import dt as dt_util
|
|
||||||
|
|
||||||
from .all import all_stmt
|
from .all import all_stmt
|
||||||
from .devices import devices_stmt
|
from .devices import devices_stmt
|
||||||
@ -28,8 +27,8 @@ def statement_for_request(
|
|||||||
context_id: str | None = None,
|
context_id: str | None = None,
|
||||||
) -> StatementLambdaElement:
|
) -> StatementLambdaElement:
|
||||||
"""Generate the logbook statement for a logbook request."""
|
"""Generate the logbook statement for a logbook request."""
|
||||||
start_day = dt_util.utc_to_timestamp(start_day_dt)
|
start_day = start_day_dt.timestamp()
|
||||||
end_day = dt_util.utc_to_timestamp(end_day_dt)
|
end_day = end_day_dt.timestamp()
|
||||||
# No entities: logbook sends everything for the timeframe
|
# No entities: logbook sends everything for the timeframe
|
||||||
# limited by the context_id and the yaml configured filter
|
# limited by the context_id and the yaml configured filter
|
||||||
if not entity_ids and not device_ids:
|
if not entity_ids and not device_ids:
|
||||||
|
@ -184,8 +184,8 @@ def _generate_stream_message(
|
|||||||
"""Generate a logbook stream message response."""
|
"""Generate a logbook stream message response."""
|
||||||
return {
|
return {
|
||||||
"events": events,
|
"events": events,
|
||||||
"start_time": dt_util.utc_to_timestamp(start_day),
|
"start_time": start_day.timestamp(),
|
||||||
"end_time": dt_util.utc_to_timestamp(end_day),
|
"end_time": end_day.timestamp(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,8 +10,6 @@ from typing import TYPE_CHECKING
|
|||||||
|
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
|
|
||||||
from .db_schema import Events, States, StatesMeta
|
from .db_schema import Events, States, StatesMeta
|
||||||
from .models import DatabaseEngine
|
from .models import DatabaseEngine
|
||||||
from .queries import (
|
from .queries import (
|
||||||
@ -251,7 +249,7 @@ def _select_state_attributes_ids_to_purge(
|
|||||||
state_ids = set()
|
state_ids = set()
|
||||||
attributes_ids = set()
|
attributes_ids = set()
|
||||||
for state_id, attributes_id in session.execute(
|
for state_id, attributes_id in session.execute(
|
||||||
find_states_to_purge(dt_util.utc_to_timestamp(purge_before), max_bind_vars)
|
find_states_to_purge(purge_before.timestamp(), max_bind_vars)
|
||||||
).all():
|
).all():
|
||||||
state_ids.add(state_id)
|
state_ids.add(state_id)
|
||||||
if attributes_id:
|
if attributes_id:
|
||||||
@ -271,7 +269,7 @@ def _select_event_data_ids_to_purge(
|
|||||||
event_ids = set()
|
event_ids = set()
|
||||||
data_ids = set()
|
data_ids = set()
|
||||||
for event_id, data_id in session.execute(
|
for event_id, data_id in session.execute(
|
||||||
find_events_to_purge(dt_util.utc_to_timestamp(purge_before), max_bind_vars)
|
find_events_to_purge(purge_before.timestamp(), max_bind_vars)
|
||||||
).all():
|
).all():
|
||||||
event_ids.add(event_id)
|
event_ids.add(event_id)
|
||||||
if data_id:
|
if data_id:
|
||||||
@ -464,7 +462,7 @@ def _select_legacy_detached_state_and_attributes_and_data_ids_to_purge(
|
|||||||
"""
|
"""
|
||||||
states = session.execute(
|
states = session.execute(
|
||||||
find_legacy_detached_states_and_attributes_to_purge(
|
find_legacy_detached_states_and_attributes_to_purge(
|
||||||
dt_util.utc_to_timestamp(purge_before), max_bind_vars
|
purge_before.timestamp(), max_bind_vars
|
||||||
)
|
)
|
||||||
).all()
|
).all()
|
||||||
_LOGGER.debug("Selected %s state ids to remove", len(states))
|
_LOGGER.debug("Selected %s state ids to remove", len(states))
|
||||||
@ -489,7 +487,7 @@ def _select_legacy_event_state_and_attributes_and_data_ids_to_purge(
|
|||||||
"""
|
"""
|
||||||
events = session.execute(
|
events = session.execute(
|
||||||
find_legacy_event_state_and_attributes_and_data_ids_to_purge(
|
find_legacy_event_state_and_attributes_and_data_ids_to_purge(
|
||||||
dt_util.utc_to_timestamp(purge_before), max_bind_vars
|
purge_before.timestamp(), max_bind_vars
|
||||||
)
|
)
|
||||||
).all()
|
).all()
|
||||||
_LOGGER.debug("Selected %s event ids to remove", len(events))
|
_LOGGER.debug("Selected %s event ids to remove", len(events))
|
||||||
|
@ -1077,9 +1077,7 @@ class Event:
|
|||||||
self.origin = origin
|
self.origin = origin
|
||||||
self.time_fired = time_fired or dt_util.utcnow()
|
self.time_fired = time_fired or dt_util.utcnow()
|
||||||
if not context:
|
if not context:
|
||||||
context = Context(
|
context = Context(id=ulid_at_time(self.time_fired.timestamp()))
|
||||||
id=ulid_at_time(dt_util.utc_to_timestamp(self.time_fired))
|
|
||||||
)
|
|
||||||
self.context = context
|
self.context = context
|
||||||
if not context.origin_event:
|
if not context.origin_event:
|
||||||
context.origin_event = self
|
context.origin_event = self
|
||||||
|
@ -1442,7 +1442,7 @@ def async_track_point_in_utc_time(
|
|||||||
"""
|
"""
|
||||||
# Ensure point_in_time is UTC
|
# Ensure point_in_time is UTC
|
||||||
utc_point_in_time = dt_util.as_utc(point_in_time)
|
utc_point_in_time = dt_util.as_utc(point_in_time)
|
||||||
expected_fire_timestamp = dt_util.utc_to_timestamp(utc_point_in_time)
|
expected_fire_timestamp = utc_point_in_time.timestamp()
|
||||||
job = (
|
job = (
|
||||||
action
|
action
|
||||||
if isinstance(action, HassJob)
|
if isinstance(action, HassJob)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user