Fix flapping derivative tests where time would move between state changes (#43579)

This commit is contained in:
Joakim Plate 2020-11-25 17:08:00 +01:00 committed by GitHub
parent ea52ffc2d9
commit ac551179ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,11 +23,13 @@ async def test_state(hass):
assert await async_setup_component(hass, "sensor", config) assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"] entity_id = config["sensor"]["source"]
hass.states.async_set(entity_id, 1, {}) base = dt_util.utcnow()
await hass.async_block_till_done() 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) now.return_value += timedelta(seconds=3600)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, 1, {}, force_update=True) hass.states.async_set(entity_id, 1, {}, force_update=True)
await hass.async_block_till_done() 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) config, entity_id = await _setup_sensor(hass, config)
# Testing a energy sensor with non-monotonic intervals and values # Testing a energy sensor with non-monotonic intervals and values
for time, value in zip(times, values): base = dt_util.utcnow()
now = dt_util.utcnow() + timedelta(seconds=time) with patch("homeassistant.util.dt.utcnow") as now:
with patch("homeassistant.util.dt.utcnow", return_value=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) hass.states.async_set(entity_id, value, {}, force_update=True)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -163,8 +166,9 @@ async def test_data_moving_average_for_discrete_sensor(hass):
}, },
) # two minute window ) # two minute window
base = dt_util.utcnow()
for time, value in zip(times, temperature_values): 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): with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True) hass.states.async_set(entity_id, value, {}, force_update=True)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -192,13 +196,15 @@ async def test_prefix(hass):
assert await async_setup_component(hass, "sensor", config) assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"] entity_id = config["sensor"]["source"]
hass.states.async_set( base = dt_util.utcnow()
entity_id, 1000, {"unit_of_measurement": POWER_WATT}, force_update=True with patch("homeassistant.util.dt.utcnow") as now:
) now.return_value = base
await hass.async_block_till_done() 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) now.return_value += timedelta(seconds=3600)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set( hass.states.async_set(
entity_id, 1000, {"unit_of_measurement": POWER_WATT}, force_update=True 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) assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"] entity_id = config["sensor"]["source"]
hass.states.async_set(entity_id, 1000, {}) base = dt_util.utcnow()
await hass.async_block_till_done() 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) now.return_value += timedelta(seconds=10)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, 1000, {}, force_update=True) hass.states.async_set(entity_id, 1000, {}, force_update=True)
await hass.async_block_till_done() await hass.async_block_till_done()