1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Fix flaky laundrify coordinator test (#158460)

This commit is contained in:
Paul Tarjan
2025-12-09 13:12:55 -07:00
committed by GitHub
parent 5388740c83
commit 0c2cb460cb
2 changed files with 31 additions and 19 deletions

View File

@@ -33,7 +33,9 @@ def laundrify_sensor_fixture() -> LaundrifyDevice:
@pytest.fixture(name="laundrify_config_entry")
async def laundrify_setup_config_entry(
hass: HomeAssistant, access_token: str = VALID_ACCESS_TOKEN
hass: HomeAssistant,
laundrify_api_mock,
access_token: str = VALID_ACCESS_TOKEN,
) -> MockConfigEntry:
"""Create laundrify entry in Home Assistant."""
entry = MockConfigEntry(

View File

@@ -3,26 +3,24 @@
from datetime import timedelta
from freezegun.api import FrozenDateTimeFactory
from laundrify_aio import LaundrifyDevice, exceptions
from laundrify_aio import exceptions
from homeassistant.components.laundrify.const import DEFAULT_POLL_INTERVAL
from homeassistant.components.laundrify.const import DEFAULT_POLL_INTERVAL, DOMAIN
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant, State
from homeassistant.util import slugify
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import async_fire_time_changed
def get_coord_entity(hass: HomeAssistant, mock_device: LaundrifyDevice) -> State:
"""Get the coordinated energy sensor entity."""
device_slug = slugify(mock_device.name, separator="_")
return hass.states.get(f"sensor.{device_slug}_energy")
# Device ID from fixtures/machines.json
DEVICE_ID = "14"
async def test_coordinator_update_success(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
laundrify_config_entry,
mock_device: LaundrifyDevice,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the coordinator update is performed successfully."""
@@ -30,15 +28,19 @@ async def test_coordinator_update_success(
async_fire_time_changed(hass)
await hass.async_block_till_done()
coord_entity = get_coord_entity(hass, mock_device)
assert coord_entity.state != STATE_UNAVAILABLE
entity_id = entity_registry.async_get_entity_id(
"sensor", DOMAIN, f"{DEVICE_ID}_{SensorDeviceClass.ENERGY}"
)
state = hass.states.get(entity_id)
assert state is not None
assert state.state != STATE_UNAVAILABLE
async def test_coordinator_update_unauthorized(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
laundrify_config_entry,
laundrify_api_mock,
mock_device: LaundrifyDevice,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the coordinator update fails if an UnauthorizedException is thrown."""
@@ -48,15 +50,19 @@ async def test_coordinator_update_unauthorized(
async_fire_time_changed(hass)
await hass.async_block_till_done()
coord_entity = get_coord_entity(hass, mock_device)
assert coord_entity.state == STATE_UNAVAILABLE
entity_id = entity_registry.async_get_entity_id(
"sensor", DOMAIN, f"{DEVICE_ID}_{SensorDeviceClass.ENERGY}"
)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNAVAILABLE
async def test_coordinator_update_connection_failed(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
laundrify_config_entry,
laundrify_api_mock,
mock_device: LaundrifyDevice,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the coordinator update fails if an ApiConnectionException is thrown."""
@@ -66,5 +72,9 @@ async def test_coordinator_update_connection_failed(
async_fire_time_changed(hass)
await hass.async_block_till_done()
coord_entity = get_coord_entity(hass, mock_device)
assert coord_entity.state == STATE_UNAVAILABLE
entity_id = entity_registry.async_get_entity_id(
"sensor", DOMAIN, f"{DEVICE_ID}_{SensorDeviceClass.ENERGY}"
)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNAVAILABLE