Skip to content

Commit

Permalink
fix test_dataSet6
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Feb 3, 2020
1 parent 3d5eca2 commit 3f9fcac
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions tests/components/derivative/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async def _setup_sensor(hass, config):
"platform": "derivative",
"name": "power",
"source": "sensor.energy",
"unit_time": "s",
"round": 2,
}

Expand Down Expand Up @@ -79,38 +78,40 @@ async def setup_tests(hass, config, times, values, expected_state):
async def test_dataSet1(hass):
"""Test derivative sensor state."""
times, values = zip(*[(20, 10), (30, 30), (40, 5), (50, 0)])
await setup_tests(hass, {}, times, values, expected_state=-0.5)
await setup_tests(hass, {"unit_time": "s"}, times, values, expected_state=-0.5)


async def test_dataSet2(hass):
"""Test derivative sensor state."""
times, values = zip(*[(20, 5), (30, 0)])
await setup_tests(hass, {}, times, values, expected_state=-0.5)
await setup_tests(hass, {"unit_time": "s"}, times, values, expected_state=-0.5)


async def test_dataSet3(hass):
"""Test derivative sensor state."""
times, values = zip(*[(20, 5), (30, 10)])
state = await setup_tests(hass, {}, times, values, expected_state=0.5)
state = await setup_tests(
hass, {"unit_time": "s"}, times, values, expected_state=0.5
)

assert state.attributes.get("unit_of_measurement") == "/s"


async def test_dataSet4(hass):
"""Test derivative sensor state."""
times, values = zip(*[(20, 5), (30, 5)])
await setup_tests(hass, {}, times, values, expected_state=0)
await setup_tests(hass, {"unit_time": "s"}, times, values, expected_state=0)


async def test_dataSet5(hass):
"""Test derivative sensor state."""
times, values = zip(*[(20, 10), (30, -10)])
await setup_tests(hass, {}, times, values, expected_state=-2)
await setup_tests(hass, {"unit_time": "s"}, times, values, expected_state=-2)


async def test_dataSet6(hass):
"""Test derivative sensor state."""
times, values = zip(*[(20, 0), (30, 36000)])
times, values = zip(*[(0, 0), (60, 1 / 60)])
await setup_tests(hass, {}, times, values, expected_state=1)


Expand All @@ -122,9 +123,10 @@ async def test_data_moving_average_for_discrete_sensor(hass):
# the temperature rounded down to an integer value.

temperature_values = []
for minute in range(60):
temperature_values += [minute] * 60
for temperature in range(60):
temperature_values += [temperature] * 60
time_window = 600

times = list(range(len(temperature_values)))
config, entity_id = await _setup_sensor(
hass, {"time_window": {"seconds": time_window}, "unit_time": "min", "round": 1}
Expand All @@ -136,10 +138,11 @@ async def test_data_moving_average_for_discrete_sensor(hass):
hass.states.async_set(entity_id, value, {}, force_update=True)
await hass.async_block_till_done()

if time_window < time < len(times) - time_window:
if time_window < time < times[-1] - time_window:
state = hass.states.get("sensor.power")
derivative = round(float(state.state), config["sensor"]["round"])
# Test that the error is never more than 10%
# Test that the error is never more than
# (time_window_in_minutes / true_derivative * 100) =10%
assert abs(1 - derivative) <= 0.1


Expand Down

0 comments on commit 3f9fcac

Please sign in to comment.