Improve integration sensor tests (#120326)

This commit is contained in:
Erik Montnemery 2024-06-24 18:33:08 +02:00 committed by GitHub
parent 0247f91855
commit d073fd9b37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -294,7 +294,36 @@ async def test_restore_state_failed(hass: HomeAssistant, extra_attributes) -> No
assert state.state == STATE_UNKNOWN
async def test_trapezoidal(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
("force_update", "sequence"),
[
(
False,
(
(20, 10, 1.67),
(30, 30, 5.0),
(40, 5, 7.92),
(50, 5, 7.92),
(60, 0, 8.75),
),
),
(
True,
(
(20, 10, 1.67),
(30, 30, 5.0),
(40, 5, 7.92),
(50, 5, 8.75),
(60, 0, 9.17),
),
),
],
)
async def test_trapezoidal(
hass: HomeAssistant,
sequence: tuple[tuple[float, float, float, ...]],
force_update: bool,
) -> None:
"""Test integration sensor state."""
config = {
"sensor": {
@ -314,32 +343,51 @@ async def test_trapezoidal(hass: HomeAssistant) -> None:
start_time = dt_util.utcnow()
with freeze_time(start_time) as freezer:
# Testing a power sensor with non-monotonic intervals and values
for time, value, expected in (
(20, 10, 1.67),
(30, 30, 5.0),
(40, 5, 7.92),
(50, 0, 8.33),
):
for time, value, expected in sequence:
freezer.move_to(start_time + timedelta(minutes=time))
hass.states.async_set(
entity_id,
value,
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT},
force_update=True,
force_update=force_update,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
assert round(float(state.state), config["sensor"]["round"]) == expected
state = hass.states.get("sensor.integration")
assert state is not None
assert round(float(state.state), config["sensor"]["round"]) == 8.33
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR
async def test_left(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
("force_update", "sequence"),
[
(
False,
(
(20, 10, 0.0),
(30, 30, 1.67),
(40, 5, 6.67),
(50, 5, 6.67),
(60, 0, 8.33),
),
),
(
True,
(
(20, 10, 0.0),
(30, 30, 1.67),
(40, 5, 6.67),
(50, 5, 7.5),
(60, 0, 8.33),
),
),
],
)
async def test_left(
hass: HomeAssistant,
sequence: tuple[tuple[float, float, float, ...]],
force_update: bool,
) -> None:
"""Test integration sensor state with left reimann method."""
config = {
"sensor": {
@ -362,32 +410,51 @@ async def test_left(hass: HomeAssistant) -> None:
# Testing a power sensor with non-monotonic intervals and values
start_time = dt_util.utcnow()
with freeze_time(start_time) as freezer:
for time, value, expected in (
(20, 10, 0.0),
(30, 30, 1.67),
(40, 5, 6.67),
(50, 0, 7.5),
):
for time, value, expected in sequence:
freezer.move_to(start_time + timedelta(minutes=time))
hass.states.async_set(
entity_id,
value,
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT},
force_update=True,
force_update=force_update,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
assert round(float(state.state), config["sensor"]["round"]) == expected
state = hass.states.get("sensor.integration")
assert state is not None
assert round(float(state.state), config["sensor"]["round"]) == 7.5
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR
async def test_right(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
("force_update", "sequence"),
[
(
False,
(
(20, 10, 3.33),
(30, 30, 8.33),
(40, 5, 9.17),
(50, 5, 9.17),
(60, 0, 9.17),
),
),
(
True,
(
(20, 10, 3.33),
(30, 30, 8.33),
(40, 5, 9.17),
(50, 5, 10.0),
(60, 0, 10.0),
),
),
],
)
async def test_right(
hass: HomeAssistant,
sequence: tuple[tuple[float, float, float, ...]],
force_update: bool,
) -> None:
"""Test integration sensor state with left reimann method."""
config = {
"sensor": {
@ -410,28 +477,18 @@ async def test_right(hass: HomeAssistant) -> None:
# Testing a power sensor with non-monotonic intervals and values
start_time = dt_util.utcnow()
with freeze_time(start_time) as freezer:
for time, value, expected in (
(20, 10, 3.33),
(30, 30, 8.33),
(40, 5, 9.17),
(50, 0, 9.17),
):
for time, value, expected in sequence:
freezer.move_to(start_time + timedelta(minutes=time))
hass.states.async_set(
entity_id,
value,
{ATTR_UNIT_OF_MEASUREMENT: UnitOfPower.KILO_WATT},
force_update=True,
force_update=force_update,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
assert round(float(state.state), config["sensor"]["round"]) == expected
state = hass.states.get("sensor.integration")
assert state is not None
assert round(float(state.state), config["sensor"]["round"]) == 9.17
assert state.attributes.get("unit_of_measurement") == UnitOfEnergy.KILO_WATT_HOUR