Fix moving average test for discrete derivative sensor (#31750)

* fix test_data_moving_average_for_discrete_sensor

After https://github.com/home-assistant/home-assistant/pull/31717 the test
didn't actually test anything anymore.

This fixes that.

* make the test faster
This commit is contained in:
Bas Nijholt 2020-02-12 18:53:07 +01:00 committed by GitHub
parent 378c432f6d
commit 8498ca37cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -124,18 +124,18 @@ async def test_dataSet6(hass):
async def test_data_moving_average_for_discrete_sensor(hass):
"""Test derivative sensor state."""
# We simulate the following situation:
# The temperature rises 1 °C per minute for 1 hour long.
# There is a data point every second, however, the sensor returns
# The temperature rises 1 °C per minute for 30 minutes long.
# There is a data point every 30 seconds, however, the sensor returns
# the temperature rounded down to an integer value.
# We use a time window of 10 minutes and therefore we can expect
# (because the true derivative is 1 °C/min) an error of less than 10%.
temperature_values = []
for temperature in range(6):
temperature_values += [temperature] * 6
for temperature in range(30):
temperature_values += [temperature] * 2 # two values per minute
time_window = 600
times = list(range(0, 1800 + 30, 30))
times = list(range(len(temperature_values)))
config, entity_id = await _setup_sensor(
hass, {"time_window": {"seconds": time_window}, "unit_time": "min", "round": 1}
) # two minute window
@ -150,8 +150,8 @@ async def test_data_moving_average_for_discrete_sensor(hass):
state = hass.states.get("sensor.power")
derivative = round(float(state.state), config["sensor"]["round"])
# Test that the error is never more than
# (time_window_in_minutes / true_derivative * 100) = 10%
assert abs(1 - derivative) <= 0.1
# (time_window_in_minutes / true_derivative * 100) = 10% + ε
assert abs(1 - derivative) <= 0.1 + 1e-6
async def test_prefix(hass):