From 38c7ce07f4281065fa5ca026f0ecaad01f3bf170 Mon Sep 17 00:00:00 2001 From: robotsnh <88983536+robotsnh@users.noreply.github.com> Date: Tue, 24 Nov 2026 09:37:57 +0000 Subject: [PATCH] refactor(`plant`): avoid naive `datetime.now` calls (#192653) Co-authored-by: Gemini <200291788+gemini-code-assist@users.noreply.github.com> --- homeassistant/components/plant/entity.py | 4 ++-- tests/components/plant/test_init.py | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/plant/entity.py b/homeassistant/components/plant/entity.py index 25aab98a251b..4523f12b4d71 100644 --- a/homeassistant/components/plant/entity.py +++ b/homeassistant/components/plant/entity.py @@ -2,7 +2,7 @@ from collections import deque from contextlib import suppress -from datetime import datetime, timedelta +from datetime import timedelta import logging from typing import Any, override @@ -304,7 +304,7 @@ class DailyHistory: def add_measurement(self, value, timestamp=None): """Add a new measurement for a certain day.""" - day = (timestamp or datetime.now()).date() # pylint: disable=home-assistant-enforce-naive-now + day = dt_util.as_local(timestamp or dt_util.now()).date() if not isinstance(value, (int, float)): return if self._days is None: diff --git a/tests/components/plant/test_init.py b/tests/components/plant/test_init.py index 2202b5c375a1..b6e047764fa8 100644 --- a/tests/components/plant/test_init.py +++ b/tests/components/plant/test_init.py @@ -1,6 +1,6 @@ """Unit tests for platform/plant.py.""" -from datetime import datetime, timedelta +from datetime import UTC, datetime, timedelta from pytest_homeassistant.components.recorder.common import async_wait_recording_done @@ -19,6 +19,7 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant, State from homeassistant.helpers.entity_component import DATA_INSTANCES from homeassistant.setup import async_setup_component +from homeassistant.util import dt as dt_util GOOD_DATA = { "moisture": 50, @@ -254,7 +255,7 @@ def test_daily_history_one_day(hass: HomeAssistant) -> None: def test_daily_history_multiple_days(hass: HomeAssistant) -> None: """Test storing data for different days.""" dh = DailyHistory(3) - today = datetime.now() # pylint: disable=home-assistant-enforce-naive-now + today = dt_util.now() today_minus_1 = today - timedelta(days=1) today_minus_2 = today_minus_1 - timedelta(days=1) today_minus_3 = today_minus_2 - timedelta(days=1) @@ -265,3 +266,22 @@ def test_daily_history_multiple_days(hass: HomeAssistant) -> None: for i, value in enumerate(days): dh.add_measurement(values[i], value) assert max_values[i] == dh.max + + +async def test_daily_history_timezone_date_boundaries(hass: HomeAssistant) -> None: + """Test UTC timestamps are grouped by local date.""" + await hass.config.async_set_time_zone("America/New_York") + dh = DailyHistory(2) + + dh.add_measurement(10, datetime(2026, 1, 1, 23, 0, tzinfo=UTC)) + dh.add_measurement(25, datetime(2026, 1, 2, 3, 0, tzinfo=UTC)) + assert len(dh._days) == 1 + assert dh.max == 25 + + dh.add_measurement(15, datetime(2026, 1, 2, 6, 0, tzinfo=UTC)) + assert len(dh._days) == 2 + assert dh.max == 25 + + dh.add_measurement(12, datetime(2026, 1, 3, 6, 0, tzinfo=UTC)) + assert len(dh._days) == 2 + assert dh.max == 15