mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use loop time to set context (#99701)
* Use loop time to set context loop time is faster than utcnow, and since its only used internally it can be switched without a breaking change * fix mocking
This commit is contained in:
parent
71afa0ff43
commit
034fabe188
@ -5,7 +5,7 @@ from abc import ABC
|
|||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Coroutine, Iterable, Mapping, MutableMapping
|
from collections.abc import Coroutine, Iterable, Mapping, MutableMapping
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, timedelta
|
from datetime import timedelta
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
import functools as ft
|
import functools as ft
|
||||||
import logging
|
import logging
|
||||||
@ -41,7 +41,7 @@ from homeassistant.exceptions import (
|
|||||||
NoEntitySpecifiedError,
|
NoEntitySpecifiedError,
|
||||||
)
|
)
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
from homeassistant.util import dt as dt_util, ensure_unique_string, slugify
|
from homeassistant.util import ensure_unique_string, slugify
|
||||||
|
|
||||||
from . import device_registry as dr, entity_registry as er
|
from . import device_registry as dr, entity_registry as er
|
||||||
from .device_registry import DeviceInfo, EventDeviceRegistryUpdatedData
|
from .device_registry import DeviceInfo, EventDeviceRegistryUpdatedData
|
||||||
@ -272,7 +272,7 @@ class Entity(ABC):
|
|||||||
|
|
||||||
# Context
|
# Context
|
||||||
_context: Context | None = None
|
_context: Context | None = None
|
||||||
_context_set: datetime | None = None
|
_context_set: float | None = None
|
||||||
|
|
||||||
# If entity is added to an entity platform
|
# If entity is added to an entity platform
|
||||||
_platform_state = EntityPlatformState.NOT_ADDED
|
_platform_state = EntityPlatformState.NOT_ADDED
|
||||||
@ -660,7 +660,7 @@ class Entity(ABC):
|
|||||||
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 = dt_util.utcnow()
|
self._context_set = self.hass.loop.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.
|
||||||
@ -847,7 +847,8 @@ class Entity(ABC):
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
self._context_set is not None
|
self._context_set is not None
|
||||||
and dt_util.utcnow() - self._context_set > self.context_recent_time
|
and hass.loop.time() - self._context_set
|
||||||
|
> self.context_recent_time.total_seconds()
|
||||||
):
|
):
|
||||||
self._context = None
|
self._context = None
|
||||||
self._context_set = None
|
self._context_set = None
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
"""Test service helpers."""
|
"""Test service helpers."""
|
||||||
from collections import OrderedDict
|
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -54,7 +53,7 @@ def mock_handle_entity_call():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_entities(hass):
|
def mock_entities(hass: HomeAssistant) -> dict[str, MockEntity]:
|
||||||
"""Return mock entities in an ordered dict."""
|
"""Return mock entities in an ordered dict."""
|
||||||
kitchen = MockEntity(
|
kitchen = MockEntity(
|
||||||
entity_id="light.kitchen",
|
entity_id="light.kitchen",
|
||||||
@ -80,11 +79,13 @@ def mock_entities(hass):
|
|||||||
should_poll=False,
|
should_poll=False,
|
||||||
supported_features=(SUPPORT_B | SUPPORT_C),
|
supported_features=(SUPPORT_B | SUPPORT_C),
|
||||||
)
|
)
|
||||||
entities = OrderedDict()
|
entities = {}
|
||||||
entities[kitchen.entity_id] = kitchen
|
entities[kitchen.entity_id] = kitchen
|
||||||
entities[living_room.entity_id] = living_room
|
entities[living_room.entity_id] = living_room
|
||||||
entities[bedroom.entity_id] = bedroom
|
entities[bedroom.entity_id] = bedroom
|
||||||
entities[bathroom.entity_id] = bathroom
|
entities[bathroom.entity_id] = bathroom
|
||||||
|
for entity in entities.values():
|
||||||
|
entity.hass = hass
|
||||||
return entities
|
return entities
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user