Restore default timezone after electric_kiwi sensor tests (#116217)

This commit is contained in:
Erik Montnemery 2024-04-26 12:28:40 +02:00 committed by Paulus Schoutsen
parent 8f02ed4bf3
commit 5fb08e8b25
2 changed files with 16 additions and 6 deletions

View File

@ -5,7 +5,6 @@ from __future__ import annotations
from collections.abc import Awaitable, Callable, Generator from collections.abc import Awaitable, Callable, Generator
from time import time from time import time
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
import zoneinfo
from electrickiwi_api.model import AccountBalance, Hop, HopIntervals from electrickiwi_api.model import AccountBalance, Hop, HopIntervals
import pytest import pytest
@ -24,8 +23,6 @@ CLIENT_ID = "1234"
CLIENT_SECRET = "5678" CLIENT_SECRET = "5678"
REDIRECT_URI = "https://example.com/auth/external/callback" REDIRECT_URI = "https://example.com/auth/external/callback"
TZ_NAME = "Pacific/Auckland"
TIMEZONE = zoneinfo.ZoneInfo(TZ_NAME)
YieldFixture = Generator[AsyncMock, None, None] YieldFixture = Generator[AsyncMock, None, None]
ComponentSetup = Callable[[], Awaitable[bool]] ComponentSetup = Callable[[], Awaitable[bool]]

View File

@ -2,6 +2,7 @@
from datetime import UTC, datetime from datetime import UTC, datetime
from unittest.mock import AsyncMock, Mock from unittest.mock import AsyncMock, Mock
import zoneinfo
from freezegun import freeze_time from freezegun import freeze_time
import pytest import pytest
@ -19,10 +20,22 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_registry import EntityRegistry from homeassistant.helpers.entity_registry import EntityRegistry
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .conftest import TIMEZONE, ComponentSetup, YieldFixture from .conftest import ComponentSetup, YieldFixture
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
DEFAULT_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE
TEST_TZ_NAME = "Pacific/Auckland"
TEST_TIMEZONE = zoneinfo.ZoneInfo(TEST_TZ_NAME)
@pytest.fixture(autouse=True)
def restore_timezone():
"""Restore default timezone."""
yield
dt_util.set_default_time_zone(DEFAULT_TIME_ZONE)
@pytest.mark.parametrize( @pytest.mark.parametrize(
("sensor", "sensor_state"), ("sensor", "sensor_state"),
@ -124,8 +137,8 @@ async def test_check_and_move_time(ek_api: AsyncMock) -> None:
"""Test correct time is returned depending on time of day.""" """Test correct time is returned depending on time of day."""
hop = await ek_api(Mock()).get_hop() hop = await ek_api(Mock()).get_hop()
test_time = datetime(2023, 6, 21, 18, 0, 0, tzinfo=TIMEZONE) test_time = datetime(2023, 6, 21, 18, 0, 0, tzinfo=TEST_TIMEZONE)
dt_util.set_default_time_zone(TIMEZONE) dt_util.set_default_time_zone(TEST_TIMEZONE)
with freeze_time(test_time): with freeze_time(test_time):
value = _check_and_move_time(hop, "4:00 PM") value = _check_and_move_time(hop, "4:00 PM")