mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Speed up setting state (#93472)
This commit is contained in:
parent
3cca338c6e
commit
e09e4f14d6
@ -25,6 +25,7 @@ import os
|
|||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
from time import monotonic
|
from time import monotonic
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
@ -1620,10 +1621,24 @@ class StateMachine:
|
|||||||
if same_state and same_attr:
|
if same_state and same_attr:
|
||||||
return
|
return
|
||||||
|
|
||||||
now = dt_util.utcnow()
|
|
||||||
|
|
||||||
if context is None:
|
if context is None:
|
||||||
context = Context(id=ulid_util.ulid_at_time(dt_util.utc_to_timestamp(now)))
|
# It is much faster to convert a timestamp to a utc datetime object
|
||||||
|
# than converting a utc datetime object to a timestamp since cpython
|
||||||
|
# does not have a fast path for handling the UTC timezone and has to do
|
||||||
|
# multiple local timezone conversions.
|
||||||
|
#
|
||||||
|
# from_timestamp implementation:
|
||||||
|
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L2936
|
||||||
|
#
|
||||||
|
# timestamp implementation:
|
||||||
|
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L6387
|
||||||
|
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L6323
|
||||||
|
timestamp = time.time()
|
||||||
|
now = dt_util.utc_from_timestamp(timestamp)
|
||||||
|
context = Context(id=ulid_util.ulid_at_time(timestamp))
|
||||||
|
else:
|
||||||
|
now = dt_util.utcnow()
|
||||||
|
|
||||||
state = State(
|
state = State(
|
||||||
entity_id,
|
entity_id,
|
||||||
new_state,
|
new_state,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user