Repository: home-assistant/core
File: tests/components/surepetcare/test_sensor.py (Python)

Code before:
    """Test the generation of unique ids."""
    state_entity_ids = hass.states.async_entity_ids()

    for entity_id, unique_id in EXPECTED_ENTITY_IDS.items():
        assert entity_id in state_entity_ids
        state = hass.states.get(entity_id)
        assert state
        assert state.state == "100"
        entity = entity_registry.async_get(entity_id)
        assert entity.unique_id == unique_id


async def test_disabled_sensors(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    surepetcare,
    mock_config_entry_setup: MockConfigEntry,
) -> None:
    """Test disabled sensor entities."""
    flap_device_id_entity = "sensor.pet_last_seen_flap_device_id"
    user_id_entity = "sensor.pet_last_seen_user_id"

    assert not hass.states.get(flap_device_id_entity)
    assert not hass.states.get(user_id_entity)

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

        async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
        await hass.async_block_till_done(wait_background_tasks=True)

    state = hass.states.get(flap_device_id_entity)
    assert state
    assert state.state == str(MOCK_PET["position"]["device_id"])
    entity = entity_registry.async_get(flap_device_id_entity)
    assert entity.unique_id == DISABLED_ENTITY_IDS[flap_device_id_entity]

    state = hass.states.get(user_id_entity)
    assert state
    assert state.state == str(MOCK_PET["position"]["user_id"])
    entity = entity_registry.async_get(user_id_entity)
    assert entity.unique_id == DISABLED_ENTITY_IDS[user_id_entity]


Review comment (suggestion):
Instead of this, use the `entity_registry_enabled_by_default` fixture in a test which checks the sensor state when they are enabled.

You can check that they are not enabled by default in a separate test (check that they exist in the entity registry  but are disabled and don't have a state).

Example: https://github.com/home-assistant/core/blob/c5c2690a57a71e9502f6af430089b01f64d0b321/tests/components/accuweather/test_sensor.py#L34-L41

Code after:

DEFAULT_DISABLED_ENTITIES = [
    "sensor.pet_last_seen_flap_device_id",
    "sensor.pet_last_seen_user_id",
]


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_sensors(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    surepetcare,
    mock_config_entry_setup: MockConfigEntry,
) -> None:
    """Test the generation of unique ids and sensor states."""
    state_entity_ids = hass.states.async_entity_ids()

    for entity_id, unique_id, expected_state in EXPECTED_ENTITIES:
        assert entity_id in state_entity_ids
        state = hass.states.get(entity_id)
        assert state
        assert state.state == expected_state
        entity = entity_registry.async_get(entity_id)
        assert entity.unique_id == unique_id


async def test_default_disabled_sensors(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    surepetcare,
    mock_config_entry_setup: MockConfigEntry,
) -> None:
    """Test sensor entities that are disabled by default."""
    for entity_id in DEFAULT_DISABLED_ENTITIES:
        entity = entity_registry.async_get(entity_id)
        assert entity
        assert entity.disabled_by == RegistryEntryDisabler.INTEGRATION
        assert not hass.states.get(entity_id)