Repository: home-assistant/core
File: tests/components/zwave_js/test_init.py (Python)

Code before:
    assert state
    assert state.state == STATE_UNAVAILABLE

    # existing entities and the entities with removed values should be unavailable
    new_unavailable_entities = {
        state.entity_id
        for state in hass.states.async_all()
        if state.state == STATE_UNAVAILABLE
    }
    assert (
        unavailable_entities
        | {
            battery_level_entity,
            binary_cover_entity,
            idle_cover_status_button_entity,
        }
        == new_unavailable_entities
    )

    # Entities should still be in the entity registry (not fully removed)
    assert entity_registry.async_get(battery_level_entity) is not None
    assert entity_registry.async_get(binary_cover_entity) is not None
    assert entity_registry.async_get(idle_cover_status_button_entity) is not None


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_value_removed_and_readded(
    hass: HomeAssistant,
    zp3111: Node,
    client: MagicMock,
    integration: MockConfigEntry,
) -> None:
    """Test entity recovers when primary value is removed and re-added."""
    battery_level_entity = "sensor.4_in_1_sensor_battery_level"

    state = hass.states.get(battery_level_entity)
    assert state
    assert state.state != STATE_UNAVAILABLE

    # Remove the battery level value
    event = Event(
        type="value removed",
        data={
            "source": "node",
            "event": "value removed",
            "nodeId": zp3111.node_id,
            "args": {
                "commandClassName": "Battery",
                "commandClass": 128,
                "endpoint": 0,
                "property": "level",

Review comment (suggestion):
If we're only interested in a  single entity, I'd use the platforms fixture for this test and only enable the wanted platform to speed up the test.

Use `pytest.mark.parametrize`.

Code after:
    assert state
    assert state.state == STATE_UNAVAILABLE

    # existing entities and the entities with removed values should be unavailable
    new_unavailable_entities = {
        state.entity_id
        for state in hass.states.async_all()
        if state.state == STATE_UNAVAILABLE
    }
    assert (
        unavailable_entities
        | {
            battery_level_entity,
            binary_cover_entity,
            idle_cover_status_button_entity,
        }
        == new_unavailable_entities
    )

    # Entities should still be in the entity registry (not fully removed)
    assert entity_registry.async_get(battery_level_entity) is not None
    assert entity_registry.async_get(binary_cover_entity) is not None
    assert entity_registry.async_get(idle_cover_status_button_entity) is not None


@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize("platforms", [[Platform.SENSOR]])
async def test_value_removed_and_readded(
    hass: HomeAssistant,
    zp3111: Node,
    client: MagicMock,
    integration: MockConfigEntry,
) -> None:
    """Test entity recovers when primary value is removed and re-added."""
    battery_level_entity = "sensor.4_in_1_sensor_battery_level"

    state = hass.states.get(battery_level_entity)
    assert state
    assert state.state == "0.0"

    # Remove the battery level value
    event = Event(
        type="value removed",
        data={
            "source": "node",
            "event": "value removed",
            "nodeId": zp3111.node_id,
            "args": {
                "commandClassName": "Battery",
                "commandClass": 128,
                "endpoint": 0,