From c7c4ee13c33c4e215233386b6d71ddbea4b39c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 30 May 2023 23:30:31 +0300 Subject: [PATCH] Import `util.dt` as `dt_util` in remaining tests, enforce with ruff (#93765) * Import `util.dt` as `dt_util` in remaining tests, enforce with ruff * Fix `util.dt` import in pvpc_hourly_pricing tests --- pyproject.toml | 1 + tests/common.py | 12 ++++++------ .../pvpc_hourly_pricing/test_config_flow.py | 5 +++-- tests/helpers/test_storage.py | 16 ++++++++-------- tests/helpers/test_storage_remove.py | 4 ++-- tests/test_config_entries.py | 8 ++++---- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index caf904d86d3..9bb55d3d29d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -284,6 +284,7 @@ voluptuous = "vol" "homeassistant.helpers.device_registry" = "dr" "homeassistant.helpers.entity_registry" = "er" "homeassistant.helpers.issue_registry" = "ir" +"homeassistant.util.dt" = "dt_util" [tool.ruff.flake8-pytest-style] fixture-parentheses = false diff --git a/tests/common.py b/tests/common.py index 65e119c8c49..f0d7a8de3c3 100644 --- a/tests/common.py +++ b/tests/common.py @@ -68,7 +68,7 @@ from homeassistant.helpers.json import JSONEncoder from homeassistant.helpers.typing import ConfigType, StateType from homeassistant.setup import setup_component from homeassistant.util.async_ import run_callback_threadsafe -import homeassistant.util.dt as date_util +import homeassistant.util.dt as dt_util from homeassistant.util.json import ( JsonArrayType, JsonObjectType, @@ -357,7 +357,7 @@ def async_fire_time_changed_exact( if datetime_ is None: utc_datetime = datetime.now(timezone.utc) else: - utc_datetime = date_util.as_utc(datetime_) + utc_datetime = dt_util.as_utc(datetime_) _async_fire_time_changed(hass, utc_datetime, fire_all) @@ -379,7 +379,7 @@ def async_fire_time_changed( if datetime_ is None: utc_datetime = datetime.now(timezone.utc) else: - utc_datetime = date_util.as_utc(datetime_) + utc_datetime = dt_util.as_utc(datetime_) if utc_datetime.microsecond < 500000: # Allow up to 500000 microseconds to be added to the time @@ -395,7 +395,7 @@ def async_fire_time_changed( def _async_fire_time_changed( hass: HomeAssistant, utc_datetime: datetime | None, fire_all: bool ) -> None: - timestamp = date_util.utc_to_timestamp(utc_datetime) + timestamp = dt_util.utc_to_timestamp(utc_datetime) for task in list(hass.loop._scheduled): if not isinstance(task, asyncio.TimerHandle): continue @@ -1012,7 +1012,7 @@ def mock_restore_cache(hass: HomeAssistant, states: Sequence[State]) -> None: """Mock the DATA_RESTORE_CACHE.""" key = restore_state.DATA_RESTORE_STATE_TASK data = restore_state.RestoreStateData(hass) - now = date_util.utcnow() + now = dt_util.utcnow() last_states = {} for state in states: @@ -1039,7 +1039,7 @@ def mock_restore_cache_with_extra_data( """Mock the DATA_RESTORE_CACHE.""" key = restore_state.DATA_RESTORE_STATE_TASK data = restore_state.RestoreStateData(hass) - now = date_util.utcnow() + now = dt_util.utcnow() last_states = {} for state, extra_data in states: diff --git a/tests/components/pvpc_hourly_pricing/test_config_flow.py b/tests/components/pvpc_hourly_pricing/test_config_flow.py index 360e7f7f756..8623830f0dd 100644 --- a/tests/components/pvpc_hourly_pricing/test_config_flow.py +++ b/tests/components/pvpc_hourly_pricing/test_config_flow.py @@ -14,13 +14,14 @@ from homeassistant.components.pvpc_hourly_pricing import ( from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er +from homeassistant.util import dt as dt_util from .conftest import check_valid_state -from tests.common import async_fire_time_changed, date_util +from tests.common import async_fire_time_changed from tests.test_util.aiohttp import AiohttpClientMocker -_MOCK_TIME_VALID_RESPONSES = datetime(2023, 1, 6, 12, 0, tzinfo=date_util.UTC) +_MOCK_TIME_VALID_RESPONSES = datetime(2023, 1, 6, 12, 0, tzinfo=dt_util.UTC) async def test_config_flow( diff --git a/tests/helpers/test_storage.py b/tests/helpers/test_storage.py index b60e072d2f1..76dfbdbeb46 100644 --- a/tests/helpers/test_storage.py +++ b/tests/helpers/test_storage.py @@ -14,7 +14,7 @@ from homeassistant.const import ( ) from homeassistant.core import CoreState, HomeAssistant from homeassistant.helpers import storage -from homeassistant.util import dt +from homeassistant.util import dt as dt_util from homeassistant.util.color import RGBColor from tests.common import async_fire_time_changed, async_test_home_assistant @@ -113,7 +113,7 @@ async def test_saving_with_delay( store.async_delay_save(lambda: MOCK_DATA, 1) assert store.key not in hass_storage - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() assert hass_storage[store.key] == { "version": MOCK_VERSION, @@ -135,7 +135,7 @@ async def test_saving_on_final_write( hass.state = CoreState.stopping await hass.async_block_till_done() - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=10)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10)) await hass.async_block_till_done() assert store.key not in hass_storage @@ -159,7 +159,7 @@ async def test_not_delayed_saving_while_stopping( hass.state = CoreState.stopping store.async_delay_save(lambda: MOCK_DATA, 1) - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=2)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2)) await hass.async_block_till_done() assert store.key not in hass_storage @@ -177,7 +177,7 @@ async def test_not_delayed_saving_after_stopping( await hass.async_block_till_done() assert store.key not in hass_storage - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=15)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=15)) await hass.async_block_till_done() assert store.key not in hass_storage @@ -230,7 +230,7 @@ async def test_writing_while_writing_delay( "data": {"delay": "no"}, } - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() assert hass_storage[store.key] == { "version": MOCK_VERSION, @@ -260,7 +260,7 @@ async def test_multiple_delay_save_calls( "data": {"delay": "no"}, } - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() assert hass_storage[store.key] == { "version": MOCK_VERSION, @@ -496,7 +496,7 @@ async def test_changing_delayed_written_data( loaded_data["hello"] = "earth" - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() assert hass_storage[store.key] == { "version": MOCK_VERSION, diff --git a/tests/helpers/test_storage_remove.py b/tests/helpers/test_storage_remove.py index 21eabe80e45..e948f75ea5f 100644 --- a/tests/helpers/test_storage_remove.py +++ b/tests/helpers/test_storage_remove.py @@ -7,7 +7,7 @@ from unittest.mock import patch import py from homeassistant.helpers import storage -from homeassistant.util import dt +from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed, async_test_home_assistant @@ -32,7 +32,7 @@ async def test_removing_while_delay_in_progress(tmpdir: py.path.local) -> None: await real_store.async_remove() assert not await hass.async_add_executor_job(os.path.exists, real_store.path) - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() assert not await hass.async_add_executor_job(os.path.exists, real_store.path) await hass.async_stop() diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 774c85dbc0d..b20f2af7669 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -31,7 +31,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.setup import async_set_domains_to_be_loaded, async_setup_component -from homeassistant.util import dt import homeassistant.util.dt as dt_util from .common import ( @@ -650,7 +649,7 @@ async def test_saving_and_loading(hass: HomeAssistant) -> None: ) # To trigger the call_later - async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) # To execute the save await hass.async_block_till_done() @@ -1611,7 +1610,8 @@ async def test_reload_entry_entity_registry_works( async_fire_time_changed( hass, - dt.utcnow() + timedelta(seconds=config_entries.RELOAD_AFTER_UPDATE_DELAY + 1), + dt_util.utcnow() + + timedelta(seconds=config_entries.RELOAD_AFTER_UPDATE_DELAY + 1), ) await hass.async_block_till_done() @@ -3303,7 +3303,7 @@ async def test_setup_retrying_during_shutdown(hass: HomeAssistant) -> None: assert len(mock_call.return_value.mock_calls) == 0 - async_fire_time_changed(hass, dt.utcnow() + timedelta(hours=4)) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(hours=4)) await hass.async_block_till_done() assert len(mock_call.return_value.mock_calls) == 0