Avoid total_seconds conversion every state write when context is set (#107617)

This commit is contained in:
J. Nick Koston 2024-01-13 16:04:32 -10:00 committed by GitHub
parent bc2738c3a1
commit 454c62b5b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 7 deletions

View File

@ -6,7 +6,6 @@ import asyncio
from collections import deque
from collections.abc import Callable, Coroutine, Iterable, Mapping, MutableMapping
import dataclasses
from datetime import timedelta
from enum import Enum, IntFlag, auto
import functools as ft
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
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
@ -1164,8 +1163,7 @@ class Entity(
if (
self._context_set is not None
and hass.loop.time() - self._context_set
> CONTEXT_RECENT_TIME.total_seconds()
and hass.loop.time() - self._context_set > CONTEXT_RECENT_TIME_SECONDS
):
self._context = None
self._context_set = None

View File

@ -655,9 +655,7 @@ async def test_set_context_expired(hass: HomeAssistant) -> None:
"""Test setting context."""
context = Context()
with patch(
"homeassistant.helpers.entity.CONTEXT_RECENT_TIME", timedelta(seconds=-5)
):
with patch("homeassistant.helpers.entity.CONTEXT_RECENT_TIME_SECONDS", -5):
ent = entity.Entity()
ent.hass = hass
ent.entity_id = "hello.world"