Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve integration sensor tests #120316

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions tests/components/integration/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ 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 in ((20, 10), (30, 30), (40, 5), (50, 0)):
for time, value, expected in (
(20, 10, 1.67),
(30, 30, 5.0),
(40, 5, 7.92),
(50, 0, 8.33),
):
freezer.move_to(start_time + timedelta(minutes=time))
hass.states.async_set(
entity_id,
Expand All @@ -323,6 +328,8 @@ async def test_trapezoidal(hass: HomeAssistant) -> None:
force_update=True,
)
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
Expand Down Expand Up @@ -353,16 +360,24 @@ async def test_left(hass: HomeAssistant) -> None:
await hass.async_block_till_done()

# Testing a power sensor with non-monotonic intervals and values
for time, value in ((20, 10), (30, 30), (40, 5), (50, 0)):
now = dt_util.utcnow() + timedelta(minutes=time)
with freeze_time(now):
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),
):
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,
)
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
Expand Down Expand Up @@ -393,16 +408,24 @@ async def test_right(hass: HomeAssistant) -> None:
await hass.async_block_till_done()

# Testing a power sensor with non-monotonic intervals and values
for time, value in ((20, 10), (30, 30), (40, 5), (50, 0)):
now = dt_util.utcnow() + timedelta(minutes=time)
with freeze_time(now):
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),
):
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,
)
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
Expand Down