mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 22:37:11 +00:00
Avoid total_seconds conversion every state write when context is set (#107617)
This commit is contained in:
parent
bc2738c3a1
commit
454c62b5b4
@ -6,7 +6,6 @@ import asyncio
|
|||||||
from collections import deque
|
from collections import deque
|
||||||
from collections.abc import Callable, Coroutine, Iterable, Mapping, MutableMapping
|
from collections.abc import Callable, Coroutine, Iterable, Mapping, MutableMapping
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from datetime import timedelta
|
|
||||||
from enum import Enum, IntFlag, auto
|
from enum import Enum, IntFlag, auto
|
||||||
import functools as ft
|
import functools as ft
|
||||||
import logging
|
import logging
|
||||||
@ -88,7 +87,7 @@ FLOAT_PRECISION = abs(int(math.floor(math.log10(abs(sys.float_info.epsilon)))))
|
|||||||
# How many times per hour we allow capabilities to be updated before logging a warning
|
# How many times per hour we allow capabilities to be updated before logging a warning
|
||||||
CAPABILITIES_UPDATE_LIMIT = 100
|
CAPABILITIES_UPDATE_LIMIT = 100
|
||||||
|
|
||||||
CONTEXT_RECENT_TIME = timedelta(seconds=5) # Time that a context is considered recent
|
CONTEXT_RECENT_TIME_SECONDS = 5 # Time that a context is considered recent
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -1164,8 +1163,7 @@ class Entity(
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
self._context_set is not None
|
self._context_set is not None
|
||||||
and hass.loop.time() - self._context_set
|
and hass.loop.time() - self._context_set > CONTEXT_RECENT_TIME_SECONDS
|
||||||
> CONTEXT_RECENT_TIME.total_seconds()
|
|
||||||
):
|
):
|
||||||
self._context = None
|
self._context = None
|
||||||
self._context_set = None
|
self._context_set = None
|
||||||
|
@ -655,9 +655,7 @@ async def test_set_context_expired(hass: HomeAssistant) -> None:
|
|||||||
"""Test setting context."""
|
"""Test setting context."""
|
||||||
context = Context()
|
context = Context()
|
||||||
|
|
||||||
with patch(
|
with patch("homeassistant.helpers.entity.CONTEXT_RECENT_TIME_SECONDS", -5):
|
||||||
"homeassistant.helpers.entity.CONTEXT_RECENT_TIME", timedelta(seconds=-5)
|
|
||||||
):
|
|
||||||
ent = entity.Entity()
|
ent = entity.Entity()
|
||||||
ent.hass = hass
|
ent.hass = hass
|
||||||
ent.entity_id = "hello.world"
|
ent.entity_id = "hello.world"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user