diff --git a/tests/components/tibber/test_services.py b/tests/components/tibber/test_services.py index fe437e421d7..e9bee3ba31f 100644 --- a/tests/components/tibber/test_services.py +++ b/tests/components/tibber/test_services.py @@ -4,6 +4,7 @@ import asyncio import datetime as dt from unittest.mock import MagicMock +from freezegun.api import FrozenDateTimeFactory import pytest from homeassistant.components.tibber.const import DOMAIN @@ -11,11 +12,12 @@ from homeassistant.components.tibber.services import PRICE_SERVICE_NAME, __get_p from homeassistant.core import ServiceCall from homeassistant.exceptions import ServiceValidationError +STARTTIME = dt.datetime.fromtimestamp(1615766400) + def generate_mock_home_data(): """Create mock data from the tibber connection.""" - today = remove_microseconds(dt.datetime.now()) - tomorrow = remove_microseconds(today + dt.timedelta(days=1)) + tomorrow = STARTTIME + dt.timedelta(days=1) mock_homes = [ MagicMock( name="first_home", @@ -26,13 +28,13 @@ def generate_mock_home_data(): "priceInfo": { "today": [ { - "startsAt": today.isoformat(), + "startsAt": STARTTIME.isoformat(), "total": 0.46914, "level": "VERY_EXPENSIVE", }, { "startsAt": ( - today + dt.timedelta(hours=1) + STARTTIME + dt.timedelta(hours=1) ).isoformat(), "total": 0.46914, "level": "VERY_EXPENSIVE", @@ -67,13 +69,13 @@ def generate_mock_home_data(): "priceInfo": { "today": [ { - "startsAt": today.isoformat(), + "startsAt": STARTTIME.isoformat(), "total": 0.46914, "level": "VERY_EXPENSIVE", }, { "startsAt": ( - today + dt.timedelta(hours=1) + STARTTIME + dt.timedelta(hours=1) ).isoformat(), "total": 0.46914, "level": "VERY_EXPENSIVE", @@ -119,19 +121,16 @@ def create_mock_hass(): return mock_hass -def remove_microseconds(dt): - """Remove microseconds from a datetime object.""" - return dt.replace(microsecond=0) - - -async def test_get_prices(): +async def test_get_prices( + freezer: FrozenDateTimeFactory, +) -> None: """Test __get_prices with mock data.""" - today = remove_microseconds(dt.datetime.now()) - tomorrow = remove_microseconds(dt.datetime.now() + dt.timedelta(days=1)) + freezer.move_to(STARTTIME) + tomorrow = STARTTIME + dt.timedelta(days=1) call = ServiceCall( DOMAIN, PRICE_SERVICE_NAME, - {"start": today.date().isoformat(), "end": tomorrow.date().isoformat()}, + {"start": STARTTIME.date().isoformat(), "end": tomorrow.date().isoformat()}, ) result = await __get_prices(call, hass=create_mock_hass()) @@ -140,24 +139,24 @@ async def test_get_prices(): "prices": { "first_home": [ { - "start_time": today, + "start_time": STARTTIME, "price": 0.46914, "level": "VERY_EXPENSIVE", }, { - "start_time": today + dt.timedelta(hours=1), + "start_time": STARTTIME + dt.timedelta(hours=1), "price": 0.46914, "level": "VERY_EXPENSIVE", }, ], "second_home": [ { - "start_time": today, + "start_time": STARTTIME, "price": 0.46914, "level": "VERY_EXPENSIVE", }, { - "start_time": today + dt.timedelta(hours=1), + "start_time": STARTTIME + dt.timedelta(hours=1), "price": 0.46914, "level": "VERY_EXPENSIVE", }, @@ -166,9 +165,11 @@ async def test_get_prices(): } -async def test_get_prices_no_input(): +async def test_get_prices_no_input( + freezer: FrozenDateTimeFactory, +) -> None: """Test __get_prices with no input.""" - today = remove_microseconds(dt.datetime.now()) + freezer.move_to(STARTTIME) call = ServiceCall(DOMAIN, PRICE_SERVICE_NAME, {}) result = await __get_prices(call, hass=create_mock_hass()) @@ -177,24 +178,24 @@ async def test_get_prices_no_input(): "prices": { "first_home": [ { - "start_time": today, + "start_time": STARTTIME, "price": 0.46914, "level": "VERY_EXPENSIVE", }, { - "start_time": today + dt.timedelta(hours=1), + "start_time": STARTTIME + dt.timedelta(hours=1), "price": 0.46914, "level": "VERY_EXPENSIVE", }, ], "second_home": [ { - "start_time": today, + "start_time": STARTTIME, "price": 0.46914, "level": "VERY_EXPENSIVE", }, { - "start_time": today + dt.timedelta(hours=1), + "start_time": STARTTIME + dt.timedelta(hours=1), "price": 0.46914, "level": "VERY_EXPENSIVE", }, @@ -203,9 +204,12 @@ async def test_get_prices_no_input(): } -async def test_get_prices_start_tomorrow(): +async def test_get_prices_start_tomorrow( + freezer: FrozenDateTimeFactory, +) -> None: """Test __get_prices with start date tomorrow.""" - tomorrow = remove_microseconds(dt.datetime.now() + dt.timedelta(days=1)) + freezer.move_to(STARTTIME) + tomorrow = STARTTIME + dt.timedelta(days=1) call = ServiceCall( DOMAIN, PRICE_SERVICE_NAME, {"start": tomorrow.date().isoformat()} ) @@ -242,7 +246,7 @@ async def test_get_prices_start_tomorrow(): } -async def test_get_prices_invalid_input(): +async def test_get_prices_invalid_input() -> None: """Test __get_prices with invalid input.""" call = ServiceCall(DOMAIN, PRICE_SERVICE_NAME, {"start": "test"})