From cb8842b1fb56872ad6a62ee75e181de4755e7fea Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 25 Aug 2023 16:02:47 +0200 Subject: [PATCH] Use freezegun in landisgyr_heat_meter tests (#99037) --- tests/components/landisgyr_heat_meter/test_sensor.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/components/landisgyr_heat_meter/test_sensor.py b/tests/components/landisgyr_heat_meter/test_sensor.py index e28ebe695b3..5ed2a397ccd 100644 --- a/tests/components/landisgyr_heat_meter/test_sensor.py +++ b/tests/components/landisgyr_heat_meter/test_sensor.py @@ -2,6 +2,7 @@ import datetime from unittest.mock import patch +from freezegun.api import FrozenDateTimeFactory import pytest import serial from syrupy import SnapshotAssertion @@ -122,7 +123,9 @@ async def test_create_sensors( @patch(API_HEAT_METER_SERVICE) -async def test_exception_on_polling(mock_heat_meter, hass: HomeAssistant) -> None: +async def test_exception_on_polling( + mock_heat_meter, hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test sensor.""" entry_data = { "device": "/dev/USB0", @@ -148,7 +151,8 @@ async def test_exception_on_polling(mock_heat_meter, hass: HomeAssistant) -> Non # Now 'disable' the connection and wait for polling and see if it fails mock_heat_meter().read.side_effect = serial.serialutil.SerialException - async_fire_time_changed(hass, dt_util.utcnow() + POLLING_INTERVAL) + freezer.tick(POLLING_INTERVAL) + async_fire_time_changed(hass) await hass.async_block_till_done() state = hass.states.get("sensor.heat_meter_heat_usage_gj") assert state.state == STATE_UNAVAILABLE @@ -159,7 +163,8 @@ async def test_exception_on_polling(mock_heat_meter, hass: HomeAssistant) -> Non mock_heat_meter().read.return_value = mock_heat_meter_response mock_heat_meter().read.side_effect = None - async_fire_time_changed(hass, dt_util.utcnow() + POLLING_INTERVAL) + freezer.tick(POLLING_INTERVAL) + async_fire_time_changed(hass) await hass.async_block_till_done() state = hass.states.get("sensor.heat_meter_heat_usage_gj") assert state