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

Fix flaky tests in advantage_air #129758

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
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
52 changes: 17 additions & 35 deletions tests/components/advantage_air/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""Test the Advantage Air Binary Sensor Platform."""

from datetime import timedelta
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, patch

from homeassistant.components.advantage_air import ADVANTAGE_AIR_SYNC_INTERVAL
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
Expand Down Expand Up @@ -70,22 +68,14 @@ async def test_binary_sensor_async_setup_entry(
assert not hass.states.get(entity_id)

mock_get.reset_mock()
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()

async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=ADVANTAGE_AIR_SYNC_INTERVAL + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 1

async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 3

with patch("homeassistant.config_entries.RELOAD_AFTER_UPDATE_DELAY", 1):
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()

async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 1

state = hass.states.get(entity_id)
assert state
Expand All @@ -101,22 +91,14 @@ async def test_binary_sensor_async_setup_entry(
assert not hass.states.get(entity_id)

mock_get.reset_mock()
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()

async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=ADVANTAGE_AIR_SYNC_INTERVAL + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 1

async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 3

with patch("homeassistant.config_entries.RELOAD_AFTER_UPDATE_DELAY", 1):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patching RELOAD_AFTER_UPDATE_DELAY isn't great, but its an existing issue

entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()

async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 1

state = hass.states.get(entity_id)
assert state
Expand Down
24 changes: 7 additions & 17 deletions tests/components/advantage_air/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""Test the Advantage Air Sensor Platform."""

from datetime import timedelta
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, patch

from homeassistant.components.advantage_air import ADVANTAGE_AIR_SYNC_INTERVAL
from homeassistant.components.advantage_air.const import DOMAIN as ADVANTAGE_AIR_DOMAIN
from homeassistant.components.advantage_air.sensor import (
ADVANTAGE_AIR_SERVICE_SET_TIME_TO,
ADVANTAGE_AIR_SET_COUNTDOWN_VALUE,
)
from homeassistant.config_entries import RELOAD_AFTER_UPDATE_DELAY
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
Expand Down Expand Up @@ -124,23 +122,15 @@ async def test_sensor_platform_disabled_entity(

assert not hass.states.get(entity_id)

entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done(wait_background_tasks=True)
mock_get.reset_mock()

async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=ADVANTAGE_AIR_SYNC_INTERVAL + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 1
with patch("homeassistant.config_entries.RELOAD_AFTER_UPDATE_DELAY", 1):
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done(wait_background_tasks=True)

async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=RELOAD_AFTER_UPDATE_DELAY + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 3
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
await hass.async_block_till_done(wait_background_tasks=True)
assert len(mock_get.mock_calls) == 1

state = hass.states.get(entity_id)
assert state
Expand Down