mirror of
https://github.com/home-assistant/core.git
synced 2026-12-04 12:50:45 -04:00
refactor(plant): avoid naive datetime.now calls (#192653)
Co-authored-by: Gemini <200291788+gemini-code-assist@users.noreply.github.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user