Reduce number of time calls needed to write state (#116297)
This commit is contained in:
parent
f295172d07
commit
cbcfd71f3d
2 changed files with 13 additions and 9 deletions
|
@ -2205,6 +2205,7 @@ class StateMachine:
|
||||||
force_update: bool = False,
|
force_update: bool = False,
|
||||||
context: Context | None = None,
|
context: Context | None = None,
|
||||||
state_info: StateInfo | None = None,
|
state_info: StateInfo | None = None,
|
||||||
|
timestamp: float | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set the state of an entity, add entity if it does not exist.
|
"""Set the state of an entity, add entity if it does not exist.
|
||||||
|
|
||||||
|
@ -2244,7 +2245,8 @@ class StateMachine:
|
||||||
# timestamp implementation:
|
# timestamp implementation:
|
||||||
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L6387
|
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L6387
|
||||||
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L6323
|
# https://github.com/python/cpython/blob/c90a862cdcf55dc1753c6466e5fa4a467a13ae24/Modules/_datetimemodule.c#L6323
|
||||||
timestamp = time.time()
|
if timestamp is None:
|
||||||
|
timestamp = time.time()
|
||||||
now = dt_util.utc_from_timestamp(timestamp)
|
now = dt_util.utc_from_timestamp(timestamp)
|
||||||
|
|
||||||
if same_state and same_attr:
|
if same_state and same_attr:
|
||||||
|
|
|
@ -14,7 +14,7 @@ import logging
|
||||||
import math
|
import math
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
import sys
|
import sys
|
||||||
from timeit import default_timer as timer
|
import time
|
||||||
from types import FunctionType
|
from types import FunctionType
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING,
|
TYPE_CHECKING,
|
||||||
|
@ -74,6 +74,8 @@ from .event import (
|
||||||
)
|
)
|
||||||
from .typing import UNDEFINED, StateType, UndefinedType
|
from .typing import UNDEFINED, StateType, UndefinedType
|
||||||
|
|
||||||
|
timer = time.time
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .entity_platform import EntityPlatform
|
from .entity_platform import EntityPlatform
|
||||||
|
|
||||||
|
@ -927,7 +929,7 @@ class Entity(
|
||||||
def async_set_context(self, context: Context) -> None:
|
def async_set_context(self, context: Context) -> None:
|
||||||
"""Set the context the entity currently operates under."""
|
"""Set the context the entity currently operates under."""
|
||||||
self._context = context
|
self._context = context
|
||||||
self._context_set = self.hass.loop.time()
|
self._context_set = time.time()
|
||||||
|
|
||||||
async def async_update_ha_state(self, force_refresh: bool = False) -> None:
|
async def async_update_ha_state(self, force_refresh: bool = False) -> None:
|
||||||
"""Update Home Assistant with current state of entity.
|
"""Update Home Assistant with current state of entity.
|
||||||
|
@ -1131,9 +1133,9 @@ class Entity(
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
start = timer()
|
state_calculate_start = timer()
|
||||||
state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
|
state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
|
||||||
end = timer()
|
time_now = timer()
|
||||||
|
|
||||||
if entry:
|
if entry:
|
||||||
# Make sure capabilities in the entity registry are up to date. Capabilities
|
# Make sure capabilities in the entity registry are up to date. Capabilities
|
||||||
|
@ -1146,7 +1148,6 @@ class Entity(
|
||||||
or supported_features != entry.supported_features
|
or supported_features != entry.supported_features
|
||||||
):
|
):
|
||||||
if not self.__capabilities_updated_at_reported:
|
if not self.__capabilities_updated_at_reported:
|
||||||
time_now = hass.loop.time()
|
|
||||||
# _Entity__capabilities_updated_at is because of name mangling
|
# _Entity__capabilities_updated_at is because of name mangling
|
||||||
if not (
|
if not (
|
||||||
capabilities_updated_at := getattr(
|
capabilities_updated_at := getattr(
|
||||||
|
@ -1180,14 +1181,14 @@ class Entity(
|
||||||
supported_features=supported_features,
|
supported_features=supported_features,
|
||||||
)
|
)
|
||||||
|
|
||||||
if end - start > 0.4 and not self._slow_reported:
|
if time_now - state_calculate_start > 0.4 and not self._slow_reported:
|
||||||
self._slow_reported = True
|
self._slow_reported = True
|
||||||
report_issue = self._suggest_report_issue()
|
report_issue = self._suggest_report_issue()
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Updating state for %s (%s) took %.3f seconds. Please %s",
|
"Updating state for %s (%s) took %.3f seconds. Please %s",
|
||||||
entity_id,
|
entity_id,
|
||||||
type(self),
|
type(self),
|
||||||
end - start,
|
time_now - state_calculate_start,
|
||||||
report_issue,
|
report_issue,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1199,7 +1200,7 @@ class Entity(
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self._context_set is not None
|
self._context_set is not None
|
||||||
and hass.loop.time() - self._context_set > CONTEXT_RECENT_TIME_SECONDS
|
and time_now - self._context_set > CONTEXT_RECENT_TIME_SECONDS
|
||||||
):
|
):
|
||||||
self._context = None
|
self._context = None
|
||||||
self._context_set = None
|
self._context_set = None
|
||||||
|
@ -1212,6 +1213,7 @@ class Entity(
|
||||||
self.force_update,
|
self.force_update,
|
||||||
self._context,
|
self._context,
|
||||||
self._state_info,
|
self._state_info,
|
||||||
|
time_now,
|
||||||
)
|
)
|
||||||
except InvalidStateError:
|
except InvalidStateError:
|
||||||
_LOGGER.exception(
|
_LOGGER.exception(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue