Skip to content

Commit 0c2cb46

Browse files
authored
Fix flaky laundrify coordinator test (home-assistant#158460)
1 parent 5388740 commit 0c2cb46

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

tests/components/laundrify/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def laundrify_sensor_fixture() -> LaundrifyDevice:
3333

3434
@pytest.fixture(name="laundrify_config_entry")
3535
async def laundrify_setup_config_entry(
36-
hass: HomeAssistant, access_token: str = VALID_ACCESS_TOKEN
36+
hass: HomeAssistant,
37+
laundrify_api_mock,
38+
access_token: str = VALID_ACCESS_TOKEN,
3739
) -> MockConfigEntry:
3840
"""Create laundrify entry in Home Assistant."""
3941
entry = MockConfigEntry(

tests/components/laundrify/test_coordinator.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,44 @@
33
from datetime import timedelta
44

55
from freezegun.api import FrozenDateTimeFactory
6-
from laundrify_aio import LaundrifyDevice, exceptions
6+
from laundrify_aio import exceptions
77

8-
from homeassistant.components.laundrify.const import DEFAULT_POLL_INTERVAL
8+
from homeassistant.components.laundrify.const import DEFAULT_POLL_INTERVAL, DOMAIN
9+
from homeassistant.components.sensor import SensorDeviceClass
910
from homeassistant.const import STATE_UNAVAILABLE
10-
from homeassistant.core import HomeAssistant, State
11-
from homeassistant.util import slugify
11+
from homeassistant.core import HomeAssistant
12+
from homeassistant.helpers import entity_registry as er
1213

1314
from tests.common import async_fire_time_changed
1415

15-
16-
def get_coord_entity(hass: HomeAssistant, mock_device: LaundrifyDevice) -> State:
17-
"""Get the coordinated energy sensor entity."""
18-
device_slug = slugify(mock_device.name, separator="_")
19-
return hass.states.get(f"sensor.{device_slug}_energy")
16+
# Device ID from fixtures/machines.json
17+
DEVICE_ID = "14"
2018

2119

2220
async def test_coordinator_update_success(
2321
hass: HomeAssistant,
22+
entity_registry: er.EntityRegistry,
2423
laundrify_config_entry,
25-
mock_device: LaundrifyDevice,
2624
freezer: FrozenDateTimeFactory,
2725
) -> None:
2826
"""Test the coordinator update is performed successfully."""
2927
freezer.tick(timedelta(seconds=DEFAULT_POLL_INTERVAL))
3028
async_fire_time_changed(hass)
3129
await hass.async_block_till_done()
3230

33-
coord_entity = get_coord_entity(hass, mock_device)
34-
assert coord_entity.state != STATE_UNAVAILABLE
31+
entity_id = entity_registry.async_get_entity_id(
32+
"sensor", DOMAIN, f"{DEVICE_ID}_{SensorDeviceClass.ENERGY}"
33+
)
34+
state = hass.states.get(entity_id)
35+
assert state is not None
36+
assert state.state != STATE_UNAVAILABLE
3537

3638

3739
async def test_coordinator_update_unauthorized(
3840
hass: HomeAssistant,
41+
entity_registry: er.EntityRegistry,
3942
laundrify_config_entry,
4043
laundrify_api_mock,
41-
mock_device: LaundrifyDevice,
4244
freezer: FrozenDateTimeFactory,
4345
) -> None:
4446
"""Test the coordinator update fails if an UnauthorizedException is thrown."""
@@ -48,15 +50,19 @@ async def test_coordinator_update_unauthorized(
4850
async_fire_time_changed(hass)
4951
await hass.async_block_till_done()
5052

51-
coord_entity = get_coord_entity(hass, mock_device)
52-
assert coord_entity.state == STATE_UNAVAILABLE
53+
entity_id = entity_registry.async_get_entity_id(
54+
"sensor", DOMAIN, f"{DEVICE_ID}_{SensorDeviceClass.ENERGY}"
55+
)
56+
state = hass.states.get(entity_id)
57+
assert state is not None
58+
assert state.state == STATE_UNAVAILABLE
5359

5460

5561
async def test_coordinator_update_connection_failed(
5662
hass: HomeAssistant,
63+
entity_registry: er.EntityRegistry,
5764
laundrify_config_entry,
5865
laundrify_api_mock,
59-
mock_device: LaundrifyDevice,
6066
freezer: FrozenDateTimeFactory,
6167
) -> None:
6268
"""Test the coordinator update fails if an ApiConnectionException is thrown."""
@@ -66,5 +72,9 @@ async def test_coordinator_update_connection_failed(
6672
async_fire_time_changed(hass)
6773
await hass.async_block_till_done()
6874

69-
coord_entity = get_coord_entity(hass, mock_device)
70-
assert coord_entity.state == STATE_UNAVAILABLE
75+
entity_id = entity_registry.async_get_entity_id(
76+
"sensor", DOMAIN, f"{DEVICE_ID}_{SensorDeviceClass.ENERGY}"
77+
)
78+
state = hass.states.get(entity_id)
79+
assert state is not None
80+
assert state.state == STATE_UNAVAILABLE

0 commit comments

Comments
 (0)