diff --git a/tests/components/smartthings/__init__.py b/tests/components/smartthings/__init__.py index 18c7f6c86cf4..8a6aff05d378 100644 --- a/tests/components/smartthings/__init__.py +++ b/tests/components/smartthings/__init__.py @@ -1,5 +1,6 @@ """Tests for the SmartThings integration.""" +from functools import cache from typing import Any from unittest.mock import AsyncMock @@ -118,6 +119,17 @@ def get_device_response(device_name: str) -> DeviceResponse: return DeviceResponse.from_json(load_fixture(f"devices/{device_name}.json", DOMAIN)) +@cache +def get_fixture_name(device_id: str) -> str: + """Get the fixture name for a given device ID.""" + for fixture_name in DEVICE_FIXTURES: + for device in get_device_response(fixture_name).items: + if device.device_id == device_id: + return fixture_name + + raise KeyError(f"Fixture for device_id {device_id} not found") + + async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: """Fixture for setting up the component.""" config_entry.add_to_hass(hass) @@ -136,8 +148,13 @@ def snapshot_smartthings_entities( entities = hass.states.async_all(platform) for entity_state in entities: entity_entry = entity_registry.async_get(entity_state.entity_id) - assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry") - assert entity_state == snapshot(name=f"{entity_entry.entity_id}-state") + prefix = "" + if platform != Platform.SCENE: + # SCENE unique id is not based on device fixture + device_id = entity_entry.unique_id[:36] + prefix = f"{get_fixture_name(device_id)}][" + assert entity_entry == snapshot(name=f"{prefix}{entity_entry.entity_id}-entry") + assert entity_state == snapshot(name=f"{prefix}{entity_entry.entity_id}-state") def set_attribute_value( diff --git a/tests/components/smartthings/conftest.py b/tests/components/smartthings/conftest.py index 08b7289a1d1e..920c7e1ce9e7 100644 --- a/tests/components/smartthings/conftest.py +++ b/tests/components/smartthings/conftest.py @@ -31,7 +31,7 @@ from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_CLIENT_S from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component -from . import DEVICE_FIXTURES, get_device_response, get_device_status +from . import DEVICE_FIXTURES, get_device_response, get_device_status, get_fixture_name from tests.common import MockConfigEntry, load_fixture @@ -99,23 +99,33 @@ def mock_smartthings() -> Generator[AsyncMock]: yield client -@pytest.fixture(params=DEVICE_FIXTURES) -def device_fixture( - mock_smartthings: AsyncMock, request: pytest.FixtureRequest -) -> Generator[str]: +@pytest.fixture +def device_fixture() -> str | None: """Return every device.""" - return request.param + return None @pytest.fixture -def devices(mock_smartthings: AsyncMock, device_fixture: str) -> Generator[AsyncMock]: +def devices(mock_smartthings: AsyncMock, device_fixture: str | None) -> AsyncMock: """Return a specific device.""" - mock_smartthings.get_devices.return_value = get_device_response( - device_fixture - ).items - mock_smartthings.get_device_status.return_value = get_device_status( - device_fixture - ).components + if device_fixture is not None: + mock_smartthings.get_devices.return_value = get_device_response( + device_fixture + ).items + mock_smartthings.get_device_status.return_value = get_device_status( + device_fixture + ).components + else: + devices = [] + for device_name in DEVICE_FIXTURES: + devices.extend(get_device_response(device_name).items) + mock_smartthings.get_devices.return_value = devices + + async def _get_device_status(device_id: str): + return get_device_status(get_fixture_name(device_id)).components + + mock_smartthings.get_device_status.side_effect = _get_device_status + return mock_smartthings diff --git a/tests/components/smartthings/test_init.py b/tests/components/smartthings/test_init.py index fbaeb3c62ec1..fd27079e20ba 100644 --- a/tests/components/smartthings/test_init.py +++ b/tests/components/smartthings/test_init.py @@ -43,7 +43,13 @@ from homeassistant.helpers.config_entry_oauth2_flow import ( ImplementationUnavailableError, ) -from . import DEVICE_FIXTURES, get_device_response, setup_integration, trigger_update +from . import ( + DEVICE_FIXTURES, + get_device_response, + get_fixture_name, + setup_integration, + trigger_update, +) from tests.common import MockConfigEntry, async_load_fixture @@ -74,12 +80,13 @@ async def test_devices( """Test all entities.""" await setup_integration(hass, mock_config_entry) - device_id = devices.get_devices.return_value[0].device_id + for specs in devices.get_devices.return_value: + device_id = specs.device_id - device = device_registry.async_get_device({(DOMAIN, device_id)}) + device = device_registry.async_get_device({(DOMAIN, device_id)}) - assert device is not None - assert device == snapshot + assert device is not None + assert device == snapshot(name=get_fixture_name(device_id)) @pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])