Skip to content

Commit

Permalink
Import util.dt as dt_util in remaining tests, enforce with ruff (#…
Browse files Browse the repository at this point in the history
…93765)

* Import `util.dt` as `dt_util` in remaining tests, enforce with ruff

* Fix `util.dt` import in pvpc_hourly_pricing tests
  • Loading branch information
scop committed May 30, 2023
1 parent 1dcabae commit c7c4ee1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
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,
Expand Down Expand Up @@ -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)

Expand 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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions tests/components/pvpc_hourly_pricing/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
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(
Expand Down
16 changes: 8 additions & 8 deletions tests/helpers/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
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
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/test_storage_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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

Expand All @@ -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()
8 changes: 4 additions & 4 deletions tests/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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 (
Expand Down Expand Up @@ -650,7 +649,7 @@ async def async_step_user(self, user_input=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()

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c7c4ee1

Please sign in to comment.