Repository: home-assistant/core
File: tests/components/saunum/test_number.py (Python)

Code before:
        )


@pytest.mark.usefixtures("init_integration")
async def test_set_value_while_session_active(
    hass: HomeAssistant,
    mock_saunum_client: MagicMock,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test error when trying to change duration while session is active."""
    entity_id = "number.saunum_leil_sauna_duration"

    # Update mock data to have session active
    base_data = mock_saunum_client.async_get_data.return_value
    mock_saunum_client.async_get_data.return_value = replace(
        base_data,
        session_active=True,
    )

    # Trigger coordinator update
    coordinator = mock_config_entry.runtime_data
    await coordinator.async_refresh()
    await hass.async_block_till_done()

    # Attempt to set value should raise ServiceValidationError
    with pytest.raises(ServiceValidationError):
        await hass.services.async_call(
            NUMBER_DOMAIN,
            SERVICE_SET_VALUE,
            {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 180},
            blocking=True,
        )


async def test_number_with_default_duration(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_saunum_client: MagicMock,
) -> None:
    """Test number entities use default when device returns None."""
    # Set duration to None (device hasn't set it yet)
    base_data = mock_saunum_client.async_get_data.return_value
    mock_saunum_client.async_get_data.return_value = replace(
        base_data,
        sauna_duration=None,
        fan_duration=None,
    )

    mock_config_entry.add_to_hass(hass)
    assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
    await hass.async_block_till_done()

Review comment (suggestion):
```suggestion
    with pytest.raises(ServiceValidationError, match="...."):
```
We can match on the exception

Code after:
        )


@pytest.mark.usefixtures("init_integration")
async def test_set_value_while_session_active(
    hass: HomeAssistant,
    mock_saunum_client: MagicMock,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test error when trying to change duration while session is active."""
    entity_id = "number.saunum_leil_sauna_duration"

    # Update mock data to have session active
    base_data = mock_saunum_client.async_get_data.return_value
    mock_saunum_client.async_get_data.return_value = replace(
        base_data,
        session_active=True,
    )

    # Trigger coordinator update
    coordinator = mock_config_entry.runtime_data
    await coordinator.async_refresh()
    await hass.async_block_till_done()

    # Attempt to set value should raise ServiceValidationError
    with pytest.raises(
        ServiceValidationError,
        match="Cannot change sauna duration while session is active",
    ):
        await hass.services.async_call(
            NUMBER_DOMAIN,
            SERVICE_SET_VALUE,
            {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 180},
            blocking=True,
        )


async def test_number_with_default_duration(
    hass: HomeAssistant,
    mock_config_entry: MockConfigEntry,
    mock_saunum_client: MagicMock,
) -> None:
    """Test number entities use default when device returns None."""
    # Set duration to None (device hasn't set it yet)
    base_data = mock_saunum_client.async_get_data.return_value
    mock_saunum_client.async_get_data.return_value = replace(
        base_data,
        sauna_duration=None,
        fan_duration=None,
    )