Skip to content

Commit 0e6a1e3

Browse files
emontnemeryCopilotgjohansson-ST
authored
Improve integration sensor tests (home-assistant#148938)
Co-authored-by: Copilot <[email protected]> Co-authored-by: G Johansson <[email protected]>
1 parent 79b8e74 commit 0e6a1e3

File tree

1 file changed

+75
-26
lines changed

1 file changed

+75
-26
lines changed

tests/components/integration/test_sensor.py

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,35 @@ async def test_restore_state_failed(hass: HomeAssistant, extra_attributes) -> No
294294
assert state.state == STATE_UNKNOWN
295295

296296

297+
@pytest.mark.parametrize("extra_config", [{}, {"max_sub_interval": {"minutes": 9999}}])
297298
@pytest.mark.parametrize("force_update", [False, True])
298299
@pytest.mark.parametrize(
299300
"sequence",
300301
[
302+
# time, value, attributes, expected
301303
(
302-
(20, 10, 1.67),
303-
(30, 30, 5.0),
304-
(40, 5, 7.92),
305-
(50, 5, 8.75),
306-
(60, 0, 9.17),
304+
(0, 0, {}, 0),
305+
(20, 10, {}, 1.67),
306+
(30, 30, {}, 5.0),
307+
(40, 5, {}, 7.92),
308+
(50, 5, {}, 8.75), # This fires a state report
309+
(60, 0, {}, 9.17),
310+
),
311+
(
312+
(0, 0, {}, 0),
313+
(20, 10, {}, 1.67),
314+
(30, 30, {}, 5.0),
315+
(40, 5, {}, 7.92),
316+
(50, 5, {"foo": "bar"}, 8.75), # This fires a state change
317+
(60, 0, {}, 9.17),
307318
),
308319
],
309320
)
310321
async def test_trapezoidal(
311322
hass: HomeAssistant,
312-
sequence: tuple[tuple[float, float, float], ...],
323+
sequence: tuple[tuple[float, float, dict[str, Any], float], ...],
313324
force_update: bool,
325+
extra_config: dict[str, Any],
314326
) -> None:
315327
"""Test integration sensor state."""
316328
config = {
@@ -320,23 +332,29 @@ async def test_trapezoidal(
320332
"source": "sensor.power",
321333
"round": 2,
322334
}
335+
| extra_config
323336
}
324337

325338
assert await async_setup_component(hass, "sensor", config)
339+
await hass.async_block_till_done()
340+
state = hass.states.get("sensor.integration")
341+
assert state.state == STATE_UNKNOWN
326342

327343
entity_id = config["sensor"]["source"]
328344
hass.states.async_set(entity_id, 0, {})
329345
await hass.async_block_till_done()
346+
state = hass.states.get("sensor.integration")
347+
assert state.state == STATE_UNKNOWN
330348

331349
start_time = dt_util.utcnow()
332350
with freeze_time(start_time) as freezer:
333351
# Testing a power sensor with non-monotonic intervals and values
334-
for time, value, expected in sequence:
352+
for time, value, extra_attributes, expected in sequence:
335353
freezer.move_to(start_time + timedelta(minutes=time))
336354
hass.states.async_set(
337355
entity_id,
338356
value,
339-
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT},
357+
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT} | extra_attributes,
340358
force_update=force_update,
341359
)
342360
await hass.async_block_till_done()
@@ -346,25 +364,35 @@ async def test_trapezoidal(
346364
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR
347365

348366

367+
@pytest.mark.parametrize("extra_config", [{}, {"max_sub_interval": {"minutes": 9999}}])
349368
@pytest.mark.parametrize("force_update", [False, True])
350369
@pytest.mark.parametrize(
351370
"sequence",
352371
[
372+
# time, value, attributes, expected
373+
(
374+
(20, 10, {}, 0.0),
375+
(30, 30, {}, 1.67),
376+
(40, 5, {}, 6.67),
377+
(50, 5, {}, 7.5), # This fires a state report
378+
(60, 0, {}, 8.33),
379+
),
353380
(
354-
(20, 10, 0.0),
355-
(30, 30, 1.67),
356-
(40, 5, 6.67),
357-
(50, 5, 7.5),
358-
(60, 0, 8.33),
381+
(20, 10, {}, 0.0),
382+
(30, 30, {}, 1.67),
383+
(40, 5, {}, 6.67),
384+
(50, 5, {"foo": "bar"}, 7.5), # This fires a state change
385+
(60, 0, {}, 8.33),
359386
),
360387
],
361388
)
362389
async def test_left(
363390
hass: HomeAssistant,
364-
sequence: tuple[tuple[float, float, float], ...],
391+
sequence: tuple[tuple[float, float, dict[str, Any], float], ...],
365392
force_update: bool,
393+
extra_config: dict[str, Any],
366394
) -> None:
367-
"""Test integration sensor state with left reimann method."""
395+
"""Test integration sensor state with left Riemann method."""
368396
config = {
369397
"sensor": {
370398
"platform": "integration",
@@ -373,25 +401,31 @@ async def test_left(
373401
"source": "sensor.power",
374402
"round": 2,
375403
}
404+
| extra_config
376405
}
377406

378407
assert await async_setup_component(hass, "sensor", config)
408+
await hass.async_block_till_done()
409+
state = hass.states.get("sensor.integration")
410+
assert state.state == STATE_UNKNOWN
379411

380412
entity_id = config["sensor"]["source"]
381413
hass.states.async_set(
382414
entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT}
383415
)
384416
await hass.async_block_till_done()
417+
state = hass.states.get("sensor.integration")
418+
assert state.state == STATE_UNKNOWN
385419

386420
# Testing a power sensor with non-monotonic intervals and values
387421
start_time = dt_util.utcnow()
388422
with freeze_time(start_time) as freezer:
389-
for time, value, expected in sequence:
423+
for time, value, extra_attributes, expected in sequence:
390424
freezer.move_to(start_time + timedelta(minutes=time))
391425
hass.states.async_set(
392426
entity_id,
393427
value,
394-
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT},
428+
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT} | extra_attributes,
395429
force_update=force_update,
396430
)
397431
await hass.async_block_till_done()
@@ -401,25 +435,34 @@ async def test_left(
401435
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR
402436

403437

438+
@pytest.mark.parametrize("extra_config", [{}, {"max_sub_interval": {"minutes": 9999}}])
404439
@pytest.mark.parametrize("force_update", [False, True])
405440
@pytest.mark.parametrize(
406441
"sequence",
407442
[
408443
(
409-
(20, 10, 3.33),
410-
(30, 30, 8.33),
411-
(40, 5, 9.17),
412-
(50, 5, 10.0),
413-
(60, 0, 10.0),
444+
(20, 10, {}, 3.33),
445+
(30, 30, {}, 8.33),
446+
(40, 5, {}, 9.17),
447+
(50, 5, {}, 10.0), # This fires a state report
448+
(60, 0, {}, 10.0),
449+
),
450+
(
451+
(20, 10, {}, 3.33),
452+
(30, 30, {}, 8.33),
453+
(40, 5, {}, 9.17),
454+
(50, 5, {"foo": "bar"}, 10.0), # This fires a state change
455+
(60, 0, {}, 10.0),
414456
),
415457
],
416458
)
417459
async def test_right(
418460
hass: HomeAssistant,
419-
sequence: tuple[tuple[float, float, float], ...],
461+
sequence: tuple[tuple[float, float, dict[str, Any], float], ...],
420462
force_update: bool,
463+
extra_config: dict[str, Any],
421464
) -> None:
422-
"""Test integration sensor state with left reimann method."""
465+
"""Test integration sensor state with right Riemann method."""
423466
config = {
424467
"sensor": {
425468
"platform": "integration",
@@ -428,25 +471,31 @@ async def test_right(
428471
"source": "sensor.power",
429472
"round": 2,
430473
}
474+
| extra_config
431475
}
432476

433477
assert await async_setup_component(hass, "sensor", config)
478+
await hass.async_block_till_done()
479+
state = hass.states.get("sensor.integration")
480+
assert state.state == STATE_UNKNOWN
434481

435482
entity_id = config["sensor"]["source"]
436483
hass.states.async_set(
437484
entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT}
438485
)
439486
await hass.async_block_till_done()
487+
state = hass.states.get("sensor.integration")
488+
assert state.state == STATE_UNKNOWN
440489

441490
# Testing a power sensor with non-monotonic intervals and values
442491
start_time = dt_util.utcnow()
443492
with freeze_time(start_time) as freezer:
444-
for time, value, expected in sequence:
493+
for time, value, extra_attributes, expected in sequence:
445494
freezer.move_to(start_time + timedelta(minutes=time))
446495
hass.states.async_set(
447496
entity_id,
448497
value,
449-
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT},
498+
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT} | extra_attributes,
450499
force_update=force_update,
451500
)
452501
await hass.async_block_till_done()

0 commit comments

Comments
 (0)