mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Remove deprecated state.get_changed_since (#96579)
This commit is contained in:
parent
4dd7611c83
commit
bc6a41fb94
@ -4,9 +4,8 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
from collections import defaultdict
|
||||
from collections.abc import Iterable
|
||||
import datetime as dt
|
||||
import logging
|
||||
from types import ModuleType, TracebackType
|
||||
from types import ModuleType
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sun import STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON
|
||||
@ -23,57 +22,10 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import Context, HomeAssistant, State
|
||||
from homeassistant.loader import IntegrationNotFound, async_get_integration, bind_hass
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from .frame import report
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AsyncTrackStates:
|
||||
"""Record the time when the with-block is entered.
|
||||
|
||||
Add all states that have changed since the start time to the return list
|
||||
when with-block is exited.
|
||||
|
||||
Must be run within the event loop.
|
||||
|
||||
Deprecated. Remove after June 2021.
|
||||
Warning added via `get_changed_since`.
|
||||
"""
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
"""Initialize a TrackStates block."""
|
||||
self.hass = hass
|
||||
self.states: list[State] = []
|
||||
|
||||
# pylint: disable=attribute-defined-outside-init
|
||||
def __enter__(self) -> list[State]:
|
||||
"""Record time from which to track changes."""
|
||||
self.now = dt_util.utcnow()
|
||||
return self.states
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc_value: BaseException | None,
|
||||
traceback: TracebackType | None,
|
||||
) -> None:
|
||||
"""Add changes states to changes list."""
|
||||
self.states.extend(get_changed_since(self.hass.states.async_all(), self.now))
|
||||
|
||||
|
||||
def get_changed_since(
|
||||
states: Iterable[State], utc_point_in_time: dt.datetime
|
||||
) -> list[State]:
|
||||
"""Return list of states that have been changed since utc_point_in_time.
|
||||
|
||||
Deprecated. Remove after June 2021.
|
||||
"""
|
||||
report("uses deprecated `get_changed_since`")
|
||||
return [state for state in states if state.last_updated >= utc_point_in_time]
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_reproduce_state(
|
||||
hass: HomeAssistant,
|
||||
|
@ -1,9 +1,7 @@
|
||||
"""Test state helpers."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
from unittest.mock import Mock, patch
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun import freeze_time
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.sun import STATE_ABOVE_HORIZON, STATE_BELOW_HORIZON
|
||||
@ -21,34 +19,10 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.helpers import state
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
async def test_async_track_states(
|
||||
hass: HomeAssistant, mock_integration_frame: Mock
|
||||
) -> None:
|
||||
"""Test AsyncTrackStates context manager."""
|
||||
point1 = dt_util.utcnow()
|
||||
point2 = point1 + timedelta(seconds=5)
|
||||
point3 = point2 + timedelta(seconds=5)
|
||||
|
||||
with freeze_time(point2) as freezer, state.AsyncTrackStates(hass) as states:
|
||||
freezer.move_to(point1)
|
||||
hass.states.async_set("light.test", "on")
|
||||
|
||||
freezer.move_to(point2)
|
||||
hass.states.async_set("light.test2", "on")
|
||||
state2 = hass.states.get("light.test2")
|
||||
|
||||
freezer.move_to(point3)
|
||||
hass.states.async_set("light.test3", "on")
|
||||
state3 = hass.states.get("light.test3")
|
||||
|
||||
assert [state2, state3] == sorted(states, key=lambda state: state.entity_id)
|
||||
|
||||
|
||||
async def test_call_to_component(hass: HomeAssistant) -> None:
|
||||
"""Test calls to components state reproduction functions."""
|
||||
with patch(
|
||||
@ -82,29 +56,6 @@ async def test_call_to_component(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
|
||||
async def test_get_changed_since(
|
||||
hass: HomeAssistant, mock_integration_frame: Mock
|
||||
) -> None:
|
||||
"""Test get_changed_since."""
|
||||
point1 = dt_util.utcnow()
|
||||
point2 = point1 + timedelta(seconds=5)
|
||||
point3 = point2 + timedelta(seconds=5)
|
||||
|
||||
with freeze_time(point1) as freezer:
|
||||
hass.states.async_set("light.test", "on")
|
||||
state1 = hass.states.get("light.test")
|
||||
|
||||
freezer.move_to(point2)
|
||||
hass.states.async_set("light.test2", "on")
|
||||
state2 = hass.states.get("light.test2")
|
||||
|
||||
freezer.move_to(point3)
|
||||
hass.states.async_set("light.test3", "on")
|
||||
state3 = hass.states.get("light.test3")
|
||||
|
||||
assert [state2, state3] == state.get_changed_since([state1, state2, state3], point2)
|
||||
|
||||
|
||||
async def test_reproduce_with_no_entity(hass: HomeAssistant) -> None:
|
||||
"""Test reproduce_state with no entity."""
|
||||
calls = async_mock_service(hass, "light", SERVICE_TURN_ON)
|
||||
|
Loading…
x
Reference in New Issue
Block a user