From 233332c3a013d56e1c5108d68e37c0c043e69370 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 15 Jan 2023 22:01:32 -1000 Subject: [PATCH] Fix fetching history include_start_time_state when timezone is not UTC (#85983) --- homeassistant/components/recorder/history.py | 4 ++-- tests/components/recorder/test_history.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/recorder/history.py b/homeassistant/components/recorder/history.py index 819575e455b..1d68becfa88 100644 --- a/homeassistant/components/recorder/history.py +++ b/homeassistant/components/recorder/history.py @@ -586,7 +586,7 @@ def _get_states_for_entites_stmt( # We got an include-list of entities, accelerate the query by filtering already # in the inner query. if schema_version >= 31: - run_start_ts = run_start.timestamp() + run_start_ts = process_timestamp(run_start).timestamp() utc_point_in_time_ts = dt_util.utc_to_timestamp(utc_point_in_time) stmt += lambda q: q.where( States.state_id @@ -629,7 +629,7 @@ def _generate_most_recent_states_by_date( ) -> Subquery: """Generate the sub query for the most recent states by data.""" if schema_version >= 31: - run_start_ts = run_start.timestamp() + run_start_ts = process_timestamp(run_start).timestamp() utc_point_in_time_ts = dt_util.utc_to_timestamp(utc_point_in_time) return ( select( diff --git a/tests/components/recorder/test_history.py b/tests/components/recorder/test_history.py index 0465c10a8d2..b7c0ddc12e0 100644 --- a/tests/components/recorder/test_history.py +++ b/tests/components/recorder/test_history.py @@ -434,7 +434,8 @@ def test_get_significant_states_minimal_response(hass_recorder): assert states == hist -def test_get_significant_states_with_initial(hass_recorder): +@pytest.mark.parametrize("time_zone", ["Europe/Berlin", "US/Hawaii", "UTC"]) +def test_get_significant_states_with_initial(time_zone, hass_recorder): """Test that only significant states are returned. We should get back every thermostat change that @@ -442,6 +443,7 @@ def test_get_significant_states_with_initial(hass_recorder): media player (attribute changes are not significant and not returned). """ hass = hass_recorder() + hass.config.set_time_zone(time_zone) zero, four, states = record_states(hass) one = zero + timedelta(seconds=1) one_and_half = zero + timedelta(seconds=1.5)