From bc6a41fb9465523998b2e152dd7f3d4275f2cc9d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 17 Jul 2023 21:42:48 -1000 Subject: [PATCH] Remove deprecated state.get_changed_since (#96579) --- homeassistant/helpers/state.py | 50 +-------------------------------- tests/helpers/test_state.py | 51 +--------------------------------- 2 files changed, 2 insertions(+), 99 deletions(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index 21d060f4ba7..dae63b4ead1 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -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, diff --git a/tests/helpers/test_state.py b/tests/helpers/test_state.py index 1919586daa3..255fba0e7e7 100644 --- a/tests/helpers/test_state.py +++ b/tests/helpers/test_state.py @@ -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)