diff --git a/tests/components/laundrify/conftest.py b/tests/components/laundrify/conftest.py index c9ad1e528a5..75df96d30b0 100644 --- a/tests/components/laundrify/conftest.py +++ b/tests/components/laundrify/conftest.py @@ -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( diff --git a/tests/components/laundrify/test_coordinator.py b/tests/components/laundrify/test_coordinator.py index 64b486d1285..a8e460da506 100644 --- a/tests/components/laundrify/test_coordinator.py +++ b/tests/components/laundrify/test_coordinator.py @@ -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