From ac551179ae51ed43ca0963c253a954eee13b2452 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Wed, 25 Nov 2020 17:08:00 +0100 Subject: [PATCH] Fix flapping derivative tests where time would move between state changes (#43579) --- tests/components/derivative/test_sensor.py | 44 +++++++++++++--------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tests/components/derivative/test_sensor.py b/tests/components/derivative/test_sensor.py index 96fee84126a..58090f1587d 100644 --- a/tests/components/derivative/test_sensor.py +++ b/tests/components/derivative/test_sensor.py @@ -23,11 +23,13 @@ async def test_state(hass): assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set(entity_id, 1, {}) - await hass.async_block_till_done() + base = dt_util.utcnow() + with patch("homeassistant.util.dt.utcnow") as now: + now.return_value = base + hass.states.async_set(entity_id, 1, {}) + await hass.async_block_till_done() - now = dt_util.utcnow() + timedelta(seconds=3600) - with patch("homeassistant.util.dt.utcnow", return_value=now): + now.return_value += timedelta(seconds=3600) hass.states.async_set(entity_id, 1, {}, force_update=True) await hass.async_block_till_done() @@ -63,9 +65,10 @@ async def setup_tests(hass, config, times, values, expected_state): config, entity_id = await _setup_sensor(hass, config) # Testing a energy sensor with non-monotonic intervals and values - for time, value in zip(times, values): - now = dt_util.utcnow() + timedelta(seconds=time) - with patch("homeassistant.util.dt.utcnow", return_value=now): + base = dt_util.utcnow() + with patch("homeassistant.util.dt.utcnow") as now: + for time, value in zip(times, values): + now.return_value = base + timedelta(seconds=time) hass.states.async_set(entity_id, value, {}, force_update=True) await hass.async_block_till_done() @@ -163,8 +166,9 @@ async def test_data_moving_average_for_discrete_sensor(hass): }, ) # two minute window + base = dt_util.utcnow() for time, value in zip(times, temperature_values): - now = dt_util.utcnow() + timedelta(seconds=time) + now = base + timedelta(seconds=time) with patch("homeassistant.util.dt.utcnow", return_value=now): hass.states.async_set(entity_id, value, {}, force_update=True) await hass.async_block_till_done() @@ -192,13 +196,15 @@ async def test_prefix(hass): assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set( - entity_id, 1000, {"unit_of_measurement": POWER_WATT}, force_update=True - ) - await hass.async_block_till_done() + base = dt_util.utcnow() + with patch("homeassistant.util.dt.utcnow") as now: + now.return_value = base + hass.states.async_set( + entity_id, 1000, {"unit_of_measurement": POWER_WATT}, force_update=True + ) + await hass.async_block_till_done() - now = dt_util.utcnow() + timedelta(seconds=3600) - with patch("homeassistant.util.dt.utcnow", return_value=now): + now.return_value += timedelta(seconds=3600) hass.states.async_set( entity_id, 1000, {"unit_of_measurement": POWER_WATT}, force_update=True ) @@ -228,11 +234,13 @@ async def test_suffix(hass): assert await async_setup_component(hass, "sensor", config) entity_id = config["sensor"]["source"] - hass.states.async_set(entity_id, 1000, {}) - await hass.async_block_till_done() + base = dt_util.utcnow() + with patch("homeassistant.util.dt.utcnow") as now: + now.return_value = base + hass.states.async_set(entity_id, 1000, {}) + await hass.async_block_till_done() - now = dt_util.utcnow() + timedelta(seconds=10) - with patch("homeassistant.util.dt.utcnow", return_value=now): + now.return_value += timedelta(seconds=10) hass.states.async_set(entity_id, 1000, {}, force_update=True) await hass.async_block_till_done()